summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2014-04-08 00:09:37 +0200
committerAndreas Rumpf <rumpf_a@web.de>2014-04-08 00:09:37 +0200
commit230af70be49f0af48cd1152f1dae5074d4fcd106 (patch)
tree850a43e911a9517d149976a706a1cf7bcc19e682
parent369d8c57ae6242afc7b64d79a4df48a539312677 (diff)
parent99ebcaec3a6902dee05ec2ffac59e4d01db235f8 (diff)
downloadNim-230af70be49f0af48cd1152f1dae5074d4fcd106.tar.gz
Merge pull request #1069 from flaviut/devel
Add renderRstToJson in docutils
-rw-r--r--lib/packages/docutils/rstast.nim26
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/packages/docutils/rstast.nim b/lib/packages/docutils/rstast.nim
index f5ef0f53d..8f946d973 100644
--- a/lib/packages/docutils/rstast.nim
+++ b/lib/packages/docutils/rstast.nim
@@ -9,7 +9,7 @@
 
 ## This module implements an AST for the `reStructuredText`:idx: parser.
 
-import strutils
+import strutils, json
 
 type
   TRstNodeKind* = enum        ## the possible node kinds of an PRstNode
@@ -286,3 +286,27 @@ proc renderRstToRst*(n: PRstNode, result: var string) =
   var d: TRenderContext
   renderRstToRst(d, n, result)
 
+proc renderRstToJsonNode(node: PRstNode): PJsonNode =
+  result =
+    %[
+      (key: "kind", val: %($node.kind)),
+      (key: "level", val: %BiggestInt(node.level))
+     ]
+  if node.text != nil:
+    result.add("text", %node.text)
+  if node.sons != nil and len(node.sons) > 0:
+    var accm = newSeq[PJsonNode](len(node.sons))
+    for i, son in node.sons:
+      accm[i] = renderRstToJsonNode(son)
+    result.add("sons", %accm)
+
+proc renderRstToJson*(node: PRstNode): string =
+  ## Writes the given RST node as JSON that is in the form
+  ## :: 
+  ##   {
+  ##     "kind":string node.kind,
+  ##     "text":optional string node.text,
+  ##     "level":optional int node.level,
+  ##     "sons":optional node array
+  ##   }
+  renderRstToJsonNode(node).pretty