Rstgen/xml tree fixes (#7823)

* Don't prefix lang names with "lang" in rstgen.

* Implements ability to render xmltree w/o \n. Fixes <> for `data-*` attrs.

* Various rstgen fixes.

* Fixes security vulnerabilities due to not escaping some code.
* Adds <video> support and improve `.. image:: ` directive.

* Adds comment as requested.
This commit is contained in:
Dominik Picheta 2018-05-23 15:28:53 +01:00 • committed by Andreas Rumpf
commit 85b7d8fcc4
2 changed files with 50 additions and 39 deletions

View file

@ -9,7 +9,7 @@
## A simple XML tree. More efficient and simpler than the DOM.
import macros, strtabs
import macros, strtabs, strutils
type
XmlNode* = ref XmlNodeObj ## an XML tree consists of ``XmlNode``'s.
@ -217,8 +217,9 @@ proc escape*(s: string): string =
result = newStringOfCap(s.len)
addEscaped(result, s)
proc addIndent(result: var string, indent: int) =
result.add("\n")
proc addIndent(result: var string, indent: int, addNewLines: bool) =
if addNewLines:
result.add("\n")
for i in 1..indent: result.add(' ')
proc noWhitespace(n: XmlNode): bool =
@ -227,7 +228,8 @@ proc noWhitespace(n: XmlNode): bool =
for i in 0..n.len-1:
if n[i].kind in {xnText, xnEntity}: return true
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
addNewLines=true) =
## adds the textual representation of `n` to `result`.
proc addEscapedAttr(result: var string, s: string) =
@ -260,14 +262,15 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
# for mixed leaves, we cannot output whitespace for readability,
# because this would be wrong. For example: ``a<b>b</b>`` is
# different from ``a <b>b</b>``.
for i in 0..n.len-1: result.add(n[i], indent+indWidth, indWidth)
for i in 0..n.len-1:
result.add(n[i], indent+indWidth, indWidth, addNewLines)
else:
for i in 0..n.len-1:
result.addIndent(indent+indWidth)
result.add(n[i], indent+indWidth, indWidth)
result.addIndent(indent)
result.addIndent(indent+indWidth, addNewLines)
result.add(n[i], indent+indWidth, indWidth, addNewLines)
result.addIndent(indent, addNewLines)
else:
result.add(n[0], indent+indWidth, indWidth)
result.add(n[0], indent+indWidth, indWidth, addNewLines)
result.add("</")
result.add(n.fTag)
result.add(">")
@ -316,7 +319,10 @@ proc xmlConstructor(a: NimNode): NimNode {.compileTime.} =
var elements = newNimNode(nnkBracket, a)
for i in 1..a.len-1:
if a[i].kind == nnkExprEqExpr:
attrs.add(toStrLit(a[i][0]))
# In order to support attributes like `data-lang` we have to
# replace whitespace because `toStrLit` gives `data - lang`.
let attrName = toStrLit(a[i][0]).strVal.replace(" ", "")
attrs.add(newStrLitNode(attrName))
attrs.add(a[i][1])
#echo repr(attrs)
else: