diff options
Diffstat (limited to 'site')
-rw-r--r-- | site/index.html | 67 | ||||
-rw-r--r-- | site/src/Main.scala | 40 |
2 files changed, 107 insertions, 0 deletions
diff --git a/site/index.html b/site/index.html new file mode 100644 index 0000000..ea0dd74 --- /dev/null +++ b/site/index.html @@ -0,0 +1,67 @@ +<!doctype html> +<html> + +<head> + <meta charset="UTF-8"> + <style> + * { + box-sizing: border-box; + } + + html, + body { + margin: 0; + font-family: sans-serif; + width: 100%; + height: 100%; + display: flex; + flex-direction: row; + align-items: stretch; + justify-content: stretch; + } + + div { + flex: 1; + display: flex; + flex-direction: row; + align-items: stretch; + justify-content: stretch; + + border: 1px solid #f1f1f1; + border-radius: .25em; + padding: 1em; + margin: 1em; + box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); + } + + textarea { + flex: 1; + resize: none; + } + + .error { + color: red; + } + </style> +</head> + +<body> + <div> + <textarea id="input" oninput="generate()" placeholder="write yaml here" oninput="parse()"></textarea> + </div> + + <div> + <textarea id="output" placeholder="output" readonly></textarea> + </div> + + <script type="text/javascript" src="out.js"></script> + <script type="text/javascript"> + function generate() { + var str = document.getElementById("input").value + yaml.update(str) + } + </script> + +</body> + +</html> diff --git a/site/src/Main.scala b/site/src/Main.scala new file mode 100644 index 0000000..c009237 --- /dev/null +++ b/site/src/Main.scala @@ -0,0 +1,40 @@ +import scala.scalajs.js.annotation._ +import org.scalajs.dom + +@JSExportTopLevel("yaml") +object Main { + + val text: dom.html.TextArea = + dom.document.getElementById("output").asInstanceOf[dom.html.TextArea] + + @JSExport + def update(str: String): Unit = yamlesque.tryReadAll(str) match { + case Left(err) => + text.classList.add("error") + text.value = err + case Right(yamls) => + text.classList.remove("error") + val jsons = yamls.map(ytoj).map(j => ujson.write(j, 2)) + text.value = jsons.mkString("\n---\n") + } + + def ytoj(y: yamlesque.Node): ujson.Value = y match { + case yamlesque.Obj(fields) => + val j = ujson.Obj() + for ((k, v) <- fields) { + j.obj += k -> ytoj(v) + } + j + case yamlesque.Arr(values) => + val j = ujson.Arr() + for (v <- values) { + j.arr += ytoj(v) + } + j + case yamlesque.Str(x) => ujson.Str(x) + case yamlesque.Num(x) => ujson.Num(x) + case yamlesque.Bool(x) => ujson.Bool(x) + case yamlesque.Null => ujson.Null + } + +} |