docgen: support markdown link syntax; enable markdown extensions

This commit is contained in:
Araq
2019-01-11 14:20:34 +01:00
committed by Andreas Rumpf
parent c3d80647ae
commit 5ef5dc86c5
6 changed files with 37 additions and 4 deletions

View File

@@ -119,7 +119,7 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
result.conf = conf
result.cache = cache
initRstGenerator(result[], (if conf.cmd != cmdRst2tex: outHtml else: outLatex),
conf.configVars, filename.string, {roSupportRawDirective},
conf.configVars, filename.string, {roSupportRawDirective, roSupportMarkdown},
docgenFindFile, compilerMsgHandler)
if conf.configVars.hasKey("doc.googleAnalytics"):
@@ -991,7 +991,7 @@ proc commandRstAux(cache: IdentCache, conf: ConfigRef;
d.isPureRst = true
var rst = parseRst(readFile(filen.string), filen.string, 0, 1, d.hasToc,
{roSupportRawDirective}, conf)
{roSupportRawDirective, roSupportMarkdown}, conf)
var modDesc = newStringOfCap(30_000)
renderRstToOut(d[], rst, modDesc)
d.modDesc = rope(modDesc)

View File

@@ -780,6 +780,31 @@ proc parseMarkdownCodeblock(p: var RstParser): PRstNode =
add(result, nil)
add(result, lb)
proc parseMarkdownLink(p: var RstParser; father: PRstNode): bool =
result = true
var desc, link = ""
var i = p.idx
template parse(endToken, dest) =
inc i # skip begin token
while true:
if p.tok[i].kind in {tkEof, tkIndent}: return false
if p.tok[i].symbol == endToken: break
dest.add p.tok[i].symbol
inc i
inc i # skip end token
parse("]", desc)
if p.tok[i].symbol != "(": return false
parse(")", link)
let child = newRstNode(rnHyperlink)
child.add desc
child.add link
# only commit if we detected no syntax error:
father.add child
p.idx = i
result = true
proc parseInline(p: var RstParser, father: PRstNode) =
case p.tok[p.idx].kind
of tkPunct:
@@ -811,6 +836,9 @@ proc parseInline(p: var RstParser, father: PRstNode) =
var n = newRstNode(rnSubstitutionReferences)
parseUntil(p, n, "|", false)
add(father, n)
elif roSupportMarkdown in p.s.options and p.tok[p.idx].symbol == "[" and
parseMarkdownLink(p, father):
discard "parseMarkdownLink already processed it"
else:
if roSupportSmilies in p.s.options:
let n = parseSmiley(p)

View File

@@ -89,6 +89,9 @@ proc lastSon*(n: PRstNode): PRstNode =
proc add*(father, son: PRstNode) =
add(father.sons, son)
proc add*(father: PRstNode; s: string) =
add(father.sons, newRstNode(rnLeaf, s))
proc addIfNotNil*(father, son: PRstNode) =
if son != nil: add(father, son)

View File

@@ -28,5 +28,5 @@ proc test(dir: string; fixup = false) =
echo "SUCCESS: files identical: ", produced
removeDir(dir / "htmldocs")
test("nimdoc/testproject", false)
test("nimdoc/testproject", defined(fixup))
if failures > 0: quit($failures & " failures occurred.")

View File

@@ -1369,7 +1369,7 @@ The enum B.
<a id="someFunc,"></a>
<dt><pre><span class="Keyword">func</span> <span class="Identifier">someFunc</span><span class="Other">(</span><span class="Other">)</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>
My someFunc.
My someFunc. Stuff in <tt class="docutils literal"><span class="pre">quotes</span></tt> here. <a class="reference external" href="https://nim-lang.org">Some link</a>
</dd>

View File

@@ -23,4 +23,6 @@ template bEnum*(): untyped =
func someFunc*() =
## My someFunc.
## Stuff in `quotes` here.
## [Some link](https://nim-lang.org)
discard