diff options
author | Andreas Rumpf <andreas@andreas-laptop> | 2010-03-14 01:25:25 +0100 |
---|---|---|
committer | Andreas Rumpf <andreas@andreas-laptop> | 2010-03-14 01:25:25 +0100 |
commit | 7bf98411b6d4dfd203371b5d2ec0a5ef3a2305df (patch) | |
tree | 23f76f650800ca9508e17869cc83fc8061d1f310 /lib/pure/xmltree.nim | |
parent | 3dafd6856bfd644e9cf518b36e5bfedc3220a702 (diff) | |
download | Nim-7bf98411b6d4dfd203371b5d2ec0a5ef3a2305df.tar.gz |
version 0.8.8
Diffstat (limited to 'lib/pure/xmltree.nim')
-rwxr-xr-x | lib/pure/xmltree.nim | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index c79b9ad40..8604078fb 100755 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -12,9 +12,9 @@ import macros, strtabs type - PXmlNode* = ref TXmlNode ## an XML tree consists of ``PXmlNode``s. + PXmlNode* = ref TXmlNode ## an XML tree consists of ``PXmlNode``'s. - TXmlNodeKind* = enum ## different kinds of ``PXmlNode``s + TXmlNodeKind* = enum ## different kinds of ``PXmlNode``'s xnText, ## a text element xnElement, ## an element with 0 or more children xnCData, ## a CDATA node @@ -39,7 +39,7 @@ proc newXmlNode(kind: TXmlNodeKind): PXmlNode = result.k = kind proc newElement*(tag: string): PXmlNode = - ## creates a new ``PXmlNode``. of kind ``xnText`` with the given `tag`. + ## creates a new ``PXmlNode`` of kind ``xnText`` with the given `tag`. result = newXmlNode(xnElement) result.fTag = tag result.s = @[] @@ -208,9 +208,14 @@ proc add*(result: var string, n: PXmlNode, indent = 0, indWidth = 2) = result.add(n.fText) result.add(';') +const + xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + ## header to use for complete XML output + proc `$`*(n: PXmlNode): string = - ## converts `n` into its string representation. - result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + ## converts `n` into its string representation. No ``<$xml ...$>`` declaration + ## is produced, so that the produced XML fragments are composable. + result = "" result.add(n) proc newXmlTree*(tag: string, children: openArray[PXmlNode], @@ -227,16 +232,21 @@ proc xmlConstructor(e: PNimrodNode): PNimrodNode {.compileTime.} = var a = e[1] if a.kind == nnkCall: result = newCall("newXmlTree", toStrLit(a[0])) - var attrs = newCall("newStringTable", []) - var bracket = newNimNode(nnkBracket, a) + var attrs = newNimNode(nnkBracket, a) + var newStringTabCall = newCall("newStringTable", attrs, + newIdentNode("modeCaseSensitive")) + var elements = newNimNode(nnkBracket, a) for i in 1..a.len-1: - if a[i].kind == nnkExprEqExpr: + if a[i].kind == nnkExprEqExpr: attrs.add(toStrLit(a[i][0])) attrs.add(a[i][1]) + echo repr(attrs) else: - bracket.add(a[i]) - result.add(bracket) - if attrs.len > 1: result.add(attrs) + elements.add(a[i]) + result.add(elements) + if attrs.len > 1: + echo repr(newStringTabCall) + result.add(newStringTabCall) else: result = newCall("newXmlTree", toStrLit(a)) @@ -252,5 +262,3 @@ macro `<>`*(x: expr): expr = ## result = xmlConstructor(x) - - |