Skip to content

Commit

Permalink
Change iterables to iterators in inlineContent
Browse files Browse the repository at this point in the history
  • Loading branch information
obask committed Apr 1, 2024
1 parent 82d3581 commit bfb908e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 120 deletions.
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 @@ -58,25 +57,25 @@ public sealed interface InlineMarkdown {
@JvmInline
public value class StrongEmphasis(override val value: CMStrongEmphasis) : InlineMarkdown

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

@JvmInline
public value class Text(override val value: CMText) : InlineMarkdown

public val children: Iterator<InlineMarkdown>
get() = object : Iterator<InlineMarkdown> {
var current = this@InlineMarkdown.value.firstChild

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 = this@InlineMarkdown.value.firstChild

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 content: List<MarkdownBlock>) : MarkdownBlock
Expand All @@ -20,10 +24,17 @@ public sealed interface MarkdownBlock {
) : CodeBlock
}

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 = this@inlineContent.firstChild

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.CodeBlock
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

/**
* @param optimizeEdits Optional. Indicates whether the processing should only update the changed blocks
Expand Down Expand Up @@ -179,9 +178,9 @@ public class MarkdownProcessor(
private fun Node.tryProcessMarkdownBlock(): MarkdownBlock? =
// Non-Block children are ignored
when (this) {
is BlockQuote -> toMarkdownBlockQuote()
is Paragraph -> MarkdownBlock.Paragraph(this)
is Heading -> toMarkdownHeadingOrNull()
is Paragraph -> toMarkdownParagraphOrNull()
is BlockQuote -> toMarkdownBlockQuote()
is FencedCodeBlock -> toMarkdownCodeBlockOrNull()
is IndentedCodeBlock -> toMarkdownCodeBlockOrNull()
is BulletList -> toMarkdownListOrNull()
Expand All @@ -192,24 +191,16 @@ public class MarkdownProcessor(
extensions.find { it.processorExtension.canProcess(this) }
?.processorExtension?.processMarkdownBlock(this, this@MarkdownProcessor)
}

else -> null
}

private fun BlockQuote.toMarkdownBlockQuote(): MarkdownBlock.BlockQuote =
MarkdownBlock.BlockQuote(processChildren(this))

private fun Heading.toMarkdownHeadingOrNull(): MarkdownBlock.Heading? =
if (level > 6) {
null
} else {
MarkdownBlock.Heading(contentsAsInlineMarkdown(), level)
}

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

if (inlineMarkdown.isEmpty()) return null
return MarkdownBlock.Paragraph(inlineMarkdown)
private fun Heading.toMarkdownHeadingOrNull(): MarkdownBlock.Heading? {
if (level < 1 || level > 6) return null
return MarkdownBlock.Heading(this)
}

private fun FencedCodeBlock.toMarkdownCodeBlockOrNull(): CodeBlock.FencedCodeBlock =
Expand Down Expand Up @@ -264,10 +255,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.value.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

0 comments on commit bfb908e

Please sign in to comment.