From ac6c4f132bdf3c57b9151e76614e1ba09f7e13bc Mon Sep 17 00:00:00 2001 From: usbharu Date: Sun, 17 Nov 2024 19:07:06 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E7=95=B0=E7=A8=AE=E3=83=AA=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/markdown/AstNode.kt | 16 +++++- .../kotlin/dev/usbharu/markdown/Parser.kt | 10 +++- .../kotlin/dev/usbharu/markdown/ParserTest.kt | 49 +++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt index 0fc95ff..0618793 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/AstNode.kt @@ -86,16 +86,28 @@ sealed class AstNode { fun nestedPrint(nest: Int): String { val builder = StringBuilder() for (node in itemNode) { - builder.append(" ".repeat(nest)).append("- ").append(node.nestedPrint(nest)).append("\n") + builder.append(" ".repeat(nest)).append(char).append(" ").append(node.nestedPrint(nest)).append("\n") } return builder.toString().trim('\n') } + + protected abstract val char: String + } + + data class DecimalListNode(override val itemNode: List) : ListNode(itemNode) { + @JsName("with") + constructor(vararg nodes: ListItemNode) : this(nodes.toList()) + + override val char: String + get() = "1." } - data class DecimalListNode(override val itemNode: List) : ListNode(itemNode) data class DiscListNode(override val itemNode: List) : ListNode(itemNode) { @JsName("with") constructor(vararg nodes: ListItemNode) : this(nodes.toList()) + + override val char: String + get() = "-" } data class ListItemNode(val nodes: MutableList) : BlockNode() { diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt index c92b067..aec00b8 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Parser.kt @@ -76,9 +76,17 @@ class Parser { } break } - while (iterator.peekOrNull() is Token.List) { + val peekOrNull = iterator.peekOrNull() + if (peekOrNull is Token.List) { iterator.skip() + if (peekOrNull.type != list.type) { + if (item.isNotEmpty()) { + listItems.add(ListItemNode(item)) + } + break + } } + if (item.isNotEmpty()) { listItems.add(ListItemNode(item)) } diff --git a/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt b/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt index 977317c..3cb69b1 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/ParserTest.kt @@ -621,4 +621,53 @@ class ParserTest { ), actual ) } + + @Test + fun 異種listネスト() { + val parser = Parser() + + val actual = parser.parse( + listOf( + DiscList, + Text("aiueo"), + LineBreak(1), + Whitespace(4, ' '), + DecimalList('1'), + Text("abcd"), + LineBreak(1), + Whitespace(4, ' '), + DecimalList('1'), + Text("efgh"), + LineBreak(1), + DiscList, + Text("hoge") + ) + ) + + println(actual) + println(actual.print()) + + assertEquals( + RootNode( + BodyNode( + DiscListNode( + ListItemNode( + PlainText("aiueo"), + DecimalListNode( + ListItemNode( + PlainText("abcd"), + ), + ListItemNode( + PlainText("efgh"), + ) + ) + ), + ListItemNode( + PlainText("hoge") + ) + ) + ) + ), actual + ) + } } \ No newline at end of file