mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-07 04:14:19 +00:00
Add renderRstToJson in docutils
This commit is contained in:
@@ -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("]}")
|
||||
|
||||
Reference in New Issue
Block a user