From 4ce6832379846212fa163fd1acbe372c69d4a60e Mon Sep 17 00:00:00 2001 From: usbharu Date: Thu, 14 Nov 2024 15:52:34 +0900 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E3=81=A1=E6=B6=88=E3=81=97=E7=B7=9A?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/markdown/Lexer.kt | 32 ++++++++-- .../kotlin/dev/usbharu/markdown/Token.kt | 2 +- .../kotlin/dev/usbharu/markdown/LexerTest.kt | 60 +++++++++++++++++++ 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt index dc6ad55..622fc3f 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Lexer.kt @@ -68,6 +68,20 @@ class Lexer { } } + next == '~' || next == '~' -> { + if (iterator.peekOrNull() == next) { + iterator.next() + val peekString = peekString(iterator, next, next) + if (peekString == null) { + addText(tokens, "$next$next") + } else { + tokens.add(Strike(peekString)) + iterator.skip(peekString.length + 2) + } + } else { + addText(tokens, next.toString()) + } + } else -> { addText(tokens, next.toString()) @@ -88,7 +102,6 @@ class Lexer { inQuote = false } - println(tokens) val lastToken = tokens.lastOrNull() if (lastToken is Break) { if (lastToken.count == 1) { @@ -161,7 +174,7 @@ class Lexer { } } else { - val peekString = peekString(next, iterator) + val peekString = peekString(iterator, next) if (peekString != null && peekString.isEmpty()) { addText(tokens, "$next$next") iterator.next() @@ -345,17 +358,24 @@ class Lexer { return count } - fun peekString(char: Char, iterator: PeekableCharIterator): String? { + fun peekString(iterator: PeekableCharIterator, vararg char: Char): String? { var counter = 0 val stringBuilder = StringBuilder() - while (iterator.peekOrNull(counter) != null && iterator.peekOrNull(counter) != char) { + var checkCounter = 0 + while (iterator.peekOrNull(counter) != null && checkCounter < char.size) { stringBuilder.append(iterator.peekOrNull(counter)) + if (iterator.peekOrNull(counter) == char[checkCounter]) { + checkCounter++ + } else { + checkCounter = 0 + } counter++ } - if (iterator.peekOrNull(counter) == null) { + if (iterator.peekOrNull(counter) == null && checkCounter != char.size) { return null } - return stringBuilder.toString() + val string = stringBuilder.toString() + return string.substring(0, string.length - char.size) } fun collect(iterator: PeekableCharIterator): String { diff --git a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt index 5540861..5a45610 100644 --- a/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt +++ b/library/src/commonMain/kotlin/dev/usbharu/markdown/Token.kt @@ -29,4 +29,4 @@ data class UrlTitle(val title: String) : Token() data class InlineCodeBlock(val text: String) : Token() data class CodeBlock(val text: String) : Token() data class CodeBlockLanguage(val language: String, val filename: String) : Token() -data object Tilde : Token() \ No newline at end of file +data class Strike(val strike: String) : Token() \ No newline at end of file diff --git a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt index 55330a0..fae9fc1 100644 --- a/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt +++ b/library/src/commonTest/kotlin/dev/usbharu/markdown/LexerTest.kt @@ -2,6 +2,7 @@ package dev.usbharu.markdown import kotlin.test.Test import kotlin.test.assertContentEquals +import kotlin.test.assertEquals class LexerTest { @@ -825,4 +826,63 @@ class LexerTest { ), actual ) } + + @Test + fun peekStringTest() { + val lexer = Lexer() + val iterator = PeekableCharIterator("*a**a***".toCharArray()) + val peekString = lexer.peekString(iterator, '*', '*', '*') + + println(peekString) + + assertEquals("*a**a", peekString) + } + + @Test + fun 打ち消し線() { + val lexer = Lexer() + + val actual = lexer.lex("~~aiueo~~") + + println(actual) + + assertContentEquals( + listOf( + Strike("aiueo") + ), actual + ) + } + + @Test + fun 打ち消し線2() { + val lexer = Lexer() + + val actual = lexer.lex("aiueo ~~aiueo~~bcde") + + println(actual) + + assertContentEquals( + listOf( + Text("aiueo"), + Whitespace(1, ' '), + Strike("aiueo"), + Text("bcde") + ), actual + ) + } + + @Test + fun 打ち消し線かと思ったら違った() { + val lexer = Lexer() + + val actual = lexer.lex("aiueo~~abcd") + + println(actual) + + assertContentEquals( + listOf( + Text("aiueo~~abcd") + ), actual + ) + } } \ No newline at end of file