Skip to content

Commit 0990bc9

Browse files
a-mrAraq
authored andcommitted
docgen: implement cross-document links (nim-lang#20990)
* docgen: implement cross-document links Fully implements nim-lang/RFCs#125 Follow-up of: nim-lang#18642 (for internal links) and nim-lang#20127. Overview -------- Explicit import-like directive is required, called `.. importdoc::`. (the syntax is % RST, Markdown will use it for a while). Then one can reference any symbols/headings/anchors, as if they were in the local file (but they will be prefixed with a module name or markup document in link text). It's possible to reference anything from anywhere (any direction in `.nim`/`.md`/`.rst` files). See `doc/docgen.md` for full description. Working is based on `.idx` files, hence one needs to generate all `.idx` beforehand. A dedicated option `--index:only` is introduced (and a separate stage for `--index:only` is added to `kochdocs.nim`). Performance note ---------------- Full run for `./koch docs` now takes 185% of the time before this PR. (After: 315 s, before: 170 s on my PC). All the time seems to be spent on `--index:only` run, which takes almost as much (85%) of normal doc run -- it seems that most time is spent on file parsing, turning off HTML generation phase has not helped much. (One could avoid it by specifying list of files that can be referenced and pre-processing only them. But it can become error-prone and I assume that these linke will be **everywhere** in the repository anyway, especially considering nim-lang/RFCs#478. So every `.nim`/`.md` file is processed for `.idx` first). But that's all without significant part of repository converted to cross-module auto links. To estimate impact I checked the time for `doc`ing a few files (after all indexes have been generated), and everywhere difference was **negligible**. E.g. for `lib/std/private/osfiles.nim` that `importdoc`s large `os.idx` and hence should have been a case with relatively large performance impact, but: * After: 0.59 s. * Before: 0.59 s. So Nim compiler works so slow that doc part basically does not matter :-) Testing ------- 1) added `extlinks` test to `nimdoc/` 2) checked that `theindex.html` is still correct 2) fixed broken auto-links for modules that were derived from `os.nim` by adding appropriate ``importdoc`` Implementation note ------------------- Parsing and formating of `.idx` entries is moved into a dedicated `rstidx.nim` module from `rstgen.nim`. `.idx` file format changed: * fields are not escaped in most cases because we need original strings for referencing, not HTML ones (the exception is linkTitle for titles and headings). Escaping happens later -- on the stage of `rstgen` buildIndex, etc. * all lines have fixed number of columns 6 * added discriminator tag as a first column, it always allows distinguish Nim/markup entries, titles/headings, etc. `rstgen` does not rely any more (in most cases) on ad-hoc logic to determine what type each entry is. * there is now always a title entry added at the first line. * add a line number as 6th column * linkTitle (4th) column has a different format: before it was like `module: funcName()`, now it's `proc funcName()`. (This format is also propagated to `theindex.html` and search results, I kept it that way since I like it more though it's discussible.) This column is what used for Nim symbols resolution. * also changed details on column format for headings and titles: "keyword" is original, "linkTitle" is HTML one * fix paths on Windows + more clear code * Update compiler/docgen.nim Co-authored-by: Andreas Rumpf <[email protected]> * Handle .md and .nim paths uniformly in findRefFile * handle titles better + more comments * don't allow markup overwrite index title for .nim files Co-authored-by: Andreas Rumpf <[email protected]>
1 parent bdb035e commit 0990bc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1866
-494
lines changed

compiler/commands.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,13 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
820820
if conf != nil: conf.headerFile = arg
821821
incl(conf.globalOptions, optGenIndex)
822822
of "index":
823-
processOnOffSwitchG(conf, {optGenIndex}, arg, pass, info)
823+
case arg.normalize
824+
of "", "on": conf.globalOptions.incl {optGenIndex}
825+
of "only": conf.globalOptions.incl {optGenIndexOnly, optGenIndex}
826+
of "off": conf.globalOptions.excl {optGenIndex, optGenIndexOnly}
827+
else: localError(conf, info, errOnOrOffExpectedButXFound % arg)
828+
of "noimportdoc":
829+
processOnOffSwitchG(conf, {optNoImportdoc}, arg, pass, info)
824830
of "import":
825831
expectArg(conf, switch, arg, pass, info)
826832
if pass in {passCmd2, passPP}:

compiler/docgen.nim

Lines changed: 145 additions & 55 deletions
Large diffs are not rendered by default.

compiler/lineinfos.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type
5757
warnRstBrokenLink = "BrokenLink",
5858
warnRstLanguageXNotSupported = "LanguageXNotSupported",
5959
warnRstFieldXNotSupported = "FieldXNotSupported",
60+
warnRstUnusedImportdoc = "UnusedImportdoc",
6061
warnRstStyle = "warnRstStyle",
6162
warnCommentXIgnored = "CommentXIgnored",
6263
warnTypelessParam = "TypelessParam",
@@ -142,6 +143,7 @@ const
142143
warnRstBrokenLink: "broken link '$1'",
143144
warnRstLanguageXNotSupported: "language '$1' not supported",
144145
warnRstFieldXNotSupported: "field '$1' not supported",
146+
warnRstUnusedImportdoc: "importdoc for '$1' is not used",
145147
warnRstStyle: "RST style: $1",
146148
warnCommentXIgnored: "comment '$1' ignored",
147149
warnTypelessParam: "", # deadcode

compiler/options.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ type # please make sure we have under 32 options
7878
optThreadAnalysis, # thread analysis pass
7979
optTlsEmulation, # thread var emulation turned on
8080
optGenIndex # generate index file for documentation;
81+
optGenIndexOnly # generate only index file for documentation
82+
optNoImportdoc # disable loading external documentation files
8183
optEmbedOrigSrc # embed the original source in the generated code
8284
# also: generate header file
8385
optIdeDebug # idetools: debug mode

doc/advopt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ Advanced options:
130130
select which memory management to use; default is 'orc'
131131
--exceptions:setjmp|cpp|goto|quirky
132132
select the exception handling implementation
133-
--index:on|off turn index file generation on|off
133+
--index:on|off|only docgen: turn index file generation? (`only` means
134+
not generate output files like HTML)
135+
--noImportdoc:on|off disable loading documentation ``.idx`` files?
134136
--putenv:key=value set an environment variable
135137
--NimblePath:PATH add a path for Nimble support
136138
--noNimblePath deactivate the Nimble path

0 commit comments

Comments
 (0)