打ち消し線を追加
This commit is contained in:
parent
3ad0cd6f92
commit
4ce6832379
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
data class Strike(val strike: String) : Token()
|
|
@ -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
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue