Improve htmlgen (#12452)

This commit is contained in:
Juan Carlos
2019-10-18 07:16:01 -03:00
committed by Andreas Rumpf
parent a5ab502f08
commit 0a6e0a3f38
2 changed files with 195 additions and 1 deletions

View File

@@ -24,7 +24,7 @@
## Library changes
- `base64.encode` and `base64.decode` was made faster by about 50%.
- `htmlgen` adds [MathML](https://wikipedia.org/wiki/MathML) support (ISO 40314).
## Language additions

View File

@@ -19,6 +19,14 @@
## generator. Each commonly used HTML tag has a corresponding macro
## that generates a string with its HTML representation.
##
## MathML
## ======
##
## `MathML <https://wikipedia.org/wiki/MathML>`_ is supported, MathML is part of HTML5.
## `MathML <https://wikipedia.org/wiki/MathML>`_ is an Standard ISO/IEC 40314 from year 2015.
## MathML allows you to `draw advanced math on the web <https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math#Examples>`_,
## `visually similar to Latex math. <https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics#Example>`_
##
## Examples
## ========
##
@@ -608,9 +616,195 @@ macro wbr*(e: varargs[untyped]): untyped =
## generates the HTML ``wbr`` element.
result = xmlCheckedTag(e, "wbr", commonAttr, "", true)
macro math*(e: varargs[untyped]): untyped =
## Generates the HTML ``math`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math#Examples
result = xmlCheckedTag(e, "math", "mathbackground mathcolor href overflow" & commonAttr)
macro maction*(e: varargs[untyped]): untyped =
## Generates the HTML ``maction`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/maction
result = xmlCheckedTag(e, "maction", "mathbackground mathcolor href" & commonAttr)
macro menclose*(e: varargs[untyped]): untyped =
## Generates the HTML ``menclose`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/menclose
result = xmlCheckedTag(e, "menclose", "mathbackground mathcolor href notation" & commonAttr)
macro merror*(e: varargs[untyped]): untyped =
## Generates the HTML ``merror`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/merror
result = xmlCheckedTag(e, "merror", "mathbackground mathcolor href" & commonAttr)
macro mfenced*(e: varargs[untyped]): untyped =
## Generates the HTML ``mfenced`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfenced
result = xmlCheckedTag(e, "mfenced", "mathbackground mathcolor href open separators" & commonAttr)
macro mfrac*(e: varargs[untyped]): untyped =
## Generates the HTML ``mfrac`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfrac
result = xmlCheckedTag(e, "mfrac", "mathbackground mathcolor href linethickness numalign" & commonAttr)
macro mglyph*(e: varargs[untyped]): untyped =
## Generates the HTML ``mglyph`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mglyph
result = xmlCheckedTag(e, "mglyph", "mathbackground mathcolor href src valign" & commonAttr)
macro mi*(e: varargs[untyped]): untyped =
## Generates the HTML ``mi`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mi
result = xmlCheckedTag(e, "mi", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
macro mlabeledtr*(e: varargs[untyped]): untyped =
## Generates the HTML ``mlabeledtr`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mlabeledtr
result = xmlCheckedTag(e, "mlabeledtr", "mathbackground mathcolor href columnalign groupalign rowalign" & commonAttr)
macro mmultiscripts*(e: varargs[untyped]): untyped =
## Generates the HTML ``mmultiscripts`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mmultiscripts
result = xmlCheckedTag(e, "mmultiscripts", "mathbackground mathcolor href subscriptshift superscriptshift" & commonAttr)
macro mn*(e: varargs[untyped]): untyped =
## Generates the HTML ``mn`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mn
result = xmlCheckedTag(e, "mn", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
macro mo*(e: varargs[untyped]): untyped =
## Generates the HTML ``mo`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mo
result = xmlCheckedTag(e, "mo",
"mathbackground mathcolor fence form largeop lspace mathsize mathvariant movablelimits rspace separator stretchy symmetric" & commonAttr)
macro mover*(e: varargs[untyped]): untyped =
## Generates the HTML ``mover`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mover
result = xmlCheckedTag(e, "mover", "mathbackground mathcolor accent href" & commonAttr)
macro mpadded*(e: varargs[untyped]): untyped =
## Generates the HTML ``mpadded`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mpadded
result = xmlCheckedTag(e, "mpadded", "mathbackground mathcolor depth href lspace voffset" & commonAttr)
macro mphantom*(e: varargs[untyped]): untyped =
## Generates the HTML ``mphantom`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mphantom
result = xmlCheckedTag(e, "mphantom", "mathbackground" & commonAttr)
macro mroot*(e: varargs[untyped]): untyped =
## Generates the HTML ``mroot`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mroot
result = xmlCheckedTag(e, "mroot", "mathbackground mathcolor href" & commonAttr)
macro mrow*(e: varargs[untyped]): untyped =
## Generates the HTML ``mrow`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mrow
result = xmlCheckedTag(e, "mrow", "mathbackground mathcolor href" & commonAttr)
macro ms*(e: varargs[untyped]): untyped =
## Generates the HTML ``ms`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/ms
result = xmlCheckedTag(e, "ms", "mathbackground mathcolor href lquote mathsize mathvariant rquote" & commonAttr)
macro mspace*(e: varargs[untyped]): untyped =
## Generates the HTML ``mspace`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mspace
result = xmlCheckedTag(e, "mspace", "mathbackground mathcolor href linebreak" & commonAttr)
macro msqrt*(e: varargs[untyped]): untyped =
## Generates the HTML ``msqrt`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msqrt
result = xmlCheckedTag(e, "msqrt", "mathbackground mathcolor href" & commonAttr)
macro mstyle*(e: varargs[untyped]): untyped =
## Generates the HTML ``mstyle`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mstyle
result = xmlCheckedTag(e, "mstyle", ("mathbackground mathcolor href decimalpoint displaystyle " &
"infixlinebreakstyle scriptlevel scriptminsize scriptsizemultiplier" & commonAttr))
macro msub*(e: varargs[untyped]): untyped =
## Generates the HTML ``msub`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msub
result = xmlCheckedTag(e, "msub", "mathbackground mathcolor href subscriptshift" & commonAttr)
macro msubsup*(e: varargs[untyped]): untyped =
## Generates the HTML ``msubsup`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msubsup
result = xmlCheckedTag(e, "msubsup", "mathbackground mathcolor href subscriptshift superscriptshift" & commonAttr)
macro msup*(e: varargs[untyped]): untyped =
## Generates the HTML ``msup`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msup
result = xmlCheckedTag(e, "msup", "mathbackground mathcolor href superscriptshift" & commonAttr)
macro mtable*(e: varargs[untyped]): untyped =
## Generates the HTML ``mtable`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtable
result = xmlCheckedTag(e, "mtable", ("mathbackground mathcolor href align " &
"alignmentscope columnalign columnlines columnspacing columnwidth " &
"displaystyle equalcolumns equalrows frame framespacing groupalign " &
"rowalign rowlines rowspacing side width" & commonAttr))
macro mtd*(e: varargs[untyped]): untyped =
## Generates the HTML ``mtd`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtd
result = xmlCheckedTag(e, "mtd",
"mathbackground mathcolor href columnalign columnspan groupalign rowalign rowspan" & commonAttr)
macro mtext*(e: varargs[untyped]): untyped =
## Generates the HTML ``mtext`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtext
result = xmlCheckedTag(e, "mtext", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
macro munder*(e: varargs[untyped]): untyped =
## Generates the HTML ``munder`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munder
result = xmlCheckedTag(e, "munder", "mathbackground mathcolor href accentunder align" & commonAttr)
macro munderover*(e: varargs[untyped]): untyped =
## Generates the HTML ``munderover`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munderover
result = xmlCheckedTag(e, "munderover", "mathbackground mathcolor href accentunder accent align" & commonAttr)
macro semantics*(e: varargs[untyped]): untyped =
## Generates the HTML ``semantics`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
result = xmlCheckedTag(e, "semantics", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
macro annotation*(e: varargs[untyped]): untyped =
## Generates the HTML ``annotation`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
result = xmlCheckedTag(e, "annotation", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
macro `annotation-xml`*(e: varargs[untyped]): untyped =
## Generates the HTML ``annotation-xml`` element. MathML https://wikipedia.org/wiki/MathML
## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
result = xmlCheckedTag(e, "annotation", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
runnableExamples:
let nim = "Nim"
assert h1(a(href = "https://nim-lang.org", nim)) ==
"""<h1><a href="https://nim-lang.org">Nim</a></h1>"""
assert form(action = "test", `accept-charset` = "Content-Type") ==
"""<form action="test" accept-charset="Content-Type"></form>"""
assert math(
semantics(
mrow(
msup(
mi("x"),
mn("42")
)
)
)
) == "<math><semantics><mrow><msup><mi>x</mi><mn>42</mn></msup></mrow></semantics></math>"
assert math(
semantics(
annotation(encoding = "application/x-tex", title = "Latex on Web", r"x^{2} + y")
)
) == """<math><semantics><annotation encoding="application/x-tex" title="Latex on Web">x^{2} + y</annotation></semantics></math>"""