nim doc can run code blocks (#9228)
This commit is contained in:
parent
505ae14f4b
commit
d36f98fa8b
1 changed files with 32 additions and 24 deletions
|
|
@ -141,9 +141,30 @@ proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
|
||||||
result.id = 100
|
result.id = 100
|
||||||
result.jArray = newJArray()
|
result.jArray = newJArray()
|
||||||
initStrTable result.types
|
initStrTable result.types
|
||||||
result.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string; status: int; content: string) =
|
result.onTestSnippet =
|
||||||
localError(conf, newLineInfo(conf, AbsoluteFile d.filename, -1, -1),
|
proc (gen: var RstGenerator; filename, cmd: string; status: int; content: string) =
|
||||||
warnUser, "only 'rst2html' supports the ':test:' attribute")
|
var d = TDocumentor(gen)
|
||||||
|
var outp: AbsoluteFile
|
||||||
|
if filename.len == 0:
|
||||||
|
inc(d.id)
|
||||||
|
let nameOnly = splitFile(d.filename).name
|
||||||
|
let subdir = getNimcacheDir(conf) / RelativeDir(nameOnly)
|
||||||
|
createDir(subdir)
|
||||||
|
outp = subdir / RelativeFile(nameOnly & "_snippet_" & $d.id & ".nim")
|
||||||
|
elif isAbsolute(filename):
|
||||||
|
outp = AbsoluteFile filename
|
||||||
|
else:
|
||||||
|
# Nim's convention: every path is relative to the file it was written in:
|
||||||
|
outp = splitFile(d.filename).dir.AbsoluteDir / RelativeFile(filename)
|
||||||
|
# Include the current file if we're parsing a nim file
|
||||||
|
let importStmt = if d.isPureRst: "" else: "import \"$1\"\n" % [d.filename]
|
||||||
|
writeFile(outp, importStmt & content)
|
||||||
|
let c = if cmd.startsWith("nim "): os.getAppFilename() & cmd.substr(3)
|
||||||
|
else: cmd
|
||||||
|
let c2 = c % quoteShell(outp)
|
||||||
|
rawMessage(conf, hintExecuting, c2)
|
||||||
|
if execShellCmd(c2) != status:
|
||||||
|
rawMessage(conf, errGenerated, "executing of external program failed: " & c2)
|
||||||
result.emitted = initIntSet()
|
result.emitted = initIntSet()
|
||||||
result.destFile = getOutFile2(conf, relativeTo(filename, conf.projectPath),
|
result.destFile = getOutFile2(conf, relativeTo(filename, conf.projectPath),
|
||||||
outExt, RelativeDir"htmldocs", false)
|
outExt, RelativeDir"htmldocs", false)
|
||||||
|
|
@ -961,27 +982,6 @@ proc commandRstAux(cache: IdentCache, conf: ConfigRef;
|
||||||
filename: AbsoluteFile, outExt: string) =
|
filename: AbsoluteFile, outExt: string) =
|
||||||
var filen = addFileExt(filename, "txt")
|
var filen = addFileExt(filename, "txt")
|
||||||
var d = newDocumentor(filen, cache, conf, outExt)
|
var d = newDocumentor(filen, cache, conf, outExt)
|
||||||
d.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string;
|
|
||||||
status: int; content: string) =
|
|
||||||
var outp: AbsoluteFile
|
|
||||||
if filename.len == 0:
|
|
||||||
inc(d.id)
|
|
||||||
let nameOnly = splitFile(d.filename).name
|
|
||||||
let subdir = getNimcacheDir(conf) / RelativeDir(nameOnly)
|
|
||||||
createDir(subdir)
|
|
||||||
outp = subdir / RelativeFile(nameOnly & "_snippet_" & $d.id & ".nim")
|
|
||||||
elif isAbsolute(filename):
|
|
||||||
outp = AbsoluteFile filename
|
|
||||||
else:
|
|
||||||
# Nim's convention: every path is relative to the file it was written in:
|
|
||||||
outp = splitFile(d.filename).dir.AbsoluteDir / RelativeFile(filename)
|
|
||||||
writeFile(outp, content)
|
|
||||||
let c = if cmd.startsWith("nim "): os.getAppFilename() & cmd.substr(3)
|
|
||||||
else: cmd
|
|
||||||
let c2 = c % quoteShell(outp)
|
|
||||||
rawMessage(conf, hintExecuting, c2)
|
|
||||||
if execShellCmd(c2) != status:
|
|
||||||
rawMessage(conf, errGenerated, "executing of external program failed: " & c2)
|
|
||||||
|
|
||||||
d.isPureRst = true
|
d.isPureRst = true
|
||||||
var rst = parseRst(readFile(filen.string), filen.string, 0, 1, d.hasToc,
|
var rst = parseRst(readFile(filen.string), filen.string, 0, 1, d.hasToc,
|
||||||
|
|
@ -1002,6 +1002,10 @@ proc commandJson*(cache: IdentCache, conf: ConfigRef) =
|
||||||
var ast = parseFile(conf.projectMainIdx, cache, conf)
|
var ast = parseFile(conf.projectMainIdx, cache, conf)
|
||||||
if ast == nil: return
|
if ast == nil: return
|
||||||
var d = newDocumentor(conf.projectFull, cache, conf)
|
var d = newDocumentor(conf.projectFull, cache, conf)
|
||||||
|
d.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string;
|
||||||
|
status: int; content: string) =
|
||||||
|
localError(conf, newLineInfo(conf, AbsoluteFile d.filename, -1, -1),
|
||||||
|
warnUser, "the ':test:' attribute is not supported by this backend")
|
||||||
d.hasToc = true
|
d.hasToc = true
|
||||||
generateJson(d, ast)
|
generateJson(d, ast)
|
||||||
let json = d.jArray
|
let json = d.jArray
|
||||||
|
|
@ -1019,6 +1023,10 @@ proc commandTags*(cache: IdentCache, conf: ConfigRef) =
|
||||||
var ast = parseFile(conf.projectMainIdx, cache, conf)
|
var ast = parseFile(conf.projectMainIdx, cache, conf)
|
||||||
if ast == nil: return
|
if ast == nil: return
|
||||||
var d = newDocumentor(conf.projectFull, cache, conf)
|
var d = newDocumentor(conf.projectFull, cache, conf)
|
||||||
|
d.onTestSnippet = proc (d: var RstGenerator; filename, cmd: string;
|
||||||
|
status: int; content: string) =
|
||||||
|
localError(conf, newLineInfo(conf, AbsoluteFile d.filename, -1, -1),
|
||||||
|
warnUser, "the ':test:' attribute is not supported by this backend")
|
||||||
d.hasToc = true
|
d.hasToc = true
|
||||||
var
|
var
|
||||||
content: Rope
|
content: Rope
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue