xmltree.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## A simple XML tree generator.
  10. ##
  11. runnableExamples:
  12. var g = newElement("myTag")
  13. g.add newText("some text")
  14. g.add newComment("this is comment")
  15. var h = newElement("secondTag")
  16. h.add newEntity("some entity")
  17. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  18. let k = newXmlTree("treeTag", [g, h], att)
  19. doAssert $k == """<treeTag key1="first value" key2="second value">
  20. <myTag>some text<!-- this is comment --></myTag>
  21. <secondTag>&some entity;</secondTag>
  22. </treeTag>"""
  23. ## **See also:**
  24. ## * `xmlparser module <xmlparser.html>`_ for high-level XML parsing
  25. ## * `parsexml module <parsexml.html>`_ for low-level XML parsing
  26. ## * `htmlgen module <htmlgen.html>`_ for html code generator
  27. import std/private/since
  28. import macros, strtabs, strutils, sequtils
  29. when defined(nimPreviewSlimSystem):
  30. import std/assertions
  31. type
  32. XmlNode* = ref XmlNodeObj ## An XML tree consisting of XML nodes.
  33. ##
  34. ## Use `newXmlTree proc <#newXmlTree,string,openArray[XmlNode],XmlAttributes>`_
  35. ## for creating a new tree.
  36. XmlNodeKind* = enum ## Different kinds of XML nodes.
  37. xnText, ## a text element
  38. xnVerbatimText, ##
  39. xnElement, ## an element with 0 or more children
  40. xnCData, ## a CDATA node
  41. xnEntity, ## an entity (like ``&thing;``)
  42. xnComment ## an XML comment
  43. XmlAttributes* = StringTableRef ## An alias for a string to string mapping.
  44. ##
  45. ## Use `toXmlAttributes proc <#toXmlAttributes,varargs[tuple[string,string]]>`_
  46. ## to create `XmlAttributes`.
  47. XmlNodeObj {.acyclic.} = object
  48. case k: XmlNodeKind # private, use the kind() proc to read this field.
  49. of xnText, xnVerbatimText, xnComment, xnCData, xnEntity:
  50. fText: string
  51. of xnElement:
  52. fTag: string
  53. s: seq[XmlNode]
  54. fAttr: XmlAttributes
  55. fClientData: int ## for other clients
  56. const
  57. xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
  58. ## Header to use for complete XML output.
  59. template expect(node: XmlNode, kind: set[XmlNodeKind]) =
  60. ## Check the node's kind is within a set of values
  61. assert node.k in kind, "Got " & $node.k
  62. template expect(node: XmlNode, kind: XmlNodeKind) =
  63. ## Check the node's kind equals a value
  64. assert node.k == kind, "Got " & $node.k
  65. proc newXmlNode(kind: XmlNodeKind): XmlNode =
  66. ## Creates a new ``XmlNode``.
  67. result = XmlNode(k: kind)
  68. proc newElement*(tag: sink string): XmlNode =
  69. ## Creates a new ``XmlNode`` of kind ``xnElement`` with the given `tag`.
  70. ##
  71. ## See also:
  72. ## * `newXmlTree proc <#newXmlTree,string,openArray[XmlNode],XmlAttributes>`_
  73. ## * [<> macro](#<>.m,untyped)
  74. runnableExamples:
  75. var a = newElement("firstTag")
  76. a.add newElement("childTag")
  77. assert a.kind == xnElement
  78. assert $a == """<firstTag>
  79. <childTag />
  80. </firstTag>"""
  81. result = newXmlNode(xnElement)
  82. result.fTag = tag
  83. result.s = @[]
  84. # init attributes lazily to save memory
  85. proc newText*(text: sink string): XmlNode =
  86. ## Creates a new ``XmlNode`` of kind ``xnText`` with the text `text`.
  87. runnableExamples:
  88. var b = newText("my text")
  89. assert b.kind == xnText
  90. assert $b == "my text"
  91. result = newXmlNode(xnText)
  92. result.fText = text
  93. proc newVerbatimText*(text: sink string): XmlNode {.since: (1, 3).} =
  94. ## Creates a new ``XmlNode`` of kind ``xnVerbatimText`` with the text `text`.
  95. ## **Since**: Version 1.3.
  96. result = newXmlNode(xnVerbatimText)
  97. result.fText = text
  98. proc newComment*(comment: sink string): XmlNode =
  99. ## Creates a new ``XmlNode`` of kind ``xnComment`` with the text `comment`.
  100. runnableExamples:
  101. var c = newComment("my comment")
  102. assert c.kind == xnComment
  103. assert $c == "<!-- my comment -->"
  104. result = newXmlNode(xnComment)
  105. result.fText = comment
  106. proc newCData*(cdata: sink string): XmlNode =
  107. ## Creates a new ``XmlNode`` of kind ``xnCData`` with the text `cdata`.
  108. runnableExamples:
  109. var d = newCData("my cdata")
  110. assert d.kind == xnCData
  111. assert $d == "<![CDATA[my cdata]]>"
  112. result = newXmlNode(xnCData)
  113. result.fText = cdata
  114. proc newEntity*(entity: string): XmlNode =
  115. ## Creates a new ``XmlNode`` of kind ``xnEntity`` with the text `entity`.
  116. runnableExamples:
  117. var e = newEntity("my entity")
  118. assert e.kind == xnEntity
  119. assert $e == "&my entity;"
  120. result = newXmlNode(xnEntity)
  121. result.fText = entity
  122. proc newXmlTree*(tag: sink string, children: openArray[XmlNode],
  123. attributes: XmlAttributes = nil): XmlNode =
  124. ## Creates a new XML tree with `tag`, `children` and `attributes`.
  125. ##
  126. ## See also:
  127. ## * `newElement proc <#newElement,string>`_
  128. ## * [<> macro](#<>.m,untyped)
  129. runnableExamples:
  130. var g = newElement("myTag")
  131. g.add newText("some text")
  132. g.add newComment("this is comment")
  133. var h = newElement("secondTag")
  134. h.add newEntity("some entity")
  135. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  136. let k = newXmlTree("treeTag", [g, h], att)
  137. doAssert $k == """<treeTag key1="first value" key2="second value">
  138. <myTag>some text<!-- this is comment --></myTag>
  139. <secondTag>&some entity;</secondTag>
  140. </treeTag>"""
  141. result = newXmlNode(xnElement)
  142. result.fTag = tag
  143. newSeq(result.s, children.len)
  144. for i in 0..children.len-1: result.s[i] = children[i]
  145. result.fAttr = attributes
  146. proc text*(n: XmlNode): lent string {.inline.} =
  147. ## Gets the associated text with the node `n`.
  148. ##
  149. ## `n` can be a CDATA, Text, comment, or entity node.
  150. ##
  151. ## See also:
  152. ## * `text= proc <#text=,XmlNode,string>`_ for text setter
  153. ## * `tag proc <#tag,XmlNode>`_ for tag getter
  154. ## * `tag= proc <#tag=,XmlNode,string>`_ for tag setter
  155. ## * `innerText proc <#innerText,XmlNode>`_
  156. runnableExamples:
  157. var c = newComment("my comment")
  158. assert $c == "<!-- my comment -->"
  159. assert c.text == "my comment"
  160. n.expect {xnText, xnVerbatimText, xnComment, xnCData, xnEntity}
  161. result = n.fText
  162. proc `text=`*(n: XmlNode, text: sink string) {.inline.} =
  163. ## Sets the associated text with the node `n`.
  164. ##
  165. ## `n` can be a CDATA, Text, comment, or entity node.
  166. ##
  167. ## See also:
  168. ## * `text proc <#text,XmlNode>`_ for text getter
  169. ## * `tag proc <#tag,XmlNode>`_ for tag getter
  170. ## * `tag= proc <#tag=,XmlNode,string>`_ for tag setter
  171. runnableExamples:
  172. var e = newEntity("my entity")
  173. assert $e == "&my entity;"
  174. e.text = "a new entity text"
  175. assert $e == "&a new entity text;"
  176. n.expect {xnText, xnVerbatimText, xnComment, xnCData, xnEntity}
  177. n.fText = text
  178. proc tag*(n: XmlNode): lent string {.inline.} =
  179. ## Gets the tag name of `n`.
  180. ##
  181. ## `n` has to be an ``xnElement`` node.
  182. ##
  183. ## See also:
  184. ## * `text proc <#text,XmlNode>`_ for text getter
  185. ## * `text= proc <#text=,XmlNode,string>`_ for text setter
  186. ## * `tag= proc <#tag=,XmlNode,string>`_ for tag setter
  187. ## * `innerText proc <#innerText,XmlNode>`_
  188. runnableExamples:
  189. var a = newElement("firstTag")
  190. a.add newElement("childTag")
  191. assert $a == """<firstTag>
  192. <childTag />
  193. </firstTag>"""
  194. assert a.tag == "firstTag"
  195. n.expect xnElement
  196. result = n.fTag
  197. proc `tag=`*(n: XmlNode, tag: sink string) {.inline.} =
  198. ## Sets the tag name of `n`.
  199. ##
  200. ## `n` has to be an ``xnElement`` node.
  201. ##
  202. ## See also:
  203. ## * `text proc <#text,XmlNode>`_ for text getter
  204. ## * `text= proc <#text=,XmlNode,string>`_ for text setter
  205. ## * `tag proc <#tag,XmlNode>`_ for tag getter
  206. runnableExamples:
  207. var a = newElement("firstTag")
  208. a.add newElement("childTag")
  209. assert $a == """<firstTag>
  210. <childTag />
  211. </firstTag>"""
  212. a.tag = "newTag"
  213. assert $a == """<newTag>
  214. <childTag />
  215. </newTag>"""
  216. n.expect xnElement
  217. n.fTag = tag
  218. proc rawText*(n: XmlNode): string {.inline.} =
  219. ## Returns the underlying 'text' string by reference.
  220. ##
  221. ## This is only used for speed hacks.
  222. when defined(gcDestructors):
  223. result = move(n.fText)
  224. else:
  225. shallowCopy(result, n.fText)
  226. proc rawTag*(n: XmlNode): string {.inline.} =
  227. ## Returns the underlying 'tag' string by reference.
  228. ##
  229. ## This is only used for speed hacks.
  230. when defined(gcDestructors):
  231. result = move(n.fTag)
  232. else:
  233. shallowCopy(result, n.fTag)
  234. proc innerText*(n: XmlNode): string =
  235. ## Gets the inner text of `n`:
  236. ##
  237. ## - If `n` is `xnText` or `xnEntity`, returns its content.
  238. ## - If `n` is `xnElement`, runs recursively on each child node and
  239. ## concatenates the results.
  240. ## - Otherwise returns an empty string.
  241. ##
  242. ## See also:
  243. ## * `text proc <#text,XmlNode>`_
  244. runnableExamples:
  245. var f = newElement("myTag")
  246. f.add newText("my text")
  247. f.add newComment("my comment")
  248. f.add newEntity("my entity")
  249. assert $f == "<myTag>my text<!-- my comment -->&my entity;</myTag>"
  250. assert innerText(f) == "my textmy entity"
  251. proc worker(res: var string, n: XmlNode) =
  252. case n.k
  253. of xnText, xnEntity:
  254. res.add(n.fText)
  255. of xnElement:
  256. for sub in n.s:
  257. worker(res, sub)
  258. else:
  259. discard
  260. result = ""
  261. worker(result, n)
  262. proc add*(father, son: XmlNode) {.inline.} =
  263. ## Adds the child `son` to `father`.
  264. ## `father` must be of `xnElement` type
  265. ##
  266. ## See also:
  267. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  268. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  269. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  270. ## * `delete proc <#delete,XmlNode,Natural>`_
  271. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  272. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  273. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  274. runnableExamples:
  275. var f = newElement("myTag")
  276. f.add newText("my text")
  277. f.add newElement("sonTag")
  278. f.add newEntity("my entity")
  279. assert $f == "<myTag>my text<sonTag />&my entity;</myTag>"
  280. father.expect xnElement
  281. add(father.s, son)
  282. proc add*(father: XmlNode, sons: openArray[XmlNode]) {.inline.} =
  283. ## Adds the children `sons` to `father`.
  284. ## `father` must be of `xnElement` type
  285. ##
  286. ## See also:
  287. ## * `add proc <#add,XmlNode,XmlNode>`_
  288. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  289. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  290. ## * `delete proc <#delete,XmlNode,Natural>`_
  291. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  292. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  293. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  294. runnableExamples:
  295. var f = newElement("myTag")
  296. f.add(@[newText("my text"), newElement("sonTag"), newEntity("my entity")])
  297. assert $f == "<myTag>my text<sonTag />&my entity;</myTag>"
  298. father.expect xnElement
  299. add(father.s, sons)
  300. proc insert*(father, son: XmlNode, index: int) {.inline.} =
  301. ## Inserts the child `son` to a given position in `father`.
  302. ##
  303. ## `father` must be of `xnElement` kind.
  304. ##
  305. ## See also:
  306. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  307. ## * `add proc <#add,XmlNode,XmlNode>`_
  308. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  309. ## * `delete proc <#delete,XmlNode,Natural>`_
  310. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  311. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  312. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  313. runnableExamples:
  314. var f = newElement("myTag")
  315. f.add newElement("first")
  316. f.insert(newElement("second"), 0)
  317. assert $f == """<myTag>
  318. <second />
  319. <first />
  320. </myTag>"""
  321. father.expect xnElement
  322. if len(father.s) > index:
  323. insert(father.s, son, index)
  324. else:
  325. insert(father.s, son, len(father.s))
  326. proc insert*(father: XmlNode, sons: openArray[XmlNode], index: int) {.inline.} =
  327. ## Inserts the children openArray[`sons`] to a given position in `father`.
  328. ##
  329. ## `father` must be of `xnElement` kind.
  330. ##
  331. ## See also:
  332. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  333. ## * `add proc <#add,XmlNode,XmlNode>`_
  334. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  335. ## * `delete proc <#delete,XmlNode,Natural>`_
  336. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  337. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  338. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  339. runnableExamples:
  340. var f = newElement("myTag")
  341. f.add newElement("first")
  342. f.insert([newElement("second"), newElement("third")], 0)
  343. assert $f == """<myTag>
  344. <second />
  345. <third />
  346. <first />
  347. </myTag>"""
  348. father.expect xnElement
  349. if len(father.s) > index:
  350. insert(father.s, sons, index)
  351. else:
  352. insert(father.s, sons, len(father.s))
  353. proc delete*(n: XmlNode, i: Natural) =
  354. ## Deletes the `i`'th child of `n`.
  355. ##
  356. ## See also:
  357. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  358. ## * `add proc <#add,XmlNode,XmlNode>`_
  359. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  360. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  361. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  362. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  363. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  364. runnableExamples:
  365. var f = newElement("myTag")
  366. f.add newElement("first")
  367. f.insert(newElement("second"), 0)
  368. f.delete(0)
  369. assert $f == """<myTag>
  370. <first />
  371. </myTag>"""
  372. n.expect xnElement
  373. n.s.delete(i)
  374. proc delete*(n: XmlNode, slice: Slice[int]) =
  375. ## Deletes the items `n[slice]` of `n`.
  376. ##
  377. ## See also:
  378. ## * `delete proc <#delete.XmlNode,int>`_
  379. ## * `add proc <#add,XmlNode,XmlNode>`_
  380. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  381. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  382. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  383. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  384. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  385. runnableExamples:
  386. var f = newElement("myTag")
  387. f.add newElement("first")
  388. f.insert([newElement("second"), newElement("third")], 0)
  389. f.delete(0..1)
  390. assert $f == """<myTag>
  391. <first />
  392. </myTag>"""
  393. n.expect xnElement
  394. n.s.delete(slice)
  395. proc replace*(n: XmlNode, i: Natural, replacement: openArray[XmlNode]) =
  396. ## Replaces the `i`'th child of `n` with `replacement` openArray.
  397. ##
  398. ## `n` must be of `xnElement` kind.
  399. ##
  400. ## See also:
  401. ## * `replace proc <#replace.XmlNode,Slice[int],openArray[XmlNode]>`_
  402. ## * `add proc <#add,XmlNode,XmlNode>`_
  403. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  404. ## * `delete proc <#delete,XmlNode,Natural>`_
  405. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  406. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  407. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  408. runnableExamples:
  409. var f = newElement("myTag")
  410. f.add newElement("first")
  411. f.insert(newElement("second"), 0)
  412. f.replace(0, @[newElement("third"), newElement("fourth")])
  413. assert $f == """<myTag>
  414. <third />
  415. <fourth />
  416. <first />
  417. </myTag>"""
  418. n.expect xnElement
  419. n.s.delete(i)
  420. n.s.insert(replacement, i)
  421. proc replace*(n: XmlNode, slice: Slice[int], replacement: openArray[XmlNode]) =
  422. ## Deletes the items `n[slice]` of `n`.
  423. ##
  424. ## `n` must be of `xnElement` kind.
  425. ##
  426. ## See also:
  427. ## * `replace proc <#replace.XmlNode,int,openArray[XmlNode]>`_
  428. ## * `add proc <#add,XmlNode,XmlNode>`_
  429. ## * `add proc <#add,XmlNode,openArray[XmlNode]>`_
  430. ## * `delete proc <#delete,XmlNode,Natural>`_
  431. ## * `delete proc <#delete.XmlNode,Slice[int]>`_
  432. ## * `insert proc <#insert,XmlNode,XmlNode,int>`_
  433. ## * `insert proc <#insert,XmlNode,openArray[XmlNode],int>`_
  434. runnableExamples:
  435. var f = newElement("myTag")
  436. f.add newElement("first")
  437. f.insert([newElement("second"), newElement("fifth")], 0)
  438. f.replace(0..1, @[newElement("third"), newElement("fourth")])
  439. assert $f == """<myTag>
  440. <third />
  441. <fourth />
  442. <first />
  443. </myTag>"""
  444. n.expect xnElement
  445. n.s.delete(slice)
  446. n.s.insert(replacement, slice.a)
  447. proc len*(n: XmlNode): int {.inline.} =
  448. ## Returns the number of `n`'s children.
  449. runnableExamples:
  450. var f = newElement("myTag")
  451. f.add newElement("first")
  452. f.insert(newElement("second"), 0)
  453. assert len(f) == 2
  454. if n.k == xnElement: result = len(n.s)
  455. proc kind*(n: XmlNode): XmlNodeKind {.inline.} =
  456. ## Returns `n`'s kind.
  457. runnableExamples:
  458. var a = newElement("firstTag")
  459. assert a.kind == xnElement
  460. var b = newText("my text")
  461. assert b.kind == xnText
  462. result = n.k
  463. proc `[]`*(n: XmlNode, i: int): XmlNode {.inline.} =
  464. ## Returns the `i`'th child of `n`.
  465. runnableExamples:
  466. var f = newElement("myTag")
  467. f.add newElement("first")
  468. f.insert(newElement("second"), 0)
  469. assert $f[1] == "<first />"
  470. assert $f[0] == "<second />"
  471. n.expect xnElement
  472. result = n.s[i]
  473. proc `[]`*(n: var XmlNode, i: int): var XmlNode {.inline.} =
  474. ## Returns the `i`'th child of `n` so that it can be modified.
  475. n.expect xnElement
  476. result = n.s[i]
  477. proc clear*(n: var XmlNode) =
  478. ## Recursively clears all children of an XmlNode.
  479. ##
  480. runnableExamples:
  481. var g = newElement("myTag")
  482. g.add newText("some text")
  483. g.add newComment("this is comment")
  484. var h = newElement("secondTag")
  485. h.add newEntity("some entity")
  486. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  487. var k = newXmlTree("treeTag", [g, h], att)
  488. doAssert $k == """<treeTag key1="first value" key2="second value">
  489. <myTag>some text<!-- this is comment --></myTag>
  490. <secondTag>&some entity;</secondTag>
  491. </treeTag>"""
  492. clear(k)
  493. doAssert $k == """<treeTag key1="first value" key2="second value" />"""
  494. for i in 0 ..< n.len:
  495. clear(n[i])
  496. if n.k == xnElement:
  497. n.s.setLen(0)
  498. iterator items*(n: XmlNode): XmlNode {.inline.} =
  499. ## Iterates over all direct children of `n`.
  500. runnableExamples:
  501. var g = newElement("myTag")
  502. g.add newText("some text")
  503. g.add newComment("this is comment")
  504. var h = newElement("secondTag")
  505. h.add newEntity("some entity")
  506. g.add h
  507. assert $g == "<myTag>some text<!-- this is comment --><secondTag>&some entity;</secondTag></myTag>"
  508. # for x in g: # the same as `for x in items(g):`
  509. # echo x
  510. # some text
  511. # <!-- this is comment -->
  512. # <secondTag>&some entity;<![CDATA[some cdata]]></secondTag>
  513. n.expect xnElement
  514. for i in 0 .. n.len-1: yield n[i]
  515. iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
  516. ## Iterates over all direct children of `n` so that they can be modified.
  517. n.expect xnElement
  518. for i in 0 .. n.len-1: yield n[i]
  519. proc toXmlAttributes*(keyValuePairs: varargs[tuple[key,
  520. val: string]]): XmlAttributes =
  521. ## Converts `{key: value}` pairs into `XmlAttributes`.
  522. ##
  523. runnableExamples:
  524. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  525. var j = newElement("myTag")
  526. j.attrs = att
  527. doAssert $j == """<myTag key1="first value" key2="second value" />"""
  528. newStringTable(keyValuePairs)
  529. proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
  530. ## Gets the attributes belonging to `n`.
  531. ##
  532. ## Returns `nil` if attributes have not been initialised for this node.
  533. ##
  534. ## See also:
  535. ## * `attrs= proc <#attrs=,XmlNode,XmlAttributes>`_ for XmlAttributes setter
  536. ## * `attrsLen proc <#attrsLen,XmlNode>`_ for number of attributes
  537. ## * `attr proc <#attr,XmlNode,string>`_ for finding an attribute
  538. runnableExamples:
  539. var j = newElement("myTag")
  540. assert j.attrs == nil
  541. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  542. j.attrs = att
  543. assert j.attrs == att
  544. n.expect xnElement
  545. result = n.fAttr
  546. proc `attrs=`*(n: XmlNode, attr: XmlAttributes) {.inline.} =
  547. ## Sets the attributes belonging to `n`.
  548. ##
  549. ## See also:
  550. ## * `attrs proc <#attrs,XmlNode>`_ for XmlAttributes getter
  551. ## * `attrsLen proc <#attrsLen,XmlNode>`_ for number of attributes
  552. ## * `attr proc <#attr,XmlNode,string>`_ for finding an attribute
  553. runnableExamples:
  554. var j = newElement("myTag")
  555. assert j.attrs == nil
  556. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  557. j.attrs = att
  558. assert j.attrs == att
  559. n.expect xnElement
  560. n.fAttr = attr
  561. proc attrsLen*(n: XmlNode): int {.inline.} =
  562. ## Returns the number of `n`'s attributes.
  563. ##
  564. ## See also:
  565. ## * `attrs proc <#attrs,XmlNode>`_ for XmlAttributes getter
  566. ## * `attrs= proc <#attrs=,XmlNode,XmlAttributes>`_ for XmlAttributes setter
  567. ## * `attr proc <#attr,XmlNode,string>`_ for finding an attribute
  568. runnableExamples:
  569. var j = newElement("myTag")
  570. assert j.attrsLen == 0
  571. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  572. j.attrs = att
  573. assert j.attrsLen == 2
  574. n.expect xnElement
  575. if not isNil(n.fAttr): result = len(n.fAttr)
  576. proc attr*(n: XmlNode, name: string): string =
  577. ## Finds the first attribute of `n` with a name of `name`.
  578. ## Returns "" on failure.
  579. ##
  580. ## See also:
  581. ## * `attrs proc <#attrs,XmlNode>`_ for XmlAttributes getter
  582. ## * `attrs= proc <#attrs=,XmlNode,XmlAttributes>`_ for XmlAttributes setter
  583. ## * `attrsLen proc <#attrsLen,XmlNode>`_ for number of attributes
  584. runnableExamples:
  585. var j = newElement("myTag")
  586. let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
  587. j.attrs = att
  588. assert j.attr("key1") == "first value"
  589. assert j.attr("key2") == "second value"
  590. n.expect xnElement
  591. if n.attrs == nil: return ""
  592. return n.attrs.getOrDefault(name)
  593. proc clientData*(n: XmlNode): int {.inline.} =
  594. ## Gets the client data of `n`.
  595. ##
  596. ## The client data field is used by the HTML parser and generator.
  597. result = n.fClientData
  598. proc `clientData=`*(n: XmlNode, data: int) {.inline.} =
  599. ## Sets the client data of `n`.
  600. ##
  601. ## The client data field is used by the HTML parser and generator.
  602. n.fClientData = data
  603. proc addEscaped*(result: var string, s: string) =
  604. ## The same as `result.add(escape(s)) <#escape,string>`_, but more efficient.
  605. for c in items(s):
  606. case c
  607. of '<': result.add("&lt;")
  608. of '>': result.add("&gt;")
  609. of '&': result.add("&amp;")
  610. of '"': result.add("&quot;")
  611. of '\'': result.add("&apos;")
  612. else: result.add(c)
  613. proc escape*(s: string): string =
  614. ## Escapes `s` for inclusion into an XML document.
  615. ##
  616. ## Escapes these characters:
  617. ##
  618. ## ============ ===================
  619. ## char is converted to
  620. ## ============ ===================
  621. ## ``<`` ``&lt;``
  622. ## ``>`` ``&gt;``
  623. ## ``&`` ``&amp;``
  624. ## ``"`` ``&quot;``
  625. ## ``'`` ``&apos;``
  626. ## ============ ===================
  627. ##
  628. ## You can also use `addEscaped proc <#addEscaped,string,string>`_.
  629. result = newStringOfCap(s.len)
  630. addEscaped(result, s)
  631. proc addIndent(result: var string, indent: int, addNewLines: bool) =
  632. if addNewLines:
  633. result.add("\n")
  634. for i in 1 .. indent:
  635. result.add(' ')
  636. proc addImpl(result: var string, n: XmlNode, indent = 0, indWidth = 2,
  637. addNewLines = true, lastNodeIsText = false) =
  638. proc noWhitespace(n: XmlNode): bool =
  639. for i in 0 ..< n.len:
  640. if n[i].kind in {xnText, xnVerbatimText, xnEntity}: return true
  641. proc addEscapedAttr(result: var string, s: string) =
  642. # `addEscaped` alternative with less escaped characters.
  643. # Only to be used for escaping attribute values enclosed in double quotes!
  644. for c in items(s):
  645. case c
  646. of '<': result.add("&lt;")
  647. of '>': result.add("&gt;")
  648. of '&': result.add("&amp;")
  649. of '"': result.add("&quot;")
  650. else: result.add(c)
  651. if n == nil: return
  652. case n.k
  653. of xnElement:
  654. if indent > 0 and not lastNodeIsText:
  655. result.addIndent(indent, addNewLines)
  656. let
  657. addNewLines = if n.noWhitespace():
  658. false
  659. else:
  660. addNewLines
  661. result.add('<')
  662. result.add(n.fTag)
  663. if not isNil(n.fAttr):
  664. for key, val in pairs(n.fAttr):
  665. result.add(' ')
  666. result.add(key)
  667. result.add("=\"")
  668. result.addEscapedAttr(val)
  669. result.add('"')
  670. if n.len == 0:
  671. result.add(" />")
  672. return
  673. let
  674. indentNext = if n.noWhitespace():
  675. indent
  676. else:
  677. indent+indWidth
  678. result.add('>')
  679. var lastNodeIsText = false
  680. for i in 0 ..< n.len:
  681. result.addImpl(n[i], indentNext, indWidth, addNewLines, lastNodeIsText)
  682. lastNodeIsText = (n[i].kind == xnText) or (n[i].kind == xnVerbatimText)
  683. if not n.noWhitespace():
  684. result.addIndent(indent, addNewLines)
  685. result.add("</")
  686. result.add(n.fTag)
  687. result.add(">")
  688. of xnText:
  689. result.addEscaped(n.fText)
  690. of xnVerbatimText:
  691. result.add(n.fText)
  692. of xnComment:
  693. result.add("<!-- ")
  694. result.addEscaped(n.fText)
  695. result.add(" -->")
  696. of xnCData:
  697. result.add("<![CDATA[")
  698. result.add(n.fText)
  699. result.add("]]>")
  700. of xnEntity:
  701. result.add('&')
  702. result.add(n.fText)
  703. result.add(';')
  704. proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
  705. addNewLines = true) {.inline.} =
  706. ## Adds the textual representation of `n` to string `result`.
  707. runnableExamples:
  708. var
  709. a = newElement("firstTag")
  710. b = newText("my text")
  711. c = newComment("my comment")
  712. s = ""
  713. s.add(c)
  714. s.add(a)
  715. s.add(b)
  716. assert s == "<!-- my comment --><firstTag />my text"
  717. result.addImpl(n, indent, indWidth, addNewLines)
  718. proc `$`*(n: XmlNode): string =
  719. ## Converts `n` into its string representation.
  720. ##
  721. ## No ``<$xml ...$>`` declaration is produced, so that the produced
  722. ## XML fragments are composable.
  723. result = ""
  724. result.add(n)
  725. proc child*(n: XmlNode, name: string): XmlNode =
  726. ## Finds the first child element of `n` with a name of `name`.
  727. ## Returns `nil` on failure.
  728. runnableExamples:
  729. var f = newElement("myTag")
  730. f.add newElement("firstSon")
  731. f.add newElement("secondSon")
  732. f.add newElement("thirdSon")
  733. assert $(f.child("secondSon")) == "<secondSon />"
  734. n.expect xnElement
  735. for i in items(n):
  736. if i.kind == xnElement:
  737. if i.tag == name:
  738. return i
  739. proc findAll*(n: XmlNode, tag: string, result: var seq[XmlNode],
  740. caseInsensitive = false) =
  741. ## Iterates over all the children of `n` returning those matching `tag`.
  742. ##
  743. ## Found nodes satisfying the condition will be appended to the `result`
  744. ## sequence.
  745. runnableExamples:
  746. var
  747. b = newElement("good")
  748. c = newElement("bad")
  749. d = newElement("BAD")
  750. e = newElement("GOOD")
  751. b.add newText("b text")
  752. c.add newText("c text")
  753. d.add newText("d text")
  754. e.add newText("e text")
  755. let a = newXmlTree("father", [b, c, d, e])
  756. var s = newSeq[XmlNode]()
  757. a.findAll("good", s)
  758. assert $s == "@[<good>b text</good>]"
  759. s.setLen(0)
  760. a.findAll("good", s, caseInsensitive = true)
  761. assert $s == "@[<good>b text</good>, <GOOD>e text</GOOD>]"
  762. s.setLen(0)
  763. a.findAll("BAD", s)
  764. assert $s == "@[<BAD>d text</BAD>]"
  765. s.setLen(0)
  766. a.findAll("BAD", s, caseInsensitive = true)
  767. assert $s == "@[<bad>c text</bad>, <BAD>d text</BAD>]"
  768. n.expect xnElement
  769. for child in n.items():
  770. if child.k != xnElement:
  771. continue
  772. if child.tag == tag or
  773. (caseInsensitive and cmpIgnoreCase(child.tag, tag) == 0):
  774. result.add(child)
  775. child.findAll(tag, result)
  776. proc findAll*(n: XmlNode, tag: string, caseInsensitive = false): seq[XmlNode] =
  777. ## A shortcut version to assign in let blocks.
  778. runnableExamples:
  779. var
  780. b = newElement("good")
  781. c = newElement("bad")
  782. d = newElement("BAD")
  783. e = newElement("GOOD")
  784. b.add newText("b text")
  785. c.add newText("c text")
  786. d.add newText("d text")
  787. e.add newText("e text")
  788. let a = newXmlTree("father", [b, c, d, e])
  789. assert $(a.findAll("good")) == "@[<good>b text</good>]"
  790. assert $(a.findAll("BAD")) == "@[<BAD>d text</BAD>]"
  791. assert $(a.findAll("good", caseInsensitive = true)) == "@[<good>b text</good>, <GOOD>e text</GOOD>]"
  792. assert $(a.findAll("BAD", caseInsensitive = true)) == "@[<bad>c text</bad>, <BAD>d text</BAD>]"
  793. newSeq(result, 0)
  794. findAll(n, tag, result, caseInsensitive)
  795. proc xmlConstructor(a: NimNode): NimNode =
  796. if a.kind == nnkCall:
  797. result = newCall("newXmlTree", toStrLit(a[0]))
  798. var attrs = newNimNode(nnkBracket, a)
  799. var newStringTabCall = newCall(bindSym"newStringTable", attrs,
  800. bindSym"modeCaseSensitive")
  801. var elements = newNimNode(nnkBracket, a)
  802. for i in 1..a.len-1:
  803. if a[i].kind == nnkExprEqExpr:
  804. # In order to support attributes like `data-lang` we have to
  805. # replace whitespace because `toStrLit` gives `data - lang`.
  806. let attrName = toStrLit(a[i][0]).strVal.replace(" ", "")
  807. attrs.add(newStrLitNode(attrName))
  808. attrs.add(a[i][1])
  809. #echo repr(attrs)
  810. else:
  811. elements.add(a[i])
  812. result.add(elements)
  813. if attrs.len > 1:
  814. #echo repr(newStringTabCall)
  815. result.add(newStringTabCall)
  816. else:
  817. result = newCall("newXmlTree", toStrLit(a))
  818. macro `<>`*(x: untyped): untyped =
  819. ## Constructor macro for XML. Example usage:
  820. ##
  821. ## .. code-block:: nim
  822. ## <>a(href="http://nim-lang.org", newText("Nim rules."))
  823. ##
  824. ## Produces an XML tree for:
  825. ##
  826. ## <a href="http://nim-lang.org">Nim rules.</a>
  827. ##
  828. result = xmlConstructor(x)