-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #52 Allow tix config to be defined as part of body.
- Loading branch information
Showing
25 changed files
with
588 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/commonMain/kotlin/org/tix/config/domain/reader/MarkdownConfigParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.tix.config.domain.reader | ||
|
||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.decodeFromString | ||
import org.tix.config.data.raw.RawTixConfiguration | ||
import org.tix.error.TixError | ||
import org.tix.feature.plan.domain.parse.configparser.ConfigLanguage | ||
import org.tix.serialize.TixSerializers | ||
|
||
class MarkdownConfigParser { | ||
private val json = TixSerializers.json() | ||
private val yaml = TixSerializers.yaml() | ||
|
||
fun parse(code: String, language: ConfigLanguage) = | ||
when (language) { | ||
ConfigLanguage.JSON -> parseJson(code) | ||
ConfigLanguage.NO_CONFIG -> null | ||
ConfigLanguage.YAML -> parseYaml(code) | ||
} | ||
|
||
private fun parseJson(code: String) = rethrowErrorsWithMoreContext { | ||
json.decodeFromString<RawTixConfiguration>(code) | ||
} | ||
|
||
private fun parseYaml(code: String) = rethrowErrorsWithMoreContext { | ||
yaml.decodeFromString<RawTixConfiguration>(code) | ||
} | ||
|
||
private fun rethrowErrorsWithMoreContext(decodeBlock: () -> RawTixConfiguration) = | ||
try { | ||
decodeBlock() | ||
} catch (ex: SerializationException) { | ||
throw TixError(message = "Failed to parse configuration in markdown file", cause = ex) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/commonMain/kotlin/org/tix/config/domain/reader/MarkdownConfigurationReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.tix.config.domain.reader | ||
|
||
import org.intellij.markdown.MarkdownElementTypes | ||
import org.intellij.markdown.ast.ASTNode | ||
import org.intellij.markdown.parser.MarkdownParser | ||
import org.tix.config.data.raw.RawTixConfiguration | ||
import org.tix.feature.plan.domain.parse.configparser.ConfigLanguageDetector | ||
import org.tix.feature.plan.domain.parse.defaultMarkdownParser | ||
import org.tix.feature.plan.domain.parse.nodeparser.CodeFenceSegmentCreator | ||
|
||
class MarkdownConfigurationReader(private val markdownParser: MarkdownParser = defaultMarkdownParser()) { | ||
private val configParser = MarkdownConfigParser() | ||
private val headerTypes = listOf( | ||
MarkdownElementTypes.ATX_1.name, | ||
MarkdownElementTypes.ATX_2.name, | ||
MarkdownElementTypes.ATX_3.name, | ||
MarkdownElementTypes.ATX_4.name, | ||
MarkdownElementTypes.ATX_5.name, | ||
MarkdownElementTypes.ATX_6.name | ||
) | ||
|
||
fun configFromMarkdown(markdown: String?): RawTixConfiguration? { | ||
if (markdown == null) { | ||
return null | ||
} | ||
val node = markdownTree(markdown) | ||
val preambleNodes = node.children.takeWhile { !headerTypes.contains(it.type.name) } | ||
return preambleNodes.firstCodeFence() | ||
?.let {codeFence -> | ||
val segment = CodeFenceSegmentCreator.createCodeSegment(codeFence, markdown) | ||
val configType = ConfigLanguageDetector.detect(segment) | ||
configParser.parse(segment.code, configType) | ||
} | ||
} | ||
|
||
private fun markdownTree(markdown: String) = markdownParser.buildMarkdownTreeFromString(markdown) | ||
|
||
private fun List<ASTNode>.firstCodeFence() = | ||
firstOrNull { it.type.name == MarkdownElementTypes.CODE_FENCE.name } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/commonMain/kotlin/org/tix/feature/plan/domain/parse/configparser/ConfigLanguage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.tix.feature.plan.domain.parse.configparser | ||
|
||
enum class ConfigLanguage { | ||
JSON, | ||
NO_CONFIG, | ||
YAML | ||
} |
16 changes: 8 additions & 8 deletions
16
...arse/fieldparser/FieldLanguageDetector.kt → ...se/configparser/ConfigLanguageDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/commonMain/kotlin/org/tix/feature/plan/domain/parse/fieldparser/FieldLanguage.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...commonMain/kotlin/org/tix/feature/plan/domain/parse/nodeparser/CodeFenceSegmentCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.tix.feature.plan.domain.parse.nodeparser | ||
|
||
import org.intellij.markdown.MarkdownTokenTypes | ||
import org.intellij.markdown.ast.ASTNode | ||
import org.intellij.markdown.ast.findChildOfType | ||
import org.intellij.markdown.ast.getTextInNode | ||
import org.tix.ticket.body.CodeBlockSegment | ||
|
||
object CodeFenceSegmentCreator { | ||
fun createCodeSegment(node: ASTNode, markdownText: String) = | ||
CodeBlockSegment(code = code(node, markdownText), language = lang(node, markdownText)) | ||
|
||
private fun code(node: ASTNode, markdownText: String) = | ||
node.children | ||
.joinToString(separator = "") { mapChildNode(it, markdownText) } | ||
|
||
private fun mapChildNode(node: ASTNode, markdownText: String) = | ||
when (node.type.name) { | ||
MarkdownTokenTypes.CODE_FENCE_CONTENT.name -> node.getTextInNode(markdownText).toString() | ||
MarkdownTokenTypes.EOL.name -> "\n" | ||
else -> "" | ||
} | ||
|
||
private fun lang(node: ASTNode, markdownText: String) = | ||
node | ||
.findChildOfType(MarkdownTokenTypes.FENCE_LANG) | ||
?.getTextInNode(markdownText) | ||
?.toString() ?: "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.