htmlgen.nim 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Do yourself a favor and import the module
  10. ## as `from htmlgen import nil` and then fully qualify the macros.
  11. ##
  12. ## *Note*: The Karax project (`nimble install karax`) has a better
  13. ## way to achieve the same, see https://github.com/pragmagic/karax/blob/master/tests/nativehtmlgen.nim
  14. ## for an example.
  15. ##
  16. ##
  17. ## This module implements a simple `XML`:idx: and `HTML`:idx: code
  18. ## generator. Each commonly used HTML tag has a corresponding macro
  19. ## that generates a string with its HTML representation.
  20. ##
  21. ## MathML
  22. ## ======
  23. ##
  24. ## `MathML <https://wikipedia.org/wiki/MathML>`_ is supported, MathML is part of HTML5.
  25. ## `MathML <https://wikipedia.org/wiki/MathML>`_ is an Standard ISO/IEC 40314 from year 2015.
  26. ## MathML allows you to `draw advanced math on the web <https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math#Examples>`_,
  27. ## `visually similar to Latex math. <https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics#Example>`_
  28. ##
  29. ## Examples
  30. ## ========
  31. ##
  32. ## .. code-block:: Nim
  33. ## var nim = "Nim"
  34. ## echo h1(a(href="https://nim-lang.org", nim))
  35. ##
  36. ## Writes the string:
  37. ##
  38. ## <h1><a href="https://nim-lang.org">Nim</a></h1>
  39. ##
  40. import
  41. macros, strutils
  42. const
  43. coreAttr* = " accesskey class contenteditable dir hidden id lang " &
  44. "spellcheck style tabindex title translate " ## HTML DOM Core Attributes
  45. eventAttr* = "onabort onblur oncancel oncanplay oncanplaythrough onchange " &
  46. "onclick oncuechange ondblclick ondurationchange onemptied onended " &
  47. "onerror onfocus oninput oninvalid onkeydown onkeypress onkeyup onload " &
  48. "onloadeddata onloadedmetadata onloadstart onmousedown onmouseenter " &
  49. "onmouseleave onmousemove onmouseout onmouseover onmouseup onmousewheel " &
  50. "onpause onplay onplaying onprogress onratechange onreset onresize " &
  51. "onscroll onseeked onseeking onselect onshow onstalled onsubmit " &
  52. "onsuspend ontimeupdate ontoggle onvolumechange onwaiting " ## HTML DOM Event Attributes
  53. ariaAttr* = " role " ## HTML DOM Aria Attributes
  54. commonAttr* = coreAttr & eventAttr & ariaAttr ## HTML DOM Common Attributes
  55. proc getIdent(e: NimNode): string =
  56. case e.kind
  57. of nnkIdent:
  58. result = e.strVal.normalize
  59. of nnkAccQuoted:
  60. result = getIdent(e[0])
  61. for i in 1 .. e.len-1:
  62. result.add getIdent(e[i])
  63. else: error("cannot extract identifier from node: " & toStrLit(e).strVal, e)
  64. proc delete[T](s: var seq[T], attr: T): bool =
  65. var idx = find(s, attr)
  66. if idx >= 0:
  67. var L = s.len
  68. s[idx] = s[L-1]
  69. setLen(s, L-1)
  70. result = true
  71. proc xmlCheckedTag*(argsList: NimNode, tag: string, optAttr = "", reqAttr = "",
  72. isLeaf = false): NimNode =
  73. ## use this procedure to define a new XML tag
  74. # copy the attributes; when iterating over them these lists
  75. # will be modified, so that each attribute is only given one value
  76. var req = splitWhitespace(reqAttr)
  77. var opt = splitWhitespace(optAttr)
  78. result = newNimNode(nnkBracket)
  79. result.add(newStrLitNode("<"))
  80. result.add(newStrLitNode(tag))
  81. # first pass over attributes:
  82. for i in 0 ..< argsList.len:
  83. if argsList[i].kind == nnkExprEqExpr:
  84. var name = getIdent(argsList[i][0])
  85. if name.startsWith("data-") or delete(req, name) or delete(opt, name):
  86. result.add(newStrLitNode(" "))
  87. result.add(newStrLitNode(name))
  88. result.add(newStrLitNode("=\""))
  89. result.add(argsList[i][1])
  90. result.add(newStrLitNode("\""))
  91. else:
  92. error("invalid attribute for '" & tag & "' element: " & name, argsList[i])
  93. # check each required attribute exists:
  94. if req.len > 0:
  95. error(req[0] & " attribute for '" & tag & "' element expected", argsList)
  96. if isLeaf:
  97. for i in 0 ..< argsList.len:
  98. if argsList[i].kind != nnkExprEqExpr:
  99. error("element " & tag & " cannot be nested", argsList[i])
  100. result.add(newStrLitNode(" />"))
  101. else:
  102. result.add(newStrLitNode(">"))
  103. # second pass over elements:
  104. for i in 0 ..< argsList.len:
  105. if argsList[i].kind != nnkExprEqExpr: result.add(argsList[i])
  106. result.add(newStrLitNode("</"))
  107. result.add(newStrLitNode(tag))
  108. result.add(newStrLitNode(">"))
  109. result = nestList(ident"&", result)
  110. macro a*(e: varargs[untyped]): untyped =
  111. ## Generates the HTML `a` element.
  112. result = xmlCheckedTag(e, "a", "href target download rel hreflang type " &
  113. commonAttr)
  114. macro abbr*(e: varargs[untyped]): untyped =
  115. ## Generates the HTML `abbr` element.
  116. result = xmlCheckedTag(e, "abbr", commonAttr)
  117. macro address*(e: varargs[untyped]): untyped =
  118. ## Generates the HTML `address` element.
  119. result = xmlCheckedTag(e, "address", commonAttr)
  120. macro area*(e: varargs[untyped]): untyped =
  121. ## Generates the HTML `area` element.
  122. result = xmlCheckedTag(e, "area", "coords download href hreflang rel " &
  123. "shape target type" & commonAttr, "alt", true)
  124. macro article*(e: varargs[untyped]): untyped =
  125. ## Generates the HTML `article` element.
  126. result = xmlCheckedTag(e, "article", commonAttr)
  127. macro aside*(e: varargs[untyped]): untyped =
  128. ## Generates the HTML `aside` element.
  129. result = xmlCheckedTag(e, "aside", commonAttr)
  130. macro audio*(e: varargs[untyped]): untyped =
  131. ## Generates the HTML `audio` element.
  132. result = xmlCheckedTag(e, "audio", "src crossorigin preload " &
  133. "autoplay mediagroup loop muted controls" & commonAttr)
  134. macro b*(e: varargs[untyped]): untyped =
  135. ## Generates the HTML `b` element.
  136. result = xmlCheckedTag(e, "b", commonAttr)
  137. macro base*(e: varargs[untyped]): untyped =
  138. ## Generates the HTML `base` element.
  139. result = xmlCheckedTag(e, "base", "href target" & commonAttr, "", true)
  140. macro bdi*(e: varargs[untyped]): untyped =
  141. ## Generates the HTML `bdi` element.
  142. result = xmlCheckedTag(e, "bdi", commonAttr)
  143. macro bdo*(e: varargs[untyped]): untyped =
  144. ## Generates the HTML `bdo` element.
  145. result = xmlCheckedTag(e, "bdo", commonAttr)
  146. macro big*(e: varargs[untyped]): untyped =
  147. ## Generates the HTML `big` element.
  148. result = xmlCheckedTag(e, "big", commonAttr)
  149. macro blockquote*(e: varargs[untyped]): untyped =
  150. ## Generates the HTML `blockquote` element.
  151. result = xmlCheckedTag(e, "blockquote", " cite" & commonAttr)
  152. macro body*(e: varargs[untyped]): untyped =
  153. ## Generates the HTML `body` element.
  154. result = xmlCheckedTag(e, "body", "onafterprint onbeforeprint " &
  155. "onbeforeunload onhashchange onmessage onoffline ononline onpagehide " &
  156. "onpageshow onpopstate onstorage onunload" & commonAttr)
  157. macro br*(e: varargs[untyped]): untyped =
  158. ## Generates the HTML `br` element.
  159. result = xmlCheckedTag(e, "br", commonAttr, "", true)
  160. macro button*(e: varargs[untyped]): untyped =
  161. ## Generates the HTML `button` element.
  162. result = xmlCheckedTag(e, "button", "autofocus disabled form formaction " &
  163. "formenctype formmethod formnovalidate formtarget menu name type value" &
  164. commonAttr)
  165. macro canvas*(e: varargs[untyped]): untyped =
  166. ## Generates the HTML `canvas` element.
  167. result = xmlCheckedTag(e, "canvas", "width height" & commonAttr)
  168. macro caption*(e: varargs[untyped]): untyped =
  169. ## Generates the HTML `caption` element.
  170. result = xmlCheckedTag(e, "caption", commonAttr)
  171. macro center*(e: varargs[untyped]): untyped =
  172. ## Generates the HTML `center` element.
  173. result = xmlCheckedTag(e, "center", commonAttr)
  174. macro cite*(e: varargs[untyped]): untyped =
  175. ## Generates the HTML `cite` element.
  176. result = xmlCheckedTag(e, "cite", commonAttr)
  177. macro code*(e: varargs[untyped]): untyped =
  178. ## Generates the HTML `code` element.
  179. result = xmlCheckedTag(e, "code", commonAttr)
  180. macro col*(e: varargs[untyped]): untyped =
  181. ## Generates the HTML `col` element.
  182. result = xmlCheckedTag(e, "col", "span" & commonAttr, "", true)
  183. macro colgroup*(e: varargs[untyped]): untyped =
  184. ## Generates the HTML `colgroup` element.
  185. result = xmlCheckedTag(e, "colgroup", "span" & commonAttr)
  186. macro data*(e: varargs[untyped]): untyped =
  187. ## Generates the HTML `data` element.
  188. result = xmlCheckedTag(e, "data", "value" & commonAttr)
  189. macro datalist*(e: varargs[untyped]): untyped =
  190. ## Generates the HTML `datalist` element.
  191. result = xmlCheckedTag(e, "datalist", commonAttr)
  192. macro dd*(e: varargs[untyped]): untyped =
  193. ## Generates the HTML `dd` element.
  194. result = xmlCheckedTag(e, "dd", commonAttr)
  195. macro del*(e: varargs[untyped]): untyped =
  196. ## Generates the HTML `del` element.
  197. result = xmlCheckedTag(e, "del", "cite datetime" & commonAttr)
  198. macro details*(e: varargs[untyped]): untyped =
  199. ## Generates the HTML `details` element.
  200. result = xmlCheckedTag(e, "details", commonAttr & "open")
  201. macro dfn*(e: varargs[untyped]): untyped =
  202. ## Generates the HTML `dfn` element.
  203. result = xmlCheckedTag(e, "dfn", commonAttr)
  204. macro dialog*(e: varargs[untyped]): untyped =
  205. ## Generates the HTML `dialog` element.
  206. result = xmlCheckedTag(e, "dialog", commonAttr & "open")
  207. macro `div`*(e: varargs[untyped]): untyped =
  208. ## Generates the HTML `div` element.
  209. result = xmlCheckedTag(e, "div", commonAttr)
  210. macro dl*(e: varargs[untyped]): untyped =
  211. ## Generates the HTML `dl` element.
  212. result = xmlCheckedTag(e, "dl", commonAttr)
  213. macro dt*(e: varargs[untyped]): untyped =
  214. ## Generates the HTML `dt` element.
  215. result = xmlCheckedTag(e, "dt", commonAttr)
  216. macro em*(e: varargs[untyped]): untyped =
  217. ## Generates the HTML `em` element.
  218. result = xmlCheckedTag(e, "em", commonAttr)
  219. macro embed*(e: varargs[untyped]): untyped =
  220. ## Generates the HTML `embed` element.
  221. result = xmlCheckedTag(e, "embed", "src type height width" &
  222. commonAttr, "", true)
  223. macro fieldset*(e: varargs[untyped]): untyped =
  224. ## Generates the HTML `fieldset` element.
  225. result = xmlCheckedTag(e, "fieldset", "disabled form name" & commonAttr)
  226. macro figure*(e: varargs[untyped]): untyped =
  227. ## Generates the HTML `figure` element.
  228. result = xmlCheckedTag(e, "figure", commonAttr)
  229. macro figcaption*(e: varargs[untyped]): untyped =
  230. ## Generates the HTML `figcaption` element.
  231. result = xmlCheckedTag(e, "figcaption", commonAttr)
  232. macro footer*(e: varargs[untyped]): untyped =
  233. ## Generates the HTML `footer` element.
  234. result = xmlCheckedTag(e, "footer", commonAttr)
  235. macro form*(e: varargs[untyped]): untyped =
  236. ## Generates the HTML `form` element.
  237. result = xmlCheckedTag(e, "form", "accept-charset action autocomplete " &
  238. "enctype method name novalidate target" & commonAttr)
  239. macro h1*(e: varargs[untyped]): untyped =
  240. ## Generates the HTML `h1` element.
  241. result = xmlCheckedTag(e, "h1", commonAttr)
  242. macro h2*(e: varargs[untyped]): untyped =
  243. ## Generates the HTML `h2` element.
  244. result = xmlCheckedTag(e, "h2", commonAttr)
  245. macro h3*(e: varargs[untyped]): untyped =
  246. ## Generates the HTML `h3` element.
  247. result = xmlCheckedTag(e, "h3", commonAttr)
  248. macro h4*(e: varargs[untyped]): untyped =
  249. ## Generates the HTML `h4` element.
  250. result = xmlCheckedTag(e, "h4", commonAttr)
  251. macro h5*(e: varargs[untyped]): untyped =
  252. ## Generates the HTML `h5` element.
  253. result = xmlCheckedTag(e, "h5", commonAttr)
  254. macro h6*(e: varargs[untyped]): untyped =
  255. ## Generates the HTML `h6` element.
  256. result = xmlCheckedTag(e, "h6", commonAttr)
  257. macro head*(e: varargs[untyped]): untyped =
  258. ## Generates the HTML `head` element.
  259. result = xmlCheckedTag(e, "head", commonAttr)
  260. macro header*(e: varargs[untyped]): untyped =
  261. ## Generates the HTML `header` element.
  262. result = xmlCheckedTag(e, "header", commonAttr)
  263. macro html*(e: varargs[untyped]): untyped =
  264. ## Generates the HTML `html` element.
  265. result = xmlCheckedTag(e, "html", "xmlns" & commonAttr, "")
  266. macro hr*(): untyped =
  267. ## Generates the HTML `hr` element.
  268. result = xmlCheckedTag(newNimNode(nnkArgList), "hr", commonAttr, "", true)
  269. macro i*(e: varargs[untyped]): untyped =
  270. ## Generates the HTML `i` element.
  271. result = xmlCheckedTag(e, "i", commonAttr)
  272. macro iframe*(e: varargs[untyped]): untyped =
  273. ## Generates the HTML `iframe` element.
  274. result = xmlCheckedTag(e, "iframe", "src srcdoc name sandbox width height loading" &
  275. commonAttr)
  276. macro img*(e: varargs[untyped]): untyped =
  277. ## Generates the HTML `img` element.
  278. result = xmlCheckedTag(e, "img", "crossorigin usemap ismap height width loading" &
  279. commonAttr, "src alt", true)
  280. macro input*(e: varargs[untyped]): untyped =
  281. ## Generates the HTML `input` element.
  282. result = xmlCheckedTag(e, "input", "accept alt autocomplete autofocus " &
  283. "checked dirname disabled form formaction formenctype formmethod " &
  284. "formnovalidate formtarget height inputmode list max maxlength min " &
  285. "minlength multiple name pattern placeholder readonly required size " &
  286. "src step type value width" & commonAttr, "", true)
  287. macro ins*(e: varargs[untyped]): untyped =
  288. ## Generates the HTML `ins` element.
  289. result = xmlCheckedTag(e, "ins", "cite datetime" & commonAttr)
  290. macro kbd*(e: varargs[untyped]): untyped =
  291. ## Generates the HTML `kbd` element.
  292. result = xmlCheckedTag(e, "kbd", commonAttr)
  293. macro keygen*(e: varargs[untyped]): untyped =
  294. ## Generates the HTML `keygen` element.
  295. result = xmlCheckedTag(e, "keygen", "autofocus challenge disabled " &
  296. "form keytype name" & commonAttr)
  297. macro label*(e: varargs[untyped]): untyped =
  298. ## Generates the HTML `label` element.
  299. result = xmlCheckedTag(e, "label", "form for" & commonAttr)
  300. macro legend*(e: varargs[untyped]): untyped =
  301. ## Generates the HTML `legend` element.
  302. result = xmlCheckedTag(e, "legend", commonAttr)
  303. macro li*(e: varargs[untyped]): untyped =
  304. ## Generates the HTML `li` element.
  305. result = xmlCheckedTag(e, "li", "value" & commonAttr)
  306. macro link*(e: varargs[untyped]): untyped =
  307. ## Generates the HTML `link` element.
  308. result = xmlCheckedTag(e, "link", "href crossorigin rel media hreflang " &
  309. "type sizes" & commonAttr, "", true)
  310. macro main*(e: varargs[untyped]): untyped =
  311. ## Generates the HTML `main` element.
  312. result = xmlCheckedTag(e, "main", commonAttr)
  313. macro map*(e: varargs[untyped]): untyped =
  314. ## Generates the HTML `map` element.
  315. result = xmlCheckedTag(e, "map", "name" & commonAttr)
  316. macro mark*(e: varargs[untyped]): untyped =
  317. ## Generates the HTML `mark` element.
  318. result = xmlCheckedTag(e, "mark", commonAttr)
  319. macro marquee*(e: varargs[untyped]): untyped =
  320. ## Generates the HTML `marquee` element.
  321. result = xmlCheckedTag(e, "marquee", coreAttr &
  322. "behavior bgcolor direction height hspace loop scrollamount " &
  323. "scrolldelay truespeed vspace width onbounce onfinish onstart")
  324. macro meta*(e: varargs[untyped]): untyped =
  325. ## Generates the HTML `meta` element.
  326. result = xmlCheckedTag(e, "meta", "name http-equiv content charset" &
  327. commonAttr, "", true)
  328. macro meter*(e: varargs[untyped]): untyped =
  329. ## Generates the HTML `meter` element.
  330. result = xmlCheckedTag(e, "meter", "value min max low high optimum" &
  331. commonAttr)
  332. macro nav*(e: varargs[untyped]): untyped =
  333. ## Generates the HTML `nav` element.
  334. result = xmlCheckedTag(e, "nav", commonAttr)
  335. macro noscript*(e: varargs[untyped]): untyped =
  336. ## Generates the HTML `noscript` element.
  337. result = xmlCheckedTag(e, "noscript", commonAttr)
  338. macro `object`*(e: varargs[untyped]): untyped =
  339. ## Generates the HTML `object` element.
  340. result = xmlCheckedTag(e, "object", "data type typemustmatch name usemap " &
  341. "form width height" & commonAttr)
  342. macro ol*(e: varargs[untyped]): untyped =
  343. ## Generates the HTML `ol` element.
  344. result = xmlCheckedTag(e, "ol", "reversed start type" & commonAttr)
  345. macro optgroup*(e: varargs[untyped]): untyped =
  346. ## Generates the HTML `optgroup` element.
  347. result = xmlCheckedTag(e, "optgroup", "disabled" & commonAttr, "label", false)
  348. macro option*(e: varargs[untyped]): untyped =
  349. ## Generates the HTML `option` element.
  350. result = xmlCheckedTag(e, "option", "disabled label selected value" &
  351. commonAttr)
  352. macro output*(e: varargs[untyped]): untyped =
  353. ## Generates the HTML `output` element.
  354. result = xmlCheckedTag(e, "output", "for form name" & commonAttr)
  355. macro p*(e: varargs[untyped]): untyped =
  356. ## Generates the HTML `p` element.
  357. result = xmlCheckedTag(e, "p", commonAttr)
  358. macro param*(e: varargs[untyped]): untyped =
  359. ## Generates the HTML `param` element.
  360. result = xmlCheckedTag(e, "param", commonAttr, "name value", true)
  361. macro picture*(e: varargs[untyped]): untyped =
  362. ## Generates the HTML `picture` element.
  363. result = xmlCheckedTag(e, "picture", commonAttr)
  364. macro pre*(e: varargs[untyped]): untyped =
  365. ## Generates the HTML `pre` element.
  366. result = xmlCheckedTag(e, "pre", commonAttr)
  367. macro progress*(e: varargs[untyped]): untyped =
  368. ## Generates the HTML `progress` element.
  369. result = xmlCheckedTag(e, "progress", "value max" & commonAttr)
  370. macro q*(e: varargs[untyped]): untyped =
  371. ## Generates the HTML `q` element.
  372. result = xmlCheckedTag(e, "q", "cite" & commonAttr)
  373. macro rb*(e: varargs[untyped]): untyped =
  374. ## Generates the HTML `rb` element.
  375. result = xmlCheckedTag(e, "rb", commonAttr)
  376. macro rp*(e: varargs[untyped]): untyped =
  377. ## Generates the HTML `rp` element.
  378. result = xmlCheckedTag(e, "rp", commonAttr)
  379. macro rt*(e: varargs[untyped]): untyped =
  380. ## Generates the HTML `rt` element.
  381. result = xmlCheckedTag(e, "rt", commonAttr)
  382. macro rtc*(e: varargs[untyped]): untyped =
  383. ## Generates the HTML `rtc` element.
  384. result = xmlCheckedTag(e, "rtc", commonAttr)
  385. macro ruby*(e: varargs[untyped]): untyped =
  386. ## Generates the HTML `ruby` element.
  387. result = xmlCheckedTag(e, "ruby", commonAttr)
  388. macro s*(e: varargs[untyped]): untyped =
  389. ## Generates the HTML `s` element.
  390. result = xmlCheckedTag(e, "s", commonAttr)
  391. macro samp*(e: varargs[untyped]): untyped =
  392. ## Generates the HTML `samp` element.
  393. result = xmlCheckedTag(e, "samp", commonAttr)
  394. macro script*(e: varargs[untyped]): untyped =
  395. ## Generates the HTML `script` element.
  396. result = xmlCheckedTag(e, "script", "src type charset async defer " &
  397. "crossorigin" & commonAttr)
  398. macro section*(e: varargs[untyped]): untyped =
  399. ## Generates the HTML `section` element.
  400. result = xmlCheckedTag(e, "section", commonAttr)
  401. macro select*(e: varargs[untyped]): untyped =
  402. ## Generates the HTML `select` element.
  403. result = xmlCheckedTag(e, "select", "autofocus disabled form multiple " &
  404. "name required size" & commonAttr)
  405. macro slot*(e: varargs[untyped]): untyped =
  406. ## Generates the HTML `slot` element.
  407. result = xmlCheckedTag(e, "slot", commonAttr)
  408. macro small*(e: varargs[untyped]): untyped =
  409. ## Generates the HTML `small` element.
  410. result = xmlCheckedTag(e, "small", commonAttr)
  411. macro source*(e: varargs[untyped]): untyped =
  412. ## Generates the HTML `source` element.
  413. result = xmlCheckedTag(e, "source", "type" & commonAttr, "src", true)
  414. macro span*(e: varargs[untyped]): untyped =
  415. ## Generates the HTML `span` element.
  416. result = xmlCheckedTag(e, "span", commonAttr)
  417. macro strong*(e: varargs[untyped]): untyped =
  418. ## Generates the HTML `strong` element.
  419. result = xmlCheckedTag(e, "strong", commonAttr)
  420. macro style*(e: varargs[untyped]): untyped =
  421. ## Generates the HTML `style` element.
  422. result = xmlCheckedTag(e, "style", "media type" & commonAttr)
  423. macro sub*(e: varargs[untyped]): untyped =
  424. ## Generates the HTML `sub` element.
  425. result = xmlCheckedTag(e, "sub", commonAttr)
  426. macro summary*(e: varargs[untyped]): untyped =
  427. ## Generates the HTML `summary` element.
  428. result = xmlCheckedTag(e, "summary", commonAttr)
  429. macro sup*(e: varargs[untyped]): untyped =
  430. ## Generates the HTML `sup` element.
  431. result = xmlCheckedTag(e, "sup", commonAttr)
  432. macro table*(e: varargs[untyped]): untyped =
  433. ## Generates the HTML `table` element.
  434. result = xmlCheckedTag(e, "table", "border sortable" & commonAttr)
  435. macro tbody*(e: varargs[untyped]): untyped =
  436. ## Generates the HTML `tbody` element.
  437. result = xmlCheckedTag(e, "tbody", commonAttr)
  438. macro td*(e: varargs[untyped]): untyped =
  439. ## Generates the HTML `td` element.
  440. result = xmlCheckedTag(e, "td", "colspan rowspan headers" & commonAttr)
  441. macro `template`*(e: varargs[untyped]): untyped =
  442. ## Generates the HTML `template` element.
  443. result = xmlCheckedTag(e, "template", commonAttr)
  444. macro textarea*(e: varargs[untyped]): untyped =
  445. ## Generates the HTML `textarea` element.
  446. result = xmlCheckedTag(e, "textarea", "autocomplete autofocus cols " &
  447. "dirname disabled form inputmode maxlength minlength name placeholder " &
  448. "readonly required rows wrap" & commonAttr)
  449. macro tfoot*(e: varargs[untyped]): untyped =
  450. ## Generates the HTML `tfoot` element.
  451. result = xmlCheckedTag(e, "tfoot", commonAttr)
  452. macro th*(e: varargs[untyped]): untyped =
  453. ## Generates the HTML `th` element.
  454. result = xmlCheckedTag(e, "th", "colspan rowspan headers abbr scope axis" &
  455. " sorted" & commonAttr)
  456. macro thead*(e: varargs[untyped]): untyped =
  457. ## Generates the HTML `thead` element.
  458. result = xmlCheckedTag(e, "thead", commonAttr)
  459. macro time*(e: varargs[untyped]): untyped =
  460. ## Generates the HTML `time` element.
  461. result = xmlCheckedTag(e, "time", "datetime" & commonAttr)
  462. macro title*(e: varargs[untyped]): untyped =
  463. ## Generates the HTML `title` element.
  464. result = xmlCheckedTag(e, "title", commonAttr)
  465. macro tr*(e: varargs[untyped]): untyped =
  466. ## Generates the HTML `tr` element.
  467. result = xmlCheckedTag(e, "tr", commonAttr)
  468. macro track*(e: varargs[untyped]): untyped =
  469. ## Generates the HTML `track` element.
  470. result = xmlCheckedTag(e, "track", "kind srclang label default" &
  471. commonAttr, "src", true)
  472. macro tt*(e: varargs[untyped]): untyped =
  473. ## Generates the HTML `tt` element.
  474. result = xmlCheckedTag(e, "tt", commonAttr)
  475. macro u*(e: varargs[untyped]): untyped =
  476. ## Generates the HTML `u` element.
  477. result = xmlCheckedTag(e, "u", commonAttr)
  478. macro ul*(e: varargs[untyped]): untyped =
  479. ## Generates the HTML `ul` element.
  480. result = xmlCheckedTag(e, "ul", commonAttr)
  481. macro `var`*(e: varargs[untyped]): untyped =
  482. ## Generates the HTML `var` element.
  483. result = xmlCheckedTag(e, "var", commonAttr)
  484. macro video*(e: varargs[untyped]): untyped =
  485. ## Generates the HTML `video` element.
  486. result = xmlCheckedTag(e, "video", "src crossorigin poster preload " &
  487. "autoplay mediagroup loop muted controls width height" & commonAttr)
  488. macro wbr*(e: varargs[untyped]): untyped =
  489. ## Generates the HTML `wbr` element.
  490. result = xmlCheckedTag(e, "wbr", commonAttr, "", true)
  491. macro portal*(e: varargs[untyped]): untyped =
  492. ## Generates the HTML `portal` element.
  493. result = xmlCheckedTag(e, "portal", "width height type src disabled" & commonAttr, "", false)
  494. macro math*(e: varargs[untyped]): untyped =
  495. ## Generates the HTML `math` element. MathML https://wikipedia.org/wiki/MathML
  496. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math#Examples
  497. result = xmlCheckedTag(e, "math", "mathbackground mathcolor href overflow" & commonAttr)
  498. macro maction*(e: varargs[untyped]): untyped =
  499. ## Generates the HTML `maction` element. MathML https://wikipedia.org/wiki/MathML
  500. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/maction
  501. result = xmlCheckedTag(e, "maction", "mathbackground mathcolor href" & commonAttr)
  502. macro menclose*(e: varargs[untyped]): untyped =
  503. ## Generates the HTML `menclose` element. MathML https://wikipedia.org/wiki/MathML
  504. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/menclose
  505. result = xmlCheckedTag(e, "menclose", "mathbackground mathcolor href notation" & commonAttr)
  506. macro merror*(e: varargs[untyped]): untyped =
  507. ## Generates the HTML `merror` element. MathML https://wikipedia.org/wiki/MathML
  508. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/merror
  509. result = xmlCheckedTag(e, "merror", "mathbackground mathcolor href" & commonAttr)
  510. macro mfenced*(e: varargs[untyped]): untyped =
  511. ## Generates the HTML `mfenced` element. MathML https://wikipedia.org/wiki/MathML
  512. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfenced
  513. result = xmlCheckedTag(e, "mfenced", "mathbackground mathcolor href open separators" & commonAttr)
  514. macro mfrac*(e: varargs[untyped]): untyped =
  515. ## Generates the HTML `mfrac` element. MathML https://wikipedia.org/wiki/MathML
  516. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mfrac
  517. result = xmlCheckedTag(e, "mfrac", "mathbackground mathcolor href linethickness numalign" & commonAttr)
  518. macro mglyph*(e: varargs[untyped]): untyped =
  519. ## Generates the HTML `mglyph` element. MathML https://wikipedia.org/wiki/MathML
  520. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mglyph
  521. result = xmlCheckedTag(e, "mglyph", "mathbackground mathcolor href src valign" & commonAttr)
  522. macro mi*(e: varargs[untyped]): untyped =
  523. ## Generates the HTML `mi` element. MathML https://wikipedia.org/wiki/MathML
  524. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mi
  525. result = xmlCheckedTag(e, "mi", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
  526. macro mlabeledtr*(e: varargs[untyped]): untyped =
  527. ## Generates the HTML `mlabeledtr` element. MathML https://wikipedia.org/wiki/MathML
  528. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mlabeledtr
  529. result = xmlCheckedTag(e, "mlabeledtr", "mathbackground mathcolor href columnalign groupalign rowalign" & commonAttr)
  530. macro mmultiscripts*(e: varargs[untyped]): untyped =
  531. ## Generates the HTML `mmultiscripts` element. MathML https://wikipedia.org/wiki/MathML
  532. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mmultiscripts
  533. result = xmlCheckedTag(e, "mmultiscripts", "mathbackground mathcolor href subscriptshift superscriptshift" & commonAttr)
  534. macro mn*(e: varargs[untyped]): untyped =
  535. ## Generates the HTML `mn` element. MathML https://wikipedia.org/wiki/MathML
  536. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mn
  537. result = xmlCheckedTag(e, "mn", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
  538. macro mo*(e: varargs[untyped]): untyped =
  539. ## Generates the HTML `mo` element. MathML https://wikipedia.org/wiki/MathML
  540. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mo
  541. result = xmlCheckedTag(e, "mo",
  542. "mathbackground mathcolor fence form largeop lspace mathsize mathvariant movablelimits rspace separator stretchy symmetric" & commonAttr)
  543. macro mover*(e: varargs[untyped]): untyped =
  544. ## Generates the HTML `mover` element. MathML https://wikipedia.org/wiki/MathML
  545. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mover
  546. result = xmlCheckedTag(e, "mover", "mathbackground mathcolor accent href" & commonAttr)
  547. macro mpadded*(e: varargs[untyped]): untyped =
  548. ## Generates the HTML `mpadded` element. MathML https://wikipedia.org/wiki/MathML
  549. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mpadded
  550. result = xmlCheckedTag(e, "mpadded", "mathbackground mathcolor depth href lspace voffset" & commonAttr)
  551. macro mphantom*(e: varargs[untyped]): untyped =
  552. ## Generates the HTML `mphantom` element. MathML https://wikipedia.org/wiki/MathML
  553. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mphantom
  554. result = xmlCheckedTag(e, "mphantom", "mathbackground" & commonAttr)
  555. macro mroot*(e: varargs[untyped]): untyped =
  556. ## Generates the HTML `mroot` element. MathML https://wikipedia.org/wiki/MathML
  557. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mroot
  558. result = xmlCheckedTag(e, "mroot", "mathbackground mathcolor href" & commonAttr)
  559. macro mrow*(e: varargs[untyped]): untyped =
  560. ## Generates the HTML `mrow` element. MathML https://wikipedia.org/wiki/MathML
  561. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mrow
  562. result = xmlCheckedTag(e, "mrow", "mathbackground mathcolor href" & commonAttr)
  563. macro ms*(e: varargs[untyped]): untyped =
  564. ## Generates the HTML `ms` element. MathML https://wikipedia.org/wiki/MathML
  565. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/ms
  566. result = xmlCheckedTag(e, "ms", "mathbackground mathcolor href lquote mathsize mathvariant rquote" & commonAttr)
  567. macro mspace*(e: varargs[untyped]): untyped =
  568. ## Generates the HTML `mspace` element. MathML https://wikipedia.org/wiki/MathML
  569. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mspace
  570. result = xmlCheckedTag(e, "mspace", "mathbackground mathcolor href linebreak" & commonAttr)
  571. macro msqrt*(e: varargs[untyped]): untyped =
  572. ## Generates the HTML `msqrt` element. MathML https://wikipedia.org/wiki/MathML
  573. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msqrt
  574. result = xmlCheckedTag(e, "msqrt", "mathbackground mathcolor href" & commonAttr)
  575. macro mstyle*(e: varargs[untyped]): untyped =
  576. ## Generates the HTML `mstyle` element. MathML https://wikipedia.org/wiki/MathML
  577. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mstyle
  578. result = xmlCheckedTag(e, "mstyle", ("mathbackground mathcolor href decimalpoint displaystyle " &
  579. "infixlinebreakstyle scriptlevel scriptminsize scriptsizemultiplier" & commonAttr))
  580. macro msub*(e: varargs[untyped]): untyped =
  581. ## Generates the HTML `msub` element. MathML https://wikipedia.org/wiki/MathML
  582. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msub
  583. result = xmlCheckedTag(e, "msub", "mathbackground mathcolor href subscriptshift" & commonAttr)
  584. macro msubsup*(e: varargs[untyped]): untyped =
  585. ## Generates the HTML `msubsup` element. MathML https://wikipedia.org/wiki/MathML
  586. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msubsup
  587. result = xmlCheckedTag(e, "msubsup", "mathbackground mathcolor href subscriptshift superscriptshift" & commonAttr)
  588. macro msup*(e: varargs[untyped]): untyped =
  589. ## Generates the HTML `msup` element. MathML https://wikipedia.org/wiki/MathML
  590. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/msup
  591. result = xmlCheckedTag(e, "msup", "mathbackground mathcolor href superscriptshift" & commonAttr)
  592. macro mtable*(e: varargs[untyped]): untyped =
  593. ## Generates the HTML `mtable` element. MathML https://wikipedia.org/wiki/MathML
  594. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtable
  595. result = xmlCheckedTag(e, "mtable", ("mathbackground mathcolor href align " &
  596. "alignmentscope columnalign columnlines columnspacing columnwidth " &
  597. "displaystyle equalcolumns equalrows frame framespacing groupalign " &
  598. "rowalign rowlines rowspacing side width" & commonAttr))
  599. macro mtd*(e: varargs[untyped]): untyped =
  600. ## Generates the HTML `mtd` element. MathML https://wikipedia.org/wiki/MathML
  601. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtd
  602. result = xmlCheckedTag(e, "mtd",
  603. "mathbackground mathcolor href columnalign columnspan groupalign rowalign rowspan" & commonAttr)
  604. macro mtext*(e: varargs[untyped]): untyped =
  605. ## Generates the HTML `mtext` element. MathML https://wikipedia.org/wiki/MathML
  606. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/mtext
  607. result = xmlCheckedTag(e, "mtext", "mathbackground mathcolor href mathsize mathvariant" & commonAttr)
  608. macro munder*(e: varargs[untyped]): untyped =
  609. ## Generates the HTML `munder` element. MathML https://wikipedia.org/wiki/MathML
  610. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munder
  611. result = xmlCheckedTag(e, "munder", "mathbackground mathcolor href accentunder align" & commonAttr)
  612. macro munderover*(e: varargs[untyped]): untyped =
  613. ## Generates the HTML `munderover` element. MathML https://wikipedia.org/wiki/MathML
  614. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/munderover
  615. result = xmlCheckedTag(e, "munderover", "mathbackground mathcolor href accentunder accent align" & commonAttr)
  616. macro semantics*(e: varargs[untyped]): untyped =
  617. ## Generates the HTML `semantics` element. MathML https://wikipedia.org/wiki/MathML
  618. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
  619. result = xmlCheckedTag(e, "semantics", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
  620. macro annotation*(e: varargs[untyped]): untyped =
  621. ## Generates the HTML `annotation` element. MathML https://wikipedia.org/wiki/MathML
  622. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
  623. result = xmlCheckedTag(e, "annotation", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
  624. macro `annotation-xml`*(e: varargs[untyped]): untyped =
  625. ## Generates the HTML `annotation-xml` element. MathML https://wikipedia.org/wiki/MathML
  626. ## https://developer.mozilla.org/en-US/docs/Web/MathML/Element/semantics
  627. result = xmlCheckedTag(e, "annotation", "mathbackground mathcolor href definitionURL encoding cd src" & commonAttr)
  628. runnableExamples:
  629. let nim = "Nim"
  630. assert h1(a(href = "https://nim-lang.org", nim)) ==
  631. """<h1><a href="https://nim-lang.org">Nim</a></h1>"""
  632. assert form(action = "test", `accept-charset` = "Content-Type") ==
  633. """<form action="test" accept-charset="Content-Type"></form>"""
  634. assert math(
  635. semantics(
  636. mrow(
  637. msup(
  638. mi("x"),
  639. mn("42")
  640. )
  641. )
  642. )
  643. ) == "<math><semantics><mrow><msup><mi>x</mi><mn>42</mn></msup></mrow></semantics></math>"
  644. assert math(
  645. semantics(
  646. annotation(encoding = "application/x-tex", title = "Latex on Web", r"x^{2} + y")
  647. )
  648. ) == """<math><semantics><annotation encoding="application/x-tex" title="Latex on Web">x^{2} + y</annotation></semantics></math>"""