| /- | |
| Copyright (c) 2026 Lean FRO, LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Wojciech Nawrocki, David Thrane Christiansen | |
| -/ | |
| module | |
| prelude | |
| public import Lean.Data.Html.Basic | |
| import Lean.Data.Html.Spec | |
| import Init.Data.String.Search | |
| set_option doc.verso true | |
| public section | |
| namespace Lean.Html | |
| section render_impl | |
| private inductive RenderWorkItemKind where | |
| | html | |
| | attr | |
| | endAttrs | |
| | endElement | |
| | endVoidElement | |
| private structure RenderWorkItemStack where | |
| kinds : Array RenderWorkItemKind | |
| htmls : Array Html | |
| strs : Array String | |
| namespace RenderWorkItemStack | |
| @[inline] | |
| private def pushKind (q : RenderWorkItemStack) (kind : RenderWorkItemKind) : | |
| RenderWorkItemStack := { | |
| q with kinds := q.kinds.push kind | |
| } | |
| @[inline] | |
| private def pushHtml (q : RenderWorkItemStack) (value : Html) : | |
| RenderWorkItemStack := { | |
| q with htmls := q.htmls.push value | |
| } | |
| @[inline] | |
| private def pushStr (q : RenderWorkItemStack) (str : String) : | |
| RenderWorkItemStack := { | |
| q with strs := q.strs.push str | |
| } | |
| @[inline] | |
| private def popKind (q : RenderWorkItemStack) (h : q.kinds.size ≠ 0) : | |
| RenderWorkItemKind × RenderWorkItemStack := | |
| let kind := q.kinds[q.kinds.size - 1] | |
| let q := { q with kinds := q.kinds.pop } | |
| (kind, q) | |
| @[inline] | |
| private def popHtml! (q : RenderWorkItemStack) : | |
| Html × RenderWorkItemStack := | |
| let value := q.htmls[q.htmls.size - 1]! | |
| let q := { q with htmls := q.htmls.pop } | |
| (value, q) | |
| @[inline] | |
| private def popStr! (q : RenderWorkItemStack) : | |
| String × RenderWorkItemStack := | |
| let str := q.strs[q.strs.size - 1]! | |
| let q := { q with strs := q.strs.pop } | |
| (str, q) | |
| end RenderWorkItemStack | |
| /-- Renders into a string to be consumed by browsers. | |
| The input's structure is respected as much as possible: | |
| - No whitespace is added or removed. | |
| - Tag names are not escaped. | |
| The output uses [HTML5 syntax](https://html.spec.whatwg.org/dev/syntax.html). | |
| It is not compatible with [XML syntax for HTML](https://html.spec.whatwg.org/dev/xhtml.html). | |
| # Details of the output syntax | |
| We make the following XML-compatible choices: | |
| - Void elements with {name}`isEmpty` children are rendered as self-closing tags. | |
| - Void elements with non-{name}`isEmpty` children are rendered like normal elements. | |
| Such elements are forbidden by the HTML spec. | |
| - Attribute values are always double-quoted. | |
| And the following incompatible choices: | |
| - Attributes with empty values are [minimized](https://www.w3.org/TR/xhtml1/#h-4.5). -/ | |
| partial def render (h : Html) : String := | |
| go "" { | |
| kinds := #[.html] | |
| htmls := #[h] | |
| strs := #[] | |
| } | |
| where | |
| go (acc : String) (q : RenderWorkItemStack) : String := | |
| if h : q.kinds.size = 0 then | |
| acc | |
| else | |
| let (kind, q) := q.popKind h | |
| match kind with | |
| | .html => | |
| let (h, q) := q.popHtml! | |
| match h with | |
| | .element tag attrs children => | |
| let q := | |
| if children.isEmpty then | |
| if isVoidElement tag then | |
| q.pushKind .endVoidElement | |
| else | |
| let q := q.pushKind .endElement |>.pushStr tag | |
| q.pushKind .endAttrs | |
| else | |
| let q := q.pushKind .endElement |>.pushStr tag | |
| let q := q.pushKind .html |>.pushHtml children | |
| q.pushKind .endAttrs | |
| let q := attrs.foldr (init := q) fun (k, v) q => | |
| q.pushKind .attr |>.pushStr v |>.pushStr k | |
| go (acc ++ s!"<{tag}") q | |
| | .text t => | |
| go (acc ++ escapeText t) q | |
| | .raw r => | |
| go (acc ++ r) q | |
| | .seq s => | |
| go acc <| s.foldr (init := q) fun h q => q.pushKind .html |>.pushHtml h | |
| | .attr => | |
| let (k, q) := q.popStr! | |
| let (v, q) := q.popStr! | |
| if v.isEmpty then | |
| go (acc ++ s!" {k}") q | |
| else | |
| go (acc ++ s!" {k}=\"{escapeAttrVal v}\"") q | |
| | .endAttrs => | |
| go (acc.push '>') q | |
| | .endElement => | |
| let (tag, q) := q.popStr! | |
| go (acc ++ s!"</{tag}>") q | |
| | .endVoidElement => | |
| go (acc ++ "/>") q | |
| escapeAttrVal v := v.replace "&" "&" |>.replace "\"" """ | |
| escapeText s := s.replace "&" "&" |>.replace "<" "<" |>.replace ">" ">" | |
| end render_impl | |
| -- TODO: ToFormat and ToString instances with extra newlines/indentation for debug purposes | |
| end Html | |
| end Lean |