Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change iterables to iterators in inlineContent #340

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 50 additions & 62 deletions markdown/core/api/core.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.commonmark.node.HardLineBreak as CMHardLineBreak
import org.commonmark.node.HtmlInline as CMHtmlInline
import org.commonmark.node.Image as CMImage
import org.commonmark.node.Link as CMLink
import org.commonmark.node.Paragraph as CMParagraph
import org.commonmark.node.SoftLineBreak as CMSoftLineBreak
import org.commonmark.node.StrongEmphasis as CMStrongEmphasis
import org.commonmark.node.Text as CMText
Expand Down Expand Up @@ -52,9 +51,6 @@ public sealed interface InlineMarkdown {
@JvmInline
public value class Link(override val nativeNode: CMLink) : InlineMarkdown

@JvmInline
public value class Paragraph(override val nativeNode: CMParagraph) : InlineMarkdown

@JvmInline
public value class SoftLineBreak(override val nativeNode: CMSoftLineBreak) : InlineMarkdown

Expand All @@ -64,19 +60,22 @@ public sealed interface InlineMarkdown {
@JvmInline
public value class Text(override val nativeNode: CMText) : InlineMarkdown

public val children: Iterator<InlineMarkdown>
get() = object : Iterator<InlineMarkdown> {
var current = [email protected]

override fun hasNext(): Boolean = current != null

override fun next(): InlineMarkdown =
if (hasNext()) {
current.toInlineNode().also {
current = current.next
}
} else {
throw NoSuchElementException()
public val children: Iterable<InlineMarkdown>
get() = object : Iterable<InlineMarkdown> {
override fun iterator(): Iterator<InlineMarkdown> =
object : Iterator<InlineMarkdown> {
var current = [email protected]

override fun hasNext(): Boolean = current != null

override fun next(): InlineMarkdown =
if (hasNext()) {
current.toInlineNode().also {
current = current.next
}
} else {
throw NoSuchElementException()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.jetbrains.jewel.markdown

import org.commonmark.node.Block
import org.commonmark.node.Heading as CMHeading
import org.commonmark.node.Paragraph as CMParagraph

public sealed interface MarkdownBlock {

public data class BlockQuote(val children: List<MarkdownBlock>) : MarkdownBlock
Expand All @@ -20,10 +24,17 @@ public sealed interface MarkdownBlock {

public interface CustomBlock : MarkdownBlock

public data class Heading(
override val inlineContent: List<InlineMarkdown>,
val level: Int,
) : MarkdownBlock, BlockWithInlineMarkdown
@JvmInline
public value class Heading(
private val nativeBlock: CMHeading,
) : MarkdownBlock, BlockWithInlineMarkdown {

override val inlineContent: Iterable<InlineMarkdown>
get() = nativeBlock.inlineContent()

public val level: Int
get() = nativeBlock.level
}

public data class HtmlBlock(val content: String) : MarkdownBlock

Expand Down Expand Up @@ -52,11 +63,34 @@ public sealed interface MarkdownBlock {

public object ThematicBreak : MarkdownBlock

public data class Paragraph(override val inlineContent: List<InlineMarkdown>) :
MarkdownBlock, BlockWithInlineMarkdown
@JvmInline
public value class Paragraph(private val nativeBlock: CMParagraph) : MarkdownBlock, BlockWithInlineMarkdown {

override val inlineContent: Iterable<InlineMarkdown>
get() = nativeBlock.inlineContent()
}
}

public interface BlockWithInlineMarkdown {

public val inlineContent: List<InlineMarkdown>
public val inlineContent: Iterable<InlineMarkdown>
}

private fun Block.inlineContent(): Iterable<InlineMarkdown> =
object : Iterable<InlineMarkdown> {
override fun iterator(): Iterator<InlineMarkdown> =
object : Iterator<InlineMarkdown> {
var current = [email protected]

override fun hasNext(): Boolean = current != null

override fun next(): InlineMarkdown =
if (hasNext()) {
current.toInlineNode().also {
current = current.next
}
} else {
throw NoSuchElementException()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import org.jetbrains.jewel.markdown.MarkdownBlock.ListBlock
import org.jetbrains.jewel.markdown.MimeType
import org.jetbrains.jewel.markdown.extensions.MarkdownProcessorExtension
import org.jetbrains.jewel.markdown.rendering.DefaultInlineMarkdownRenderer
import org.jetbrains.jewel.markdown.toInlineNode
import org.commonmark.node.ListBlock as CMListBlock

/**
Expand Down Expand Up @@ -180,13 +179,13 @@ public class MarkdownProcessor(
private fun Node.tryProcessMarkdownBlock(): MarkdownBlock? =
// Non-Block children are ignored
when (this) {
is Paragraph -> toMarkdownParagraphOrNull()
is Paragraph -> MarkdownBlock.Paragraph(this)
is Heading -> toMarkdownHeadingOrNull()
is BulletList -> toMarkdownListOrNull()
is OrderedList -> toMarkdownListOrNull()
is BlockQuote -> toMarkdownBlockQuote()
is FencedCodeBlock -> toMarkdownCodeBlockOrNull()
is IndentedCodeBlock -> toMarkdownCodeBlockOrNull()
is BlockQuote -> toMarkdownBlockQuote()
is ThematicBreak -> MarkdownBlock.ThematicBreak
is HtmlBlock -> toMarkdownHtmlBlockOrNull()
is CustomBlock -> {
Expand All @@ -202,14 +201,7 @@ public class MarkdownProcessor(

private fun Heading.toMarkdownHeadingOrNull(): MarkdownBlock.Heading? {
if (level < 1 || level > 6) return null
return MarkdownBlock.Heading(contentsAsInlineMarkdown(), level)
}

private fun Paragraph.toMarkdownParagraphOrNull(): MarkdownBlock.Paragraph? {
val inlineMarkdown = contentsAsInlineMarkdown()

if (inlineMarkdown.isEmpty()) return null
return MarkdownBlock.Paragraph(inlineMarkdown)
return MarkdownBlock.Heading(this)
}

private fun FencedCodeBlock.toMarkdownCodeBlockOrNull(): CodeBlock.FencedCodeBlock =
Expand Down Expand Up @@ -264,10 +256,4 @@ public class MarkdownProcessor(
if (literal.isBlank()) return null
return MarkdownBlock.HtmlBlock(content = literal.trimEnd('\n'))
}

private fun Node.contentsAsInlineMarkdown() = buildList {
forEachChild {
add(it.toInlineNode())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ public open class DefaultInlineMarkdownRenderer(rendererExtensions: List<Markdow
.build()

public override fun renderAsAnnotatedString(
inlineMarkdown: List<InlineMarkdown>,
inlineMarkdown: Iterable<InlineMarkdown>,
styling: InlinesStyling,
): AnnotatedString =
buildAnnotatedString {
appendInlineMarkdownFrom(inlineMarkdown.iterator(), styling)
appendInlineMarkdownFrom(inlineMarkdown, styling)
}

@OptIn(ExperimentalTextApi::class)
private fun Builder.appendInlineMarkdownFrom(inlineMarkdown: Iterator<InlineMarkdown>, styling: InlinesStyling) {
private fun Builder.appendInlineMarkdownFrom(inlineMarkdown: Iterable<InlineMarkdown>, styling: InlinesStyling) {
for (child in inlineMarkdown) {
when (child) {
is InlineMarkdown.Paragraph -> {
appendInlineMarkdownFrom(child.children, styling)
}

is InlineMarkdown.Text -> append(child.nativeNode.literal)

is InlineMarkdown.Emphasis -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface InlineMarkdownRenderer {
* Render the [inlineMarkdown] as an [AnnotatedString], using the [styling]
* provided.
*/
public fun renderAsAnnotatedString(inlineMarkdown: List<InlineMarkdown>, styling: InlinesStyling): AnnotatedString
public fun renderAsAnnotatedString(inlineMarkdown: Iterable<InlineMarkdown>, styling: InlinesStyling): AnnotatedString

public companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,18 @@ private fun inlineMarkdowns(content: String): List<InlineMarkdown> {
private val inlineParser = InlineParserImpl(InlineParserContextImpl(emptyList(), LinkReferenceDefinitions()))

fun paragraph(@Language("Markdown") content: String): Paragraph = Paragraph(
object : org.commonmark.node.CustomBlock() {}.let { block ->
org.commonmark.node.Paragraph().let { block ->
inlineParser.parse(SourceLines.of(content.lines().map { SourceLine.of(it, null) }), block)
block
}.children().map { x -> x.toInlineNode() },
},
)

fun heading(level: Int, @Language("Markdown") content: String) = Heading(
object : org.commonmark.node.CustomBlock() {}.let { block ->
org.commonmark.node.Heading().let { block ->
inlineParser.parse(SourceLines.of(SourceLine.of(content, null)), block)
block.level = level
block
}.children().map { x -> x.toInlineNode() },
level,
},
)

fun indentedCodeBlock(content: String) = IndentedCodeBlock(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class AutolinkProcessorExtensionTest {
val processor = MarkdownProcessor(listOf(AutolinkProcessorExtension))
val rawMarkDown = "https://commonmark.org"
val processed = processor.processMarkdownDocument(rawMarkDown)
val paragraph = processed[0] as MarkdownBlock.Paragraph
val paragraph = processed.first() as MarkdownBlock.Paragraph

assertTrue(paragraph.inlineContent[0] is InlineMarkdown.Link)
assertTrue(paragraph.inlineContent.first() is InlineMarkdown.Link)
}
}