Current scope: kotmark-ext-yaml-front-matter| all classes
|
io.heapy.kotmark.ext.front.matter.internal
Coverage Summary for Class: YamlFrontMatterBlockParser (io.heapy.kotmark.ext.front.matter.internal)
| Class | Method, % | Branch, % | Line, % | Instruction, % |
|---|---|---|---|---|
| YamlFrontMatterBlockParser | 100% (4/4) | 100% (2/2) | 100% (13/13) | 100% (70/70) |
| YamlFrontMatterBlockParser$Companion | ||||
| YamlFrontMatterBlockParser$Factory | 100% (2/2) | 83.3% (5/6) | 100% (9/9) | 100% (42/42) |
| YamlFrontMatterBlockParser$WhenMappings | ||||
| Total | 100% (6/6) | 87.5% (7/8) | 100% (22/22) | 100% (112/112) |
package io.heapy.kotmark.ext.front.matter.internal
import io.heapy.kotmark.ext.front.matter.YamlFrontMatterBlock
import io.heapy.kotmark.ext.front.matter.parser.FrontMatterParser
import io.heapy.kotmark.node.Block
import io.heapy.kotmark.node.Document
import io.heapy.kotmark.parser.block.AbstractBlockParser
import io.heapy.kotmark.parser.block.AbstractBlockParserFactory
import io.heapy.kotmark.parser.block.BlockContinue
import io.heapy.kotmark.parser.block.BlockStart
import io.heapy.kotmark.parser.block.MatchedBlockParser
import io.heapy.kotmark.parser.block.ParserState
class YamlFrontMatterBlockParser(
private val frontMatterParser: FrontMatterParser,
) : AbstractBlockParser() {
override val block: Block
field = YamlFrontMatterBlock()
override fun tryContinue(parserState: ParserState): BlockContinue {
val line = parserState.line
if (REGEX_END.matches(line.content)) {
return when (frontMatterParser.onEndingSeparator(block, line)) {
FrontMatterParser.SeparatorRole.BLOCK_END -> BlockContinue.Finished
FrontMatterParser.SeparatorRole.CONTENT -> BlockContinue.AtIndex(parserState.index)
}
}
frontMatterParser.onNextLine(block, line)
return BlockContinue.AtIndex(parserState.index)
}
class Factory(
private val frontMatterParserFactory: FrontMatterParser.Factory,
) : AbstractBlockParserFactory() {
override fun tryStart(
state: ParserState,
matchedBlockParser: MatchedBlockParser,
): BlockStart? {
val parent = matchedBlockParser.matchedBlockParser.block
return if (
parent is Document &&
parent.firstChild == null &&
REGEX_BEGIN.matches(state.line.content)
) {
BlockStart.of(YamlFrontMatterBlockParser(frontMatterParserFactory.create())).atIndex(state.nextNonSpaceIndex)
} else {
BlockStart.none()
}
}
}
companion object {
private val REGEX_BEGIN = Regex("""^-{3}(\s.*)?""")
private val REGEX_END = Regex("""^(-{3}|\.{3})(\s.*)?""")
}
}