Created
May 29, 2022 19:31
-
-
Save sander/9c55318323731a8c437dc5ae1098d718 to your computer and use it in GitHub Desktop.
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
import scala.util.matching.Regex | |
val in = """# Systems | |
| | |
|## System 1 | |
| | |
|- Foo: bar | |
|- Baz: qux | |
|- Bla: | |
| 1. Bla | |
| 2. Bla | |
| 3. Bla | |
| | |
|### Header | |
| | |
|askdjfalskdjfalsdf | |
| | |
|### Another header | |
| | |
|Content | |
| | |
|## System 2 | |
| | |
|### Property | |
| | |
|- value | |
|- value | |
| | |
|## System 3 | |
| | |
|- foo: bar""".stripMargin | |
def systemNameToPropertyMap(input: String): Map[String, Map[String, String]] = { | |
val system = raw"(?m)^## (.*)\n{2}((?:.|\n)*?)(?=^## |\z)".r | |
val item = raw"(?m)^- (.*):((?:.|\n)*?)\s*(?=^- |^#{2,3} |\z)".r | |
val block = raw"(?m)^### (.*)\n\s*((?:.|\n)*?)\s*(?=^#{2,3} |\z)".r | |
val inline = raw"(?m)\A (.*)\z".r | |
val indented = raw"(?m)\A(\n {2}.*)*\z".r | |
def content(rawValue: String) = | |
rawValue match { | |
case inline(value) => value | |
case indented(_*) => rawValue.replaceFirst("\n {2}", "").replaceAll("\n {2}", "\n") | |
} | |
def groups(regex: Regex, input: String) = regex.findAllMatchIn(input).map(_.subgroups) | |
(for { | |
name :: body :: Nil <- groups(system, input) | |
items = for (k :: v :: Nil <- groups(item, body)) yield k -> content(v) | |
blocks = for (k :: v :: Nil <- groups(block, body)) yield k -> v | |
} yield name -> (items ++ blocks).toMap).toMap | |
} | |
println(systemNameToPropertyMap(in)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment