打ち消し線を追加

This commit is contained in:
usbharu 2024-11-14 15:52:34 +09:00
parent 3ad0cd6f92
commit 4ce6832379
Signed by: usbharu
GPG Key ID: 8CB1087135660B8D
3 changed files with 87 additions and 7 deletions

View File

@ -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 -> { else -> {
addText(tokens, next.toString()) addText(tokens, next.toString())
@ -88,7 +102,6 @@ class Lexer {
inQuote = false inQuote = false
} }
println(tokens)
val lastToken = tokens.lastOrNull() val lastToken = tokens.lastOrNull()
if (lastToken is Break) { if (lastToken is Break) {
if (lastToken.count == 1) { if (lastToken.count == 1) {
@ -161,7 +174,7 @@ class Lexer {
} }
} else { } else {
val peekString = peekString(next, iterator) val peekString = peekString(iterator, next)
if (peekString != null && peekString.isEmpty()) { if (peekString != null && peekString.isEmpty()) {
addText(tokens, "$next$next") addText(tokens, "$next$next")
iterator.next() iterator.next()
@ -345,17 +358,24 @@ class Lexer {
return count return count
} }
fun peekString(char: Char, iterator: PeekableCharIterator): String? { fun peekString(iterator: PeekableCharIterator, vararg char: Char): String? {
var counter = 0 var counter = 0
val stringBuilder = StringBuilder() 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)) stringBuilder.append(iterator.peekOrNull(counter))
if (iterator.peekOrNull(counter) == char[checkCounter]) {
checkCounter++
} else {
checkCounter = 0
}
counter++ counter++
} }
if (iterator.peekOrNull(counter) == null) { if (iterator.peekOrNull(counter) == null && checkCounter != char.size) {
return null return null
} }
return stringBuilder.toString() val string = stringBuilder.toString()
return string.substring(0, string.length - char.size)
} }
fun collect(iterator: PeekableCharIterator): String { fun collect(iterator: PeekableCharIterator): String {

View File

@ -29,4 +29,4 @@ data class UrlTitle(val title: String) : Token()
data class InlineCodeBlock(val text: String) : Token() data class InlineCodeBlock(val text: String) : Token()
data class CodeBlock(val text: String) : Token() data class CodeBlock(val text: String) : Token()
data class CodeBlockLanguage(val language: String, val filename: String) : Token() data class CodeBlockLanguage(val language: String, val filename: String) : Token()
data object Tilde : Token() data class Strike(val strike: String) : Token()

View File

@ -2,6 +2,7 @@ package dev.usbharu.markdown
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertContentEquals import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class LexerTest { class LexerTest {
@ -825,4 +826,63 @@ class LexerTest {
), actual ), 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
)
}
} }