diff options
author | flaviut <tamasflaviu@gmail.com> | 2014-04-05 12:51:23 -0400 |
---|---|---|
committer | flaviut <tamasflaviu@gmail.com> | 2014-04-05 15:58:16 -0400 |
commit | 335de0c49af97af9ac1ef329026c33ae5a25585a (patch) | |
tree | be77e7f829ff0f07018cadb8844a39a932c70564 | |
parent | 033f2bbbf267cd4fe4f2567aca8aa7483201a737 (diff) | |
download | Nim-335de0c49af97af9ac1ef329026c33ae5a25585a.tar.gz |
Add renderRstToJson in docutils
-rw-r--r-- | lib/packages/docutils/rstast.nim | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/packages/docutils/rstast.nim b/lib/packages/docutils/rstast.nim index f5ef0f53d..0476e2b07 100644 --- a/lib/packages/docutils/rstast.nim +++ b/lib/packages/docutils/rstast.nim @@ -286,3 +286,28 @@ proc renderRstToRst*(n: PRstNode, result: var string) = var d: TRenderContext renderRstToRst(d, n, result) +proc renderRstToJson*(node: PRstNode): string = + ## Writes the given RST node as JSON that is in the form + ## :: code-block + ## { + ## "kind":string node.kind, + ## "text":optional string node.text, + ## "level":optional int node.level, + ## "sons":optional node array + ## } + result = "" + result.add("{\"kind\":\"" & $ node.kind & "\",") + if node.text != Nil: + # XXX Json spec requires control charecters be escaped as \uXXXX, + # strutils.escape writes them as \uXX + result.add("\"text\":" & node.text.escape & ",") + result.add("\"level\":" & $ node.level) + if node.sons == nil or len(node.sons) == 0: + result.add("}") + else: + result.add(",\"sons\":[") + for i, son in node.sons: + result.add(renderRstToJson(son)) + if i < len(node.sons) - 1: + result.add(",") + result.add("]}") |