feat: Basic Html to String converter

It works okay-ish for basic HTML which should be good enough for now
main
Henrik Böving 2021-12-10 15:24:38 +01:00
parent 11de4f7f55
commit e9a9e17439
1 changed files with 21 additions and 0 deletions

View File

@ -26,6 +26,27 @@ inductive Html where
instance : Coe String Html := instance : Coe String Html :=
⟨Html.text⟩ ⟨Html.text⟩
namespace Html
def attributesToString (attrs : Array (String × String)) :String :=
attrs.foldl (λ acc (k, v) => acc ++ " " ++ k ++ "=\"" ++ v ++ "\"") ""
-- TODO: Termination proof
partial def toStringAux : Html → String
| element tag attrs #[] => s!"<{tag}{attributesToString attrs}/>\n"
| element tag attrs #[text s] => s!"<{tag}{attributesToString attrs}>{s}</{tag}>\n"
| element tag attrs #[child] => s!"<{tag}{attributesToString attrs}>\n{child.toStringAux}</{tag}>\n"
| element tag attrs children => s!"<{tag}{attributesToString attrs}>\n{children.foldl (· ++ toStringAux ·) ""}</{tag}>\n"
| text s => s
def toString (html : Html) : String :=
html.toStringAux.trimRight
instance : ToString Html :=
⟨toString⟩
end Html
namespace Jsx namespace Jsx
open Parser PrettyPrinter open Parser PrettyPrinter