mirror of https://git.sr.ht/~oppen/ariane
38 lines
1011 B
Kotlin
38 lines
1011 B
Kotlin
package oppen.ariane.io.gemini
|
|
|
|
import java.lang.StringBuilder
|
|
|
|
object GemtextHelper {
|
|
|
|
/**
|
|
*
|
|
* This is safe for most cases but fails when a line starts with ``` _within_ a code block
|
|
*
|
|
*/
|
|
fun findCodeBlocks(source: List<String>): List<String>{
|
|
val sb = StringBuilder()
|
|
var inCodeBlock = false
|
|
val parsed = mutableListOf<String>()
|
|
source.forEach { line ->
|
|
if (line.startsWith("```")) {
|
|
if (!inCodeBlock) {
|
|
//New code block starting
|
|
sb.clear()
|
|
sb.append("```")
|
|
} else {
|
|
//End of code block
|
|
parsed.add(sb.toString())
|
|
}
|
|
inCodeBlock = !inCodeBlock
|
|
} else {
|
|
if (inCodeBlock) {
|
|
sb.append("$line\n")
|
|
} else {
|
|
parsed.add(line)
|
|
}
|
|
}
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
} |