Add renderRstToJson in docutils
This commit is contained in:
parent
033f2bbbf2
commit
335de0c49a
1 changed files with 25 additions and 0 deletions
|
|
@ -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("]}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue