docutils are gcsafe

This commit is contained in:
Araq 2015-04-03 16:47:26 +02:00
commit 3c54a4a438
2 changed files with 498 additions and 497 deletions

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,7 @@ type
rnField, # a field item rnField, # a field item
rnFieldName, # consisting of a field name ... rnFieldName, # consisting of a field name ...
rnFieldBody, # ... and a field body rnFieldBody, # ... and a field body
rnOptionList, rnOptionListItem, rnOptionGroup, rnOption, rnOptionString, rnOptionList, rnOptionListItem, rnOptionGroup, rnOption, rnOptionString,
rnOptionArgument, rnDescription, rnLiteralBlock, rnQuotedLiteralBlock, rnOptionArgument, rnDescription, rnLiteralBlock, rnQuotedLiteralBlock,
rnLineBlock, # the | thingie rnLineBlock, # the | thingie
rnLineBlockItem, # sons of the | thing rnLineBlockItem, # sons of the | thing
@ -50,7 +50,7 @@ type
# * `file#id <file#id>'_ # * `file#id <file#id>'_
rnSubstitutionDef, # a definition of a substitution rnSubstitutionDef, # a definition of a substitution
rnGeneralRole, # Inline markup: rnGeneralRole, # Inline markup:
rnSub, rnSup, rnIdx, rnSub, rnSup, rnIdx,
rnEmphasis, # "*" rnEmphasis, # "*"
rnStrongEmphasis, # "**" rnStrongEmphasis, # "**"
rnTripleEmphasis, # "***" rnTripleEmphasis, # "***"
@ -71,25 +71,25 @@ type
level*: int ## valid for some node kinds level*: int ## valid for some node kinds
sons*: TRstNodeSeq ## the node's sons sons*: TRstNodeSeq ## the node's sons
proc len*(n: PRstNode): int = proc len*(n: PRstNode): int =
result = len(n.sons) result = len(n.sons)
proc newRstNode*(kind: TRstNodeKind): PRstNode = proc newRstNode*(kind: TRstNodeKind): PRstNode =
new(result) new(result)
result.sons = @[] result.sons = @[]
result.kind = kind result.kind = kind
proc newRstNode*(kind: TRstNodeKind, s: string): PRstNode = proc newRstNode*(kind: TRstNodeKind, s: string): PRstNode =
result = newRstNode(kind) result = newRstNode(kind)
result.text = s result.text = s
proc lastSon*(n: PRstNode): PRstNode = proc lastSon*(n: PRstNode): PRstNode =
result = n.sons[len(n.sons)-1] result = n.sons[len(n.sons)-1]
proc add*(father, son: PRstNode) = proc add*(father, son: PRstNode) =
add(father.sons, son) add(father.sons, son)
proc addIfNotNil*(father, son: PRstNode) = proc addIfNotNil*(father, son: PRstNode) =
if son != nil: add(father, son) if son != nil: add(father, son)
@ -98,26 +98,27 @@ type
indent: int indent: int
verbatim: int verbatim: int
proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) proc renderRstToRst(d: var TRenderContext, n: PRstNode,
result: var string) {.gcsafe.}
proc renderRstSons(d: var TRenderContext, n: PRstNode, result: var string) = proc renderRstSons(d: var TRenderContext, n: PRstNode, result: var string) =
for i in countup(0, len(n) - 1): for i in countup(0, len(n) - 1):
renderRstToRst(d, n.sons[i], result) renderRstToRst(d, n.sons[i], result)
proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) = proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
# this is needed for the index generation; it may also be useful for # this is needed for the index generation; it may also be useful for
# debugging, but most code is already debugged... # debugging, but most code is already debugged...
const const
lvlToChar: array[0..8, char] = ['!', '=', '-', '~', '`', '<', '*', '|', '+'] lvlToChar: array[0..8, char] = ['!', '=', '-', '~', '`', '<', '*', '|', '+']
if n == nil: return if n == nil: return
var ind = spaces(d.indent) var ind = spaces(d.indent)
case n.kind case n.kind
of rnInner: of rnInner:
renderRstSons(d, n, result) renderRstSons(d, n, result)
of rnHeadline: of rnHeadline:
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
let oldLen = result.len let oldLen = result.len
renderRstSons(d, n, result) renderRstSons(d, n, result)
let headlineLen = result.len - oldLen let headlineLen = result.len - oldLen
@ -131,16 +132,16 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
var headline = "" var headline = ""
renderRstSons(d, n, headline) renderRstSons(d, n, headline)
let lvl = repeat(lvlToChar[n.level], headline.len - d.indent) let lvl = repeat(lvlToChar[n.level], headline.len - d.indent)
result.add(lvl) result.add(lvl)
result.add("\n") result.add("\n")
result.add(headline) result.add(headline)
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
result.add(lvl) result.add(lvl)
of rnTransition: of rnTransition:
result.add("\n\n") result.add("\n\n")
result.add(ind) result.add(ind)
result.add repeat('-', 78-d.indent) result.add repeat('-', 78-d.indent)
@ -149,11 +150,11 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add("\n\n") result.add("\n\n")
result.add(ind) result.add(ind)
renderRstSons(d, n, result) renderRstSons(d, n, result)
of rnBulletItem: of rnBulletItem:
inc(d.indent, 2) inc(d.indent, 2)
var tmp = "" var tmp = ""
renderRstSons(d, n, tmp) renderRstSons(d, n, tmp)
if tmp.len > 0: if tmp.len > 0:
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
result.add("* ") result.add("* ")
@ -163,22 +164,22 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
inc(d.indent, 4) inc(d.indent, 4)
var tmp = "" var tmp = ""
renderRstSons(d, n, tmp) renderRstSons(d, n, tmp)
if tmp.len > 0: if tmp.len > 0:
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
result.add("(#) ") result.add("(#) ")
result.add(tmp) result.add(tmp)
dec(d.indent, 4) dec(d.indent, 4)
of rnOptionList, rnFieldList, rnDefList, rnDefItem, rnLineBlock, rnFieldName, of rnOptionList, rnFieldList, rnDefList, rnDefItem, rnLineBlock, rnFieldName,
rnFieldBody, rnStandaloneHyperlink, rnBulletList, rnEnumList: rnFieldBody, rnStandaloneHyperlink, rnBulletList, rnEnumList:
renderRstSons(d, n, result) renderRstSons(d, n, result)
of rnDefName: of rnDefName:
result.add("\n\n") result.add("\n\n")
result.add(ind) result.add(ind)
renderRstSons(d, n, result) renderRstSons(d, n, result)
of rnDefBody: of rnDefBody:
inc(d.indent, 2) inc(d.indent, 2)
if n.sons[0].kind != rnBulletList: if n.sons[0].kind != rnBulletList:
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
result.add(" ") result.add(" ")
@ -187,10 +188,10 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
of rnField: of rnField:
var tmp = "" var tmp = ""
renderRstToRst(d, n.sons[0], tmp) renderRstToRst(d, n.sons[0], tmp)
var L = max(tmp.len + 3, 30) var L = max(tmp.len + 3, 30)
inc(d.indent, L) inc(d.indent, L)
result.add "\n" result.add "\n"
result.add ind result.add ind
result.add ':' result.add ':'
@ -198,9 +199,9 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add ':' result.add ':'
result.add spaces(L - tmp.len - 2) result.add spaces(L - tmp.len - 2)
renderRstToRst(d, n.sons[1], result) renderRstToRst(d, n.sons[1], result)
dec(d.indent, L) dec(d.indent, L)
of rnLineBlockItem: of rnLineBlockItem:
result.add("\n") result.add("\n")
result.add(ind) result.add(ind)
result.add("| ") result.add("| ")
@ -209,11 +210,11 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
inc(d.indent, 2) inc(d.indent, 2)
renderRstSons(d, n, result) renderRstSons(d, n, result)
dec(d.indent, 2) dec(d.indent, 2)
of rnRef: of rnRef:
result.add("`") result.add("`")
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("`_") result.add("`_")
of rnHyperlink: of rnHyperlink:
result.add('`') result.add('`')
renderRstToRst(d, n.sons[0], result) renderRstToRst(d, n.sons[0], result)
result.add(" <") result.add(" <")
@ -225,23 +226,23 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add("`:") result.add("`:")
renderRstToRst(d, n.sons[1],result) renderRstToRst(d, n.sons[1],result)
result.add(':') result.add(':')
of rnSub: of rnSub:
result.add('`') result.add('`')
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("`:sub:") result.add("`:sub:")
of rnSup: of rnSup:
result.add('`') result.add('`')
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("`:sup:") result.add("`:sup:")
of rnIdx: of rnIdx:
result.add('`') result.add('`')
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("`:idx:") result.add("`:idx:")
of rnEmphasis: of rnEmphasis:
result.add("*") result.add("*")
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("*") result.add("*")
of rnStrongEmphasis: of rnStrongEmphasis:
result.add("**") result.add("**")
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("**") result.add("**")
@ -249,11 +250,11 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add("***") result.add("***")
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add("***") result.add("***")
of rnInterpretedText: of rnInterpretedText:
result.add('`') result.add('`')
renderRstSons(d, n, result) renderRstSons(d, n, result)
result.add('`') result.add('`')
of rnInlineLiteral: of rnInlineLiteral:
inc(d.verbatim) inc(d.verbatim)
result.add("``") result.add("``")
renderRstSons(d, n, result) renderRstSons(d, n, result)
@ -266,11 +267,11 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add("\\\\") # XXX: escape more special characters! result.add("\\\\") # XXX: escape more special characters!
else: else:
result.add(n.text) result.add(n.text)
of rnIndex: of rnIndex:
result.add("\n\n") result.add("\n\n")
result.add(ind) result.add(ind)
result.add(".. index::\n") result.add(".. index::\n")
inc(d.indent, 3) inc(d.indent, 3)
if n.sons[2] != nil: renderRstSons(d, n.sons[2], result) if n.sons[2] != nil: renderRstSons(d, n.sons[2], result)
dec(d.indent, 3) dec(d.indent, 3)
@ -280,7 +281,7 @@ proc renderRstToRst(d: var TRenderContext, n: PRstNode, result: var string) =
result.add(".. contents::") result.add(".. contents::")
else: else:
result.add("Error: cannot render: " & $n.kind) result.add("Error: cannot render: " & $n.kind)
proc renderRstToRst*(n: PRstNode, result: var string) = proc renderRstToRst*(n: PRstNode, result: var string) =
## renders `n` into its string representation and appends to `result`. ## renders `n` into its string representation and appends to `result`.
var d: TRenderContext var d: TRenderContext
@ -302,7 +303,7 @@ proc renderRstToJsonNode(node: PRstNode): JsonNode =
proc renderRstToJson*(node: PRstNode): string = proc renderRstToJson*(node: PRstNode): string =
## Writes the given RST node as JSON that is in the form ## Writes the given RST node as JSON that is in the form
## :: ## ::
## { ## {
## "kind":string node.kind, ## "kind":string node.kind,
## "text":optional string node.text, ## "text":optional string node.text,