Skip to content

Commit

Permalink
chore: move some xml function to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
acmepjz authored and hargoniX committed Jul 7, 2024
1 parent b5197b6 commit d1be57c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
18 changes: 0 additions & 18 deletions DocGen4/Output/DocString.lean
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,6 @@ partial def modifyElement (element : Element) : HtmlM Element :=
| _ => pure c
return ⟨ name, attrs, newContents ⟩

-- TODO: remove the following 3 functions
-- once <https://github.com/leanprover/lean4/issues/4411> is fixed

private def _root_.Lean.Xml.Attributes.toStringEscaped (as : Attributes) : String :=
as.fold (fun s n v => s ++ s!" {n}=\"{Html.escape v}\"") ""

mutual

private partial def _root_.Lean.Xml.eToStringEscaped : Element → String
| Element.Element n a c => s!"<{n}{a.toStringEscaped}>{c.map cToStringEscaped |>.foldl (· ++ ·) ""}</{n}>"

private partial def _root_.Lean.Xml.cToStringEscaped : Content → String
| Content.Element e => eToStringEscaped e
| Content.Comment c => s!"<!--{c}-->"
| Content.Character c => Html.escape c

end

/-- Convert docstring to Html. -/
def docStringToHtml (s : String) : HtmlM (Array Html) := do
let rendered := match MD4Lean.renderHtml s with
Expand Down
31 changes: 31 additions & 0 deletions DocGen4/Output/ToHtmlFormat.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Sebastian Ullrich, Henrik Böving
-/
import Lean.Data.Json
import Lean.Data.Xml
import Lean.Parser

/-! This module defines:
Expand Down Expand Up @@ -42,6 +43,36 @@ def escapePairs : Array (String × String) :=
def escape (s : String) : String :=
escapePairs.foldl (fun acc (o, r) => acc.replace o r) s

-- TODO: remove the following 3 functions
-- once <https://github.com/leanprover/lean4/issues/4411> is fixed

def _root_.Lean.Xml.Attributes.toStringEscaped (as : Xml.Attributes) : String :=
as.fold (fun s n v => s ++ s!" {n}=\"{Html.escape v}\"") ""

mutual

partial def _root_.Lean.Xml.eToStringEscaped : Xml.Element → String
| .Element n a c => s!"<{n}{a.toStringEscaped}>{c.map cToStringEscaped |>.foldl (· ++ ·) ""}</{n}>"

partial def _root_.Lean.Xml.cToStringEscaped : Xml.Content → String
| .Element e => eToStringEscaped e
| .Comment c => s!"<!--{c}-->"
| .Character c => Html.escape c

end

mutual

partial def _root_.Lean.Xml.eToPlaintext : Xml.Element → String
| .Element _ _ c => s!"{c.map cToPlaintext |>.foldl (· ++ ·) ""}"

partial def _root_.Lean.Xml.cToPlaintext : Xml.Content → String
| .Element e => eToPlaintext e
| .Comment _ => ""
| .Character c => c

end

def attributesToString (attrs : Array (String × String)) :String :=
attrs.foldl (fun acc (k, v) => acc ++ " " ++ k ++ "=\"" ++ escape v ++ "\"") ""

Expand Down

0 comments on commit d1be57c

Please sign in to comment.