documentation generator emits configurable html for code listings

This commit is contained in:
Araq 2015-07-14 01:36:49 +02:00
commit f7bdc205c7
3 changed files with 17 additions and 6 deletions

View file

@ -86,6 +86,9 @@ $moduledesc
$content $content
""" """
doc.listing_start = "<pre class = \"listing\">"
doc.listing_end = "</pre>"
# * $analytics: Google analytics location, includes <script> tags # * $analytics: Google analytics location, includes <script> tags
doc.file = """<?xml version="1.0" encoding="utf-8" ?> doc.file = """<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

View file

@ -46,6 +46,7 @@ type
target*: OutputTarget target*: OutputTarget
config*: StringTableRef config*: StringTableRef
splitAfter*: int # split too long entries in the TOC splitAfter*: int # split too long entries in the TOC
listingCounter: int
tocPart*: seq[TocEntry] tocPart*: seq[TocEntry]
hasToc*: bool hasToc*: bool
theIndex: string # Contents of the index file to be dumped at the end. theIndex: string # Contents of the index file to be dumped at the end.
@ -832,7 +833,7 @@ proc parseCodeBlockParams(d: PDoc, n: PRstNode): CodeBlockParams =
if result.langStr != "": if result.langStr != "":
result.lang = getSourceLanguage(result.langStr) result.lang = getSourceLanguage(result.langStr)
proc buildLinesHTMLTable(params: CodeBlockParams, code: string): proc buildLinesHTMLTable(d: PDoc; params: CodeBlockParams, code: string):
tuple[beginTable, endTable: string] = tuple[beginTable, endTable: string] =
## Returns the necessary tags to start/end a code block in HTML. ## Returns the necessary tags to start/end a code block in HTML.
## ##
@ -840,20 +841,24 @@ proc buildLinesHTMLTable(params: CodeBlockParams, code: string):
## <pre> pair. Otherwise it will build a table and insert an initial column ## <pre> pair. Otherwise it will build a table and insert an initial column
## with all the line numbers, which requires you to pass the `code` to detect ## with all the line numbers, which requires you to pass the `code` to detect
## how many lines have to be generated (and starting at which point!). ## how many lines have to be generated (and starting at which point!).
inc d.listingCounter
let id = $d.listingCounter
if not params.numberLines: if not params.numberLines:
result = ("<pre>", "</pre>") result = (d.config["doc.listing_start"] % id,
d.config["doc.listing_end"] % id)
return return
var codeLines = 1 + code.strip.countLines var codeLines = 1 + code.strip.countLines
assert codeLines > 0 assert codeLines > 0
result.beginTable = """<table class="line-nums-table"><tbody><tr><td class="blob-line-nums"><pre>""" result.beginTable = """<table class="line-nums-table"><tbody><tr><td class="blob-line-nums"><pre class="line-nums">"""
var line = params.startLine var line = params.startLine
while codeLines > 0: while codeLines > 0:
result.beginTable.add($line & "\n") result.beginTable.add($line & "\n")
line.inc line.inc
codeLines.dec codeLines.dec
result.beginTable.add("</pre></td><td><pre>") result.beginTable.add("</pre></td><td>" & (d.config["doc.listing_start"] % id))
result.endTable = "</pre></td></tr></tbody></table>" result.endTable = (d.config["doc.listing_end"] % id) &
"</td></tr></tbody></table>" & (d.config["doc.listing_button"] % id)
proc renderCodeBlock(d: PDoc, n: PRstNode, result: var string) = proc renderCodeBlock(d: PDoc, n: PRstNode, result: var string) =
## Renders a code block, appending it to `result`. ## Renders a code block, appending it to `result`.
@ -871,7 +876,7 @@ proc renderCodeBlock(d: PDoc, n: PRstNode, result: var string) =
var m = n.sons[2].sons[0] var m = n.sons[2].sons[0]
assert m.kind == rnLeaf assert m.kind == rnLeaf
let (blockStart, blockEnd) = params.buildLinesHTMLTable(m.text) let (blockStart, blockEnd) = buildLinesHTMLTable(d, params, m.text)
dispA(d.target, result, blockStart, "\\begin{rstpre}\n", []) dispA(d.target, result, blockStart, "\\begin{rstpre}\n", [])
if params.lang == langNone: if params.lang == langNone:
@ -1209,6 +1214,9 @@ $moduledesc
$content $content
</div> </div>
""") """)
setConfigVar("doc.listing_start", "<pre class = \"listing\">")
setConfigVar("doc.listing_end", "</pre>")
setConfigVar("doc.listing_button", "</pre>")
setConfigVar("doc.body_no_toc", "$moduledesc $content") setConfigVar("doc.body_no_toc", "$moduledesc $content")
setConfigVar("doc.file", "$content") setConfigVar("doc.file", "$content")
setConfigVar("doc.smiley_format", "/images/smilies/$1.gif") setConfigVar("doc.smiley_format", "/images/smilies/$1.gif")