forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix nim-lang#8871 runnableExamples now preserves source code comments…
…, litterals, and all formatting
- Loading branch information
1 parent
0cc22de
commit 1c769d4
Showing
5 changed files
with
119 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import strutils | ||
from xmltree import addEscaped | ||
|
||
import ast, options, msgs | ||
import packages/docutils/highlite | ||
|
||
proc lastNodeRec(n: PNode): PNode = | ||
# MOVE | ||
result = n | ||
while result.safeLen > 0: | ||
result = result[^1] | ||
|
||
proc isInIndentationBlock(src: string, indent: int): bool = | ||
#[ | ||
we stop at the first de-indentation; there's an inherent ambiguity with non | ||
doc comments since they can have arbitrary indentation, so we just take the | ||
practical route and require a runnableExamples to keep its code (including non | ||
doc comments) to its indentation level. | ||
]# | ||
for j in 0..<indent: | ||
if src.len <= j: return true | ||
if src[j] == ' ': continue | ||
return false | ||
return true | ||
|
||
proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode): string = | ||
## TLineInfo.offsetA,offsetB would be cleaner but it's only enabled for nimpretty, | ||
## we'd need to check performance impact to enable it for nimdoc. | ||
let first = n.lastSon.info | ||
let last = n.lastNodeRec.info | ||
var ret = "" | ||
var info = first | ||
var indent = info.col | ||
let numLines = numLines(conf, info.fileIndex).uint16 | ||
var lastNonemptyPos = 0 | ||
for line in first.line..<numLines: | ||
info.line = line | ||
let src = sourceLine(conf, info) | ||
if line > last.line and not isInIndentationBlock(src, indent): | ||
break | ||
if line > first.line: ret.add "\n" | ||
if src.len > indent: | ||
ret.add src[indent..^1] | ||
lastNonemptyPos = ret.len | ||
ret = ret[0..<lastNonemptyPos] | ||
return ret | ||
|
||
proc renderHtml*(result: var string, code: string) = | ||
var toknizr: GeneralTokenizer | ||
initGeneralTokenizer(toknizr, code) | ||
var buf = "" | ||
template append(kind, val) = | ||
buf.setLen 0 | ||
buf.addEscaped(val) | ||
let class = tokenClassToStr[kind] | ||
# TODO: use dispA | ||
# dispA(d.conf, result, "<span class=\"StringLit\">$1</span>", "\\spanStringLit{$1}", [escLit]) | ||
# result.add "<span class=\"$1\">$2</span>" % [class, val] | ||
result.add "<span class=\"$1\">$2</span>" % [class, buf] | ||
|
||
while true: | ||
getNextToken(toknizr, langNim) | ||
case toknizr.kind | ||
of gtEof: break # End Of File (or string) | ||
else: | ||
# TODO: avoid alloc; maybe toOpenArray | ||
append(toknizr.kind, substr(code, toknizr.start, toknizr.length + toknizr.start - 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters