feat: Basic Html to String converter
It works okay-ish for basic HTML which should be good enough for nowmain
parent
11de4f7f55
commit
e9a9e17439
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue