Last active
November 20, 2022 02:53
-
-
Save nafg/112bf83e5676ed316f17cea505ea5d93 to your computer and use it in GitHub Desktop.
HTML to scalajs-react VDOM
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
#!/usr/bin/env amm | |
import scala.xml.{Elem, Node, Text, XML} | |
def quoteString(s: String) = | |
'"' + | |
s.replace("\n", "\\n").replace("\"", "\\\"") + | |
'"' | |
def indentSpaces(n: Int) = " " * (n * 2) | |
def toVDOM(node: Node, indentLevel: Int = 0): Option[String] = | |
node match { | |
case elem: Elem => | |
val attrArgs = | |
elem.attributes.asAttrMap.map { | |
case (key, value) => | |
val unsupported = Set("label", "selectedindex", "tabindex", "onclick", "onchange", "style", "onkeyup") | |
val attr = | |
key.toLowerCase match { | |
case s if s.contains("-") || unsupported.contains(s) => "VdomAttr(" + quoteString(s) + ")" | |
case "class" => "^.cls" | |
case "for" => "^.htmlFor" | |
case "type" => "^.tpe" | |
case _ => "^." + key | |
} | |
attr + " := " + quoteString(value) | |
}.toSeq | |
val childArgs = elem.child.map(toVDOM(_, indentLevel + 1)) | |
val res = | |
"<." + elem.label + | |
"(\n" + indentSpaces(indentLevel + 1) + | |
(attrArgs ++ childArgs.flatten).mkString(",\n" + indentSpaces(indentLevel + 1)) + | |
"\n" + indentSpaces(indentLevel) + ")" | |
Some(res) | |
case text: Text => | |
if (text.data.trim.isEmpty) None | |
else Some(quoteString(text.data.trim)) | |
} | |
@main | |
def main(path: String) = | |
try | |
println(toVDOM(XML.loadFile(path)).get) | |
catch { | |
case e => e.printStackTrace() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hero!