docgen: support markdown headings too

This commit is contained in:
Araq
2019-01-11 15:06:45 +01:00
committed by Andreas Rumpf
parent 5ef5dc86c5
commit ee7f2d97cc
4 changed files with 57 additions and 11 deletions

View File

@@ -1065,15 +1065,32 @@ proc isOptionList(p: RstParser): bool =
result = match(p, p.idx, "-w") or match(p, p.idx, "--w") or
match(p, p.idx, "/w") or match(p, p.idx, "//w")
proc isMarkdownHeadlinePattern(s: string): bool =
if s.len >= 1 and s.len <= 6:
for c in s:
if c != '#': return false
result = true
proc isMarkdownHeadline(p: RstParser): bool =
if roSupportMarkdown in p.s.options:
if isMarkdownHeadlinePattern(p.tok[p.idx].symbol) and p.tok[p.idx+1].kind == tkWhite:
if p.tok[p.idx+2].kind in {tkWord, tkOther, tkPunct}:
result = true
proc whichSection(p: RstParser): RstNodeKind =
case p.tok[p.idx].kind
of tkAdornment:
if match(p, p.idx + 1, "ii"): result = rnTransition
elif match(p, p.idx + 1, " a"): result = rnTable
elif match(p, p.idx + 1, "i"): result = rnOverline
else: result = rnLeaf
elif isMarkdownHeadline(p):
result = rnHeadline
else:
result = rnLeaf
of tkPunct:
if match(p, tokenAfterNewline(p), "ai"):
if isMarkdownHeadline(p):
result = rnHeadline
elif match(p, tokenAfterNewline(p), "ai"):
result = rnHeadline
elif p.tok[p.idx].symbol == "::":
result = rnLiteralBlock
@@ -1158,12 +1175,18 @@ proc parseParagraph(p: var RstParser, result: PRstNode) =
proc parseHeadline(p: var RstParser): PRstNode =
result = newRstNode(rnHeadline)
parseUntilNewline(p, result)
assert(p.tok[p.idx].kind == tkIndent)
assert(p.tok[p.idx + 1].kind == tkAdornment)
var c = p.tok[p.idx + 1].symbol[0]
inc(p.idx, 2)
result.level = getLevel(p.s.underlineToLevel, p.s.uLevel, c)
if isMarkdownHeadline(p):
result.level = p.tok[p.idx].symbol.len
assert(p.tok[p.idx+1].kind == tkWhite)
inc p.idx, 2
parseUntilNewline(p, result)
else:
parseUntilNewline(p, result)
assert(p.tok[p.idx].kind == tkIndent)
assert(p.tok[p.idx + 1].kind == tkAdornment)
var c = p.tok[p.idx + 1].symbol[0]
inc(p.idx, 2)
result.level = getLevel(p.s.underlineToLevel, p.s.uLevel, c)
type
IntSeq = seq[int]

View File

@@ -1238,7 +1238,12 @@ function main() {
</select>
</div>
<ul class="simple simple-toc" id="toc-list">
<li>
<li><a class="reference" id="this-is-now-a-header_toc" href="#this-is-now-a-header">This is now a header</a></li>
<ul class="simple"><li><a class="reference" id="this-is-now-a-header-next-header_toc" href="#this-is-now-a-header-next-header">Next header</a></li>
<ul class="simple"><li><a class="reference" id="next-header-and-so-on_toc" href="#next-header-and-so-on">And so on</a></li>
</ul></ul><li><a class="reference" id="more-headers_toc" href="#more-headers">More headers</a></li>
<ul class="simple"><li><a class="reference" id="more-headers-up-to-level-6_toc" href="#more-headers-up-to-level-6">Up to level 6</a></li>
</ul><li>
<a class="reference reference-toplevel" href="#7" id="57">Types</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#SomeType"
@@ -1271,7 +1276,12 @@ function main() {
</div>
<div class="nine columns" id="content">
<div id="tocRoot"></div>
<p class="module-desc"></p>
<p class="module-desc">
<h1><a class="toc-backref" id="this-is-now-a-header" href="#this-is-now-a-header">This is now a header</a></h1>
<h2><a class="toc-backref" id="this-is-now-a-header-next-header" href="#this-is-now-a-header-next-header">Next header</a></h2>
<h3><a class="toc-backref" id="next-header-and-so-on" href="#next-header-and-so-on">And so on</a></h3>
<h1><a class="toc-backref" id="more-headers" href="#more-headers">More headers</a></h1>
<h6><a class="toc-backref" id="more-headers-up-to-level-6" href="#more-headers-up-to-level-6">Up to level 6</a></h6></p>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">

View File

@@ -1220,7 +1220,7 @@ function main() {
<div class="document" id="documentId">
<div class="container">
<h1 class="title">Index</h1>
Modules: <a href="subdir/subdir_b/utils.html">subdir/subdir_b/utils</a>, <a href="testproject.html">testproject</a>.<br/><p /><h2>API symbols</h2>
Modules: <a href="testproject.html">testproject</a>, <a href="utils.html">utils</a>.<br/><p /><h2>API symbols</h2>
<dl><dt><a name="A" href="#A"><span>A:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: A" href="testproject.html#A">testproject: A</a></li>

View File

@@ -1,3 +1,16 @@
##[
# This is now a header
## Next header
### And so on
# More headers
###### Up to level 6
]##
type
SomeType* = enum