diff options
Diffstat (limited to 'compiler/renderverbatim.nim')
-rw-r--r-- | compiler/renderverbatim.nim | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/compiler/renderverbatim.nim b/compiler/renderverbatim.nim index 2dce6824c..c12595156 100644 --- a/compiler/renderverbatim.nim +++ b/compiler/renderverbatim.nim @@ -1,8 +1,10 @@ -import strutils -from xmltree import addEscaped +import std/strutils import ast, options, msgs -import packages/docutils/highlite + +when defined(nimPreviewSlimSystem): + import std/assertions + const isDebug = false when isDebug: @@ -38,6 +40,7 @@ type LineData = object proc tripleStrLitStartsAtNextLine(conf: ConfigRef, n: PNode): bool = # enabling TLineInfo.offsetA,offsetB would probably make this easier + result = false const tripleQuote = "\"\"\"" let src = sourceLine(conf, n.info) let col = n.info.col @@ -85,7 +88,7 @@ proc startOfLineInsideTriple(ldata: LineData, line: int): bool = if index >= ldata.lines.len: false else: ldata.lines[index] -proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode): string = +proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode, indent = 0): 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. var first = n.lastSon.info @@ -104,10 +107,9 @@ proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode): string = let last = n.lastNodeRec.info var info = first - var indent = info.col + var indent2 = info.col let numLines = numLines(conf, info.fileIndex).uint16 var lastNonemptyPos = 0 - result = "" var ldata = LineData(lineFirst: first.line.int, conf: conf) visitMultilineStrings(ldata, n[^1]) @@ -116,37 +118,20 @@ proc extractRunnableExamplesSource*(conf: ConfigRef; n: PNode): string = for i in 0..<ldata.lines.len: echo (i+ldata.lineFirst, ldata.lines[i]) + result = "" for line in first.line..numLines: # bugfix, see `testNimDocTrailingExample` info.line = line let src = sourceLine(conf, info) let special = startOfLineInsideTriple(ldata, line.int) - if line > last.line and not special and not isInIndentationBlock(src, indent): + if line > last.line and not special and not isInIndentationBlock(src, indent2): break if line > first.line: result.add "\n" if special: result.add src lastNonemptyPos = result.len - elif src.len > indent: - result.add src[indent..^1] + elif src.len > indent2: + for i in 0..<indent: result.add ' ' + result.add src[indent2..^1] lastNonemptyPos = result.len result.setLen lastNonemptyPos -proc renderNimCode*(result: var string, code: string, isLatex = false) = - var toknizr: GeneralTokenizer - initGeneralTokenizer(toknizr, code) - var buf = "" - template append(kind, val) = - buf.setLen 0 - buf.addEscaped(val) - let class = tokenClassToStr[kind] - if isLatex: - result.addf "\\span$1{$2}", [class, buf] - else: - result.addf "<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)) |