diff --git a/compiler/docgen.nim b/compiler/docgen.nim
index 2bcb397c32..50abcfc4e2 100755
--- a/compiler/docgen.nim
+++ b/compiler/docgen.nim
@@ -13,11 +13,14 @@
import
ast, astalgo, strutils, hashes, options, nversion, msgs, os, ropes, idents,
- wordrecg, math, syntaxes, renderer, lexer, rst, times, highlite, importer
+ wordrecg, syntaxes, renderer, lexer, rstast, rst, rstgen, times, highlite,
+ importer
proc CommandDoc*()
proc CommandRst2Html*()
proc CommandRst2TeX*()
+
+#proc CommandBuildIndex*()
# implementation
type
@@ -29,9 +32,9 @@ type
TMetaEnum = enum
metaNone, metaTitle, metaSubtitle, metaAuthor, metaVersion
TDocumentor {.final.} = object # contains a module's documentation
+ target: TOutputTarget
options: TRstParseOptions
filename*: string # filename of the source file; without extension
- basedir*: string # base directory (where to put the documentation)
modDesc*: PRope # module description
id*: int # for generating IDs
splitAfter*: int # split too long entries in the TOC
@@ -40,13 +43,10 @@ type
toc*, section*: TSections
indexFile*, theIndex*: PRstNode
indexValFilename*: string
- indent*, verbatim*: int # for code generation
meta*: array[TMetaEnum, PRope]
PDoc = ref TDocumentor
-var splitter: string = ""
-
proc findIndexNode(n: PRstNode): PRstNode =
if n == nil:
result = nil
@@ -59,10 +59,31 @@ proc findIndexNode(n: PRstNode): PRstNode =
result = result.sons[0]
else:
result = nil
- for i in countup(0, rsonsLen(n) - 1):
+ for i in countup(0, len(n) - 1):
result = findIndexNode(n.sons[i])
if result != nil: return
+proc compilerMsgHandler(filename: string, line, col: int,
+ msgKind: rst.TMsgKind, arg: string) {.procvar.} =
+ # translate msg kind:
+ var k: msgs.TMsgKind
+ case msgKind
+ of meCannotOpenFile: k = errCannotOpenFile
+ of meExpected: k = errXExpected
+ of meGridTableNotImplemented: k = errGridTableNotImplemented
+ of meNewSectionExpected: k = errNewSectionExpected
+ of meGeneralParseError: k = errGeneralParseError
+ of meInvalidDirective: k = errInvalidDirectiveX
+ of mwRedefinitionOfLabel: k = warnRedefinitionOfLabel
+ of mwUnknownSubstitution: k = warnUnknownSubstitutionX
+ GlobalError(newLineInfo(filename, line, col), k, arg)
+
+proc parseRst(text, filename: string,
+ line, column: int, hasToc: var bool,
+ rstOptions: TRstParseOptions): PRstNode =
+ result = rstParse(text, filename, line, column, hasToc, rstOptions,
+ options.FindFile, compilerMsgHandler)
+
proc initIndexFile(d: PDoc) =
var
h: PRstNode
@@ -71,7 +92,7 @@ proc initIndexFile(d: PDoc) =
gIndexFile = addFileExt(gIndexFile, "txt")
d.indexValFilename = changeFileExt(extractFilename(d.filename), HtmlExt)
if ExistsFile(gIndexFile):
- d.indexFile = rstParse(readFile(gIndexFile), gIndexFile, 0, 1,
+ d.indexFile = parseRst(readFile(gIndexFile), gIndexFile, 0, 1,
dummyHasToc, {roSupportRawDirective})
d.theIndex = findIndexNode(d.indexFile)
if (d.theIndex == nil) or (d.theIndex.kind != rnDefList):
@@ -81,17 +102,19 @@ proc initIndexFile(d: PDoc) =
d.indexFile = newRstNode(rnInner)
h = newRstNode(rnOverline)
h.level = 1
- addSon(h, newRstNode(rnLeaf, "Index"))
- addSon(d.indexFile, h)
+ add(h, newRstNode(rnLeaf, "Index"))
+ add(d.indexFile, h)
h = newRstNode(rnIndex)
- addSon(h, nil) # no argument
- addSon(h, nil) # no options
+ add(h, nil) # no argument
+ add(h, nil) # no options
d.theIndex = newRstNode(rnDefList)
- addSon(h, d.theIndex)
- addSon(d.indexFile, h)
+ add(h, d.theIndex)
+ add(d.indexFile, h)
proc newDocumentor(filename: string): PDoc =
new(result)
+ if gCmd != cmdRst2Tex: result.target = outHtml
+ else: result.target = outLatex
result.tocPart = @[]
result.filename = filename
result.id = 100
@@ -159,72 +182,7 @@ proc ropeFormatNamedVars(frmt: TFormatStr, varnames: openarray[string],
if (frmt[i] != '$'): inc(i)
else: break
if i - 1 >= start: app(result, substr(frmt, start, i - 1))
-
-proc addXmlChar(dest: var string, c: Char) =
- case c
- of '&': add(dest, "&")
- of '<': add(dest, "<")
- of '>': add(dest, ">")
- of '\"': add(dest, """)
- else: add(dest, c)
-
-proc addRtfChar(dest: var string, c: Char) =
- case c
- of '{': add(dest, "\\{")
- of '}': add(dest, "\\}")
- of '\\': add(dest, "\\\\")
- else: add(dest, c)
-
-proc addTexChar(dest: var string, c: Char) =
- case c
- of '_': add(dest, "\\_")
- of '{': add(dest, "\\symbol{123}")
- of '}': add(dest, "\\symbol{125}")
- of '[': add(dest, "\\symbol{91}")
- of ']': add(dest, "\\symbol{93}")
- of '\\': add(dest, "\\symbol{92}")
- of '$': add(dest, "\\$")
- of '&': add(dest, "\\&")
- of '#': add(dest, "\\#")
- of '%': add(dest, "\\%")
- of '~': add(dest, "\\symbol{126}")
- of '@': add(dest, "\\symbol{64}")
- of '^': add(dest, "\\symbol{94}")
- of '`': add(dest, "\\symbol{96}")
- else: add(dest, c)
-
-proc escChar(dest: var string, c: Char) =
- if gCmd != cmdRst2Tex: addXmlChar(dest, c)
- else: addTexChar(dest, c)
-
-proc nextSplitPoint(s: string, start: int): int =
- result = start
- while result < len(s) + 0:
- case s[result]
- of '_': return
- of 'a'..'z':
- if result + 1 < len(s) + 0:
- if s[result + 1] in {'A'..'Z'}: return
- else: nil
- inc(result)
- dec(result) # last valid index
-
-proc esc(s: string, splitAfter: int = - 1): string =
- result = ""
- if splitAfter >= 0:
- var partLen = 0
- var j = 0
- while j < len(s):
- var k = nextSplitPoint(s, j)
- if (splitter != " ") or (partLen + k - j + 1 > splitAfter):
- partLen = 0
- add(result, splitter)
- for i in countup(j, k): escChar(result, s[i])
- inc(partLen, k - j + 1)
- j = k + 1
- else:
- for i in countup(0, len(s) + 0 - 1): escChar(result, s[i])
-
+
proc disp(xml, tex: string): string =
if gCmd != cmdRst2Tex: result = xml
else: result = tex
@@ -241,17 +199,17 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope
proc renderAux(d: PDoc, n: PRstNode, outer: string = "$1"): PRope =
result = nil
- for i in countup(0, rsonsLen(n) - 1): app(result, renderRstToOut(d, n.sons[i]))
+ for i in countup(0, len(n) - 1): app(result, renderRstToOut(d, n.sons[i]))
result = ropef(outer, [result])
proc setIndexForSourceTerm(d: PDoc, name: PRstNode, id: int) =
if d.theIndex == nil: return
var h = newRstNode(rnHyperlink)
var a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $id)
- addSon(h, a)
- addSon(h, a)
+ add(h, a)
+ add(h, a)
a = newRstNode(rnIdx)
- addSon(a, name)
+ add(a, name)
setIndexPair(d.theIndex, a, h)
proc renderIndexTerm(d: PDoc, n: PRstNode): PRope =
@@ -260,14 +218,14 @@ proc renderIndexTerm(d: PDoc, n: PRstNode): PRope =
[toRope(d.id), renderAux(d, n)])
var h = newRstNode(rnHyperlink)
var a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $d.id)
- addSon(h, a)
- addSon(h, a)
+ add(h, a)
+ add(h, a)
setIndexPair(d.theIndex, n, h)
proc genComment(d: PDoc, n: PNode): PRope =
var dummyHasToc: bool
if n.comment != nil and startsWith(n.comment, "##"):
- result = renderRstToOut(d, rstParse(n.comment, toFilename(n.info),
+ result = renderRstToOut(d, parseRst(n.comment, toFilename(n.info),
toLineNumber(n.info), toColumn(n.info),
dummyHasToc,
d.options + {roSkipPounds}))
@@ -294,16 +252,16 @@ proc isVisible(n: PNode): bool =
elif n.kind == nkPragmaExpr:
result = isVisible(n.sons[0])
-proc getName(n: PNode, splitAfter: int = - 1): string =
+proc getName(d: PDoc, n: PNode, splitAfter: int = - 1): string =
case n.kind
- of nkPostfix: result = getName(n.sons[1], splitAfter)
- of nkPragmaExpr: result = getName(n.sons[0], splitAfter)
- of nkSym: result = esc(n.sym.name.s, splitAfter)
- of nkIdent: result = esc(n.ident.s, splitAfter)
+ of nkPostfix: result = getName(d, n.sons[1], splitAfter)
+ of nkPragmaExpr: result = getName(d, n.sons[0], splitAfter)
+ of nkSym: result = esc(d.target, n.sym.name.s, splitAfter)
+ of nkIdent: result = esc(d.target, n.ident.s, splitAfter)
of nkAccQuoted:
- result = esc("`")
- for i in 0..