docgen.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. ===================================
  2. Nim DocGen Tools Guide
  3. ===================================
  4. :Author: Erik O'Leary
  5. :Version: |nimversion|
  6. .. default-role:: code
  7. .. include:: rstcommon.rst
  8. .. contents::
  9. Introduction
  10. ============
  11. This document describes the `documentation generation tools`:idx: built into
  12. the `Nim compiler <nimc.html>`_, which can generate HTML, Latex and JSON output
  13. from input ``.nim`` files and projects, as well as HTML and LaTeX from input RST
  14. (reStructuredText) files. The output documentation will include the module
  15. dependencies (`import`), any top-level documentation comments (`##`), and
  16. exported symbols (`*`), including procedures, types, and variables.
  17. =================== ====================== ============ ==============
  18. command runs on... input format output format
  19. =================== ====================== ============ ==============
  20. `nim doc`:cmd: documentation comments ``.nim`` ``.html`` HTML
  21. `nim doc2tex`:cmd: ″ ″ ``.tex`` LaTeX
  22. `nim jsondoc`:cmd: ″ ″ ``.json`` JSON
  23. `nim rst2html`:cmd: standalone rst files ``.rst`` ``.html`` HTML
  24. `nim rst2tex`:cmd: ″ ″ ``.tex`` LaTeX
  25. =================== ====================== ============ ==============
  26. Quick start
  27. -----------
  28. Generate HTML documentation for a file:
  29. .. code:: cmd
  30. nim doc <filename>.nim
  31. Generate HTML documentation for a whole project:
  32. .. code:: cmd
  33. # delete any htmldocs/*.idx file before starting
  34. nim doc --project --index:on --git.url:<url> --git.commit:<tag> --outdir:htmldocs <main_filename>.nim
  35. # this will generate html files, a theindex.html index, css and js under `htmldocs`
  36. # See also `--docroot` to specify a relative root.
  37. # to get search (dochacks.js) to work locally, you need a server otherwise
  38. # CORS will prevent opening file:// urls; this works:
  39. python3 -m http.server 7029 --directory htmldocs
  40. # When --outdir is omitted it defaults to $projectPath/htmldocs,
  41. # or `$nimcache/htmldocs` with `--usenimcache` which avoids clobbering your sources;
  42. # and likewise without `--project`.
  43. # Adding `-r` will open in a browser directly.
  44. Documentation Comments
  45. ----------------------
  46. Any comments which are preceded by a double-hash (`##`), are interpreted as
  47. documentation. Comments are parsed as RST (see `reference
  48. <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_), providing
  49. Nim module authors the ability to easily generate richly formatted
  50. documentation with only their well-documented code!
  51. Basic Markdown syntax is also supported inside the doc comments.
  52. Example:
  53. .. code-block:: nim
  54. type Person* = object
  55. ## This type contains a description of a person
  56. name: string
  57. age: int
  58. Outputs::
  59. Person* = object
  60. name: string
  61. age: int
  62. This type contains a description of a person
  63. Field documentation comments can be added to fields like so:
  64. .. code-block:: nim
  65. var numValues: int ## \
  66. ## `numValues` stores the number of values
  67. Note that without the `*` following the name of the type, the documentation for
  68. this type would not be generated. Documentation will only be generated for
  69. *exported* types/procedures/etc.
  70. It's recommended to always add exactly **one** space after `##` for readability
  71. of comments — this extra space will be cropped from the parsed comments and
  72. won't influence RST formatting.
  73. .. note:: Generally, this baseline indentation level inside a documentation
  74. comment may not be 1: it can be any since it is determined by the offset
  75. of the first non-whitespace character in the comment.
  76. After that indentation **must** be consistent on the following lines of
  77. the same comment.
  78. If you still need to add an additional indentation at the very beginning
  79. (for RST block quote syntax) use backslash \\ before it:
  80. .. code-block:: nim
  81. ## \
  82. ##
  83. ## Block quote at the first line.
  84. ##
  85. ## Paragraph.
  86. Document Types
  87. ==============
  88. Example of Nim file input
  89. -------------------------
  90. The following examples will generate documentation for this sample
  91. *Nim* module, aptly named ``doc/docgen_sample.nim``:
  92. .. code:: nim
  93. :file: docgen_sample.nim
  94. All the below commands save their output to ``htmldocs`` directory relative to
  95. the directory of file;
  96. hence the output for this sample will be in ``doc/htmldocs``.
  97. HTML
  98. ----
  99. The generation of HTML documents is done via the `doc`:option: command. This command
  100. takes either a single ``.nim`` file, outputting a single ``.html`` file with the same
  101. base filename, or multiple ``.nim`` files, outputting multiple ``.html`` files and,
  102. optionally, an index file.
  103. The `doc`:option: command:
  104. .. code:: cmd
  105. nim doc docgen_sample.nim
  106. Partial Output::
  107. ...
  108. proc helloWorld(times: int) {.raises: [], tags: [].}
  109. ...
  110. The full output can be seen here: `docgen_sample.html <docgen_sample.html>`_.
  111. It runs after semantic checking and includes pragmas attached implicitly by the
  112. compiler.
  113. LaTeX
  114. -----
  115. LaTeX files are intended to be converted to PDF, especially for offline
  116. reading or making hard copies. (LaTeX output is oftentimes better than
  117. HTML -> PDF conversion).
  118. The `doc2tex`:option: command:
  119. .. code:: cmd
  120. nim doc2tex docgen_sample.nim
  121. cd htmldocs
  122. xelatex docgen_sample.tex
  123. xelatex docgen_sample.tex
  124. # It is usually necessary to run `xelatex` 2 times (or even 3 times for
  125. # large documents) to get all labels generated.
  126. # That depends on this warning in the end of `xelatex` output:
  127. # LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
  128. The output is ``docgen_sample.pdf``.
  129. JSON
  130. ----
  131. The generation of JSON documents is done via the `jsondoc`:option: command.
  132. This command takes in a ``.nim`` file and outputs a ``.json`` file with
  133. the same base filename.
  134. Note that this tool is built off of the `doc`:option: command
  135. (previously `doc2`:option:), and contains the same information.
  136. The `jsondoc`:option: command:
  137. .. code:: cmd
  138. nim jsondoc docgen_sample.nim
  139. Output::
  140. {
  141. "orig": "docgen_sample.nim",
  142. "nimble": "",
  143. "moduleDescription": "This module is a sample",
  144. "entries": [
  145. {
  146. "name": "helloWorld",
  147. "type": "skProc",
  148. "line": 5,
  149. "col": 0,
  150. "description": "Takes an integer and outputs as many &quot;hello world!&quot;s",
  151. "code": "proc helloWorld(times: int) {.raises: [], tags: [].}"
  152. }
  153. ]
  154. }
  155. Similarly to the old `doc`:option: command, the old `jsondoc`:option: command has been
  156. renamed to `jsondoc0`:option:.
  157. The `jsondoc0`:option: command:
  158. .. code:: cmd
  159. nim jsondoc0 docgen_sample.nim
  160. Output::
  161. [
  162. {
  163. "comment": "This module is a sample."
  164. },
  165. {
  166. "name": "helloWorld",
  167. "type": "skProc",
  168. "description": "Takes an integer and outputs as many &quot;hello world!&quot;s",
  169. "code": "proc helloWorld*(times: int)"
  170. }
  171. ]
  172. Note that the `jsondoc`:option: command outputs its JSON without pretty-printing it,
  173. while `jsondoc0`:option: outputs pretty-printed JSON.
  174. Referencing Nim symbols: simple documentation links
  175. ===================================================
  176. You can reference Nim identifiers from Nim documentation comments, currently
  177. only inside their ``.nim`` file (or inside a ``.rst`` file included from
  178. a ``.nim``). The point is that such links will be resolved automatically
  179. by `nim doc`:cmd: (or `nim jsondoc`:cmd: or `nim doc2tex`:cmd:).
  180. This pertains to any exported symbol like `proc`, `const`, `iterator`, etc.
  181. Syntax for referencing is basically a normal RST one: addition of
  182. underscore `_` to a *link text*.
  183. Link text is either one word or a group of words enclosed by backticks `\``
  184. (for a one word case backticks are usually omitted).
  185. Link text will be displayed *as is* while *link target* will be set to
  186. the anchor [*]_ of Nim symbol that corresponds to link text.
  187. .. [*] anchors' format is described in `HTML anchor generation`_ section below.
  188. If you have a constant:
  189. .. code:: Nim
  190. const pi* = 3.14
  191. then it should be referenced in one of the 2 forms:
  192. A. non-qualified (no symbol kind specification)::
  193. pi_
  194. B. qualified (with symbol kind specification)::
  195. `const pi`_
  196. For routine kinds there are more options. Consider this definition:
  197. .. code:: Nim
  198. proc foo*(a: int, b: float): string
  199. Generally following syntax is allowed for referencing `foo`:
  200. * short (without parameters):
  201. A. non-qualified::
  202. foo_
  203. B. qualified::
  204. `proc foo`_
  205. * longer variants (with parameters):
  206. A. non-qualified:
  207. 1) specifying parameters names::
  208. `foo(a, b)`_
  209. 2) specifying parameters types::
  210. `foo(int, float)`_
  211. 3) specifying both names and types::
  212. `foo(a: int, b: float)`_
  213. 4) output parameter can also be specified if you wish::
  214. `foo(a: int, b: float): string`_
  215. B. qualified: all 4 options above are valid.
  216. Particularly you can use the full format::
  217. `proc foo(a: int, b: float): string`_
  218. .. Tip:: Avoid cluttering your text with extraneous information by using
  219. one of shorter forms::
  220. binarySearch_
  221. `binarySearch(a, key, cmp)`_
  222. Brevity is better for reading! If you use a short form and have an
  223. ambiguity problem (see below) then just add some additional info.
  224. Symbol kind like `proc` can also be specified in the postfix form::
  225. `foo proc`_
  226. `walkDir(d: string) iterator`_
  227. .. Warning:: An ambiguity in resolving documentation links may arise because of:
  228. 1. clash with other RST anchors
  229. * manually setup anchors
  230. * automatically set up, e.g. section names
  231. 2. collision with other Nim symbols:
  232. * routines with different parameters can exist e.g. for
  233. `proc` and `template`. In this case they are split between their
  234. corresponding sections in output file. Qualified references are
  235. useful in this case -- just disambiguate by referring to these
  236. sections explicitly::
  237. See `foo proc`_ and `foo template`_.
  238. * because in Nim `proc` and `iterator` belong to different namespaces,
  239. so there can be a collision even if parameters are the same.
  240. Use `\`proc foo\`_`:literal: or `\`iterator foo\`_`:literal: then.
  241. Any ambiguity is always reported with Nim compiler warnings and an anchor
  242. with higher priority is selected. Manual anchors have highest
  243. priority, then go automatic RST anchors; then Nim-generated anchors
  244. (while procs have higher priority than other Nim symbol kinds).
  245. Generic parameters can also be used. All in all, this long form will be
  246. recognized fine::
  247. `proc binarySearch*[T; K](a: openArray[T], key: K, cmp: proc(T, K)): int`_
  248. **Limitations**:
  249. 1. The parameters of a nested routine type can be specified only with types
  250. (without parameter names, see form A.2 above).
  251. E.g. for this signature:
  252. .. code:: Nim
  253. proc binarySearch*[T, K](a: openArray[T]; key: K;
  254. cmp: proc (x: T; y: K): int {.closure.}): int
  255. ~~ ~~ ~~~~~
  256. you cannot use names underlined by `~~` so it must be referenced with
  257. ``cmp: proc(T, K)``. Hence these forms are valid::
  258. `binarySearch(a: openArray[T], key: K, cmp: proc(T, K))`_
  259. `binarySearch(openArray[T], K, proc(T, K))`_
  260. `binarySearch(a, key, cmp)`_
  261. 2. Default values in routine parameters are not recognized, one needs to
  262. specify the type and/or name instead. E.g. for referencing `proc f(x = 7)`
  263. use one of the mentioned forms::
  264. `f(int)`_ or `f(x)`_ or `f(x: int)`_.
  265. 3. Generic parameters must be given the same way as in the
  266. definition of referenced symbol.
  267. * their names should be the same
  268. * parameters list should be given the same way, e.g. without substitutions
  269. between commas (,) and semicolons (;).
  270. .. Note:: A bit special case is operators
  271. (as their signature is also defined with `\``):
  272. .. code:: Nim
  273. func `$`(x: MyType): string
  274. func `[]`*[T](x: openArray[T]): T
  275. A short form works without additional backticks::
  276. `$`_
  277. `[]`_
  278. However for fully-qualified reference copy-pasting backticks (`) into other
  279. backticks will not work in our RST parser (because we use Markdown-like
  280. inline markup rules). You need either to delete backticks or keep
  281. them and escape with backslash \\::
  282. no backticks: `func $`_
  283. escaped: `func \`$\``_
  284. no backticks: `func [][T](x: openArray[T]): T`_
  285. escaped: `func \`[]\`[T](x: openArray[T]): T`_
  286. Related Options
  287. ===============
  288. Project switch
  289. --------------
  290. .. code:: cmd
  291. nim doc --project filename.nim
  292. This will recursively generate documentation of all Nim modules imported
  293. into the input module that belong to the Nimble package that ``filename.nim``
  294. belongs to. The index files and the corresponding ``theindex.html`` will
  295. also be generated.
  296. Index switch
  297. ------------
  298. .. code:: cmd
  299. nim doc --index:on filename.nim
  300. This will generate an index of all the exported symbols in the input Nim
  301. module, and put it into a neighboring file with the extension of ``.idx``. The
  302. index file is line-oriented (newlines have to be escaped). Each line
  303. represents a tab-separated record of several columns, the first two mandatory,
  304. the rest optional. See the `Index (idx) file format`_ section for details.
  305. Once index files have been generated for one or more modules, the Nim
  306. compiler command `buildIndex directory` can be run to go over all the index
  307. files in the specified directory to generate a `theindex.html <theindex.html>`_
  308. file.
  309. See source switch
  310. -----------------
  311. .. code:: cmd
  312. nim doc --git.url:<url> filename.nim
  313. With the `git.url`:option: switch the *See source* hyperlink will appear below each
  314. documented item in your source code pointing to the implementation of that
  315. item on a GitHub repository.
  316. You can click the link to see the implementation of the item.
  317. The `git.commit`:option: switch overrides the hardcoded `devel` branch in
  318. ``config/nimdoc.cfg``.
  319. This is useful to link to a different branch e.g. `--git.commit:master`:option:,
  320. or to a tag e.g. `--git.commit:1.2.3`:option: or a commit.
  321. Source URLs are generated as ``href="${url}/tree/${commit}/${path}#L${line}"``
  322. by default and thus compatible with GitHub but not with GitLab.
  323. Similarly, `git.devel`:option: switch overrides the hardcoded `devel` branch
  324. for the `Edit` link which is also useful if you have a different working
  325. branch than `devel` e.g. `--git.devel:master`:option:.
  326. Edit URLs are generated as ``href="${url}/tree/${devel}/${path}#L${line}"``
  327. by default.
  328. You can edit ``config/nimdoc.cfg`` and modify the ``doc.item.seesrc`` value
  329. with a hyperlink to your own code repository.
  330. In the case of Nim's own documentation, the `commit` value is just a commit
  331. hash to append to a formatted URL to https://github.com/nim-lang/Nim. The
  332. ``tools/nimweb.nim`` helper queries the current git commit hash during the doc
  333. generation, but since you might be working on an unpublished repository, it
  334. also allows specifying a `githash` value in ``web/website.ini`` to force a
  335. specific commit in the output.
  336. Other Input Formats
  337. ===================
  338. The *Nim compiler* also has support for RST (reStructuredText) files with
  339. the `rst2html`:option: and `rst2tex`:option: commands. Documents like this one are
  340. initially written in a dialect of RST which adds support for Nim source code
  341. highlighting with the ``.. code-block:: nim`` prefix. ``code-block`` also
  342. supports highlighting of a few other languages supported by the
  343. `packages/docutils/highlite module <highlite.html>`_.
  344. Usage:
  345. .. code:: cmd
  346. nim rst2html docgen.rst
  347. Output::
  348. You're reading it!
  349. The `rst2tex`:option: command is invoked identically to `rst2html`:option:,
  350. but outputs a ``.tex`` file instead of ``.html``.
  351. HTML anchor generation
  352. ======================
  353. When you run the `rst2html`:option: command, all sections in the RST document will
  354. get an anchor you can hyperlink to. Usually, you can guess the anchor lower
  355. casing the section title and replacing spaces with dashes, and in any case, you
  356. can get it from the table of contents. But when you run the `doc`:option:
  357. command to generate API documentation, some symbol get one or two anchors at
  358. the same time: a numerical identifier, or a plain name plus a complex name.
  359. The numerical identifier is just a random number. The number gets assigned
  360. according to the section and position of the symbol in the file being processed
  361. and you should not rely on it being constant: if you add or remove a symbol the
  362. numbers may shuffle around.
  363. The plain name of a symbol is a simplified version of its fully exported
  364. signature. Variables or constants have the same plain name symbol as their
  365. complex name. The plain name for procs, templates, and other callable types
  366. will be their unquoted value after removing parameters, return types, and
  367. pragmas. The plain name allows short and nice linking of symbols that works
  368. unless you have a module with collisions due to overloading.
  369. If you hyperlink a plain name symbol and there are other matches on the same
  370. HTML file, most browsers will go to the first one. To differentiate the rest,
  371. you will need to use the complex name. A complex name for a callable type is
  372. made up of several parts:
  373. (**plain symbol**)(**.type**),(**first param**)?(**,param type**)\*
  374. The first thing to note is that all callable types have at least a comma, even
  375. if they don't have any parameters. If there are parameters, they are
  376. represented by their types and will be comma-separated. To the plain symbol a
  377. suffix may be added depending on the type of the callable:
  378. ============== ==============
  379. Callable type Suffix
  380. ============== ==============
  381. `proc`, `func` *empty string*
  382. `macro` ``.m``
  383. `method` ``.e``
  384. `iterator` ``.i``
  385. `template` ``.t``
  386. `converter` ``.c``
  387. ============== ==============
  388. The relationship of type to suffix is made by the proc `complexName` in the
  389. ``compiler/docgen.nim`` file. Here are some examples of complex names for
  390. symbols in the `system module <system.html>`_.
  391. * `type SomeSignedInt = int | int8 | int16 | int32 | int64` **=>**
  392. `#SomeSignedInt <system.html#SomeSignedInt>`_
  393. * `var globalRaiseHook: proc (e: ref E_Base): bool {.nimcall.}` **=>**
  394. `#globalRaiseHook <system.html#globalRaiseHook>`_
  395. * `const NimVersion = "0.0.0"` **=>**
  396. `#NimVersion <system.html#NimVersion>`_
  397. * `proc getTotalMem(): int {.rtl, raises: [], tags: [].}` **=>**
  398. `#getTotalMem, <system.html#getTotalMem>`_
  399. * `proc len[T](x: seq[T]): int {.magic: "LengthSeq", noSideEffect.}` **=>**
  400. `#len,seq[T] <system.html#len,seq[T]>`_
  401. * `iterator pairs[T](a: seq[T]): tuple[key: int, val: T] {.inline.}` **=>**
  402. `#pairs.i,seq[T] <iterators.html#pairs.i,seq[T]>`_
  403. * `template newException[](exceptn: typedesc; message: string;
  404. parentException: ref Exception = nil): untyped` **=>**
  405. `#newException.t,typedesc,string,ref.Exception
  406. <system.html#newException.t,typedesc,string,ref.Exception>`_
  407. Index (idx) file format
  408. =======================
  409. Files with the ``.idx`` extension are generated when you use the `Index
  410. switch <#related-options-index-switch>`_ along with commands to generate
  411. documentation from source or text files. You can programmatically generate
  412. indices with the `setIndexTerm()
  413. <rstgen.html#setIndexTerm,RstGenerator,string,string,string,string,string>`_
  414. and `writeIndexFile() <rstgen.html#writeIndexFile,RstGenerator,string>`_ procs.
  415. The purpose of `idx` files is to hold the interesting symbols and their HTML
  416. references so they can be later concatenated into a big index file with
  417. `mergeIndexes() <rstgen.html#mergeIndexes,string>`_. This section documents
  418. the file format in detail.
  419. Index files are line-oriented and tab-separated (newline and tab characters
  420. have to be escaped). Each line represents a record with at least two fields
  421. but can have up to four (additional columns are ignored). The content of these
  422. columns is:
  423. 1. Mandatory term being indexed. Terms can include quoting according to
  424. Nim's rules (e.g. \`^\`).
  425. 2. Base filename plus anchor hyperlink (e.g. ``algorithm.html#*,int,SortOrder``).
  426. 3. Optional human-readable string to display as a hyperlink. If the value is not
  427. present or is the empty string, the hyperlink will be rendered
  428. using the term. Prefix whitespace indicates that this entry is
  429. not for an API symbol but for a TOC entry.
  430. 4. Optional title or description of the hyperlink. Browsers usually display
  431. this as a tooltip after hovering a moment over the hyperlink.
  432. The index generation tools try to differentiate between documentation
  433. generated from ``.nim`` files and documentation generated from ``.txt`` or
  434. ``.rst`` files. The former are always closely related to source code and
  435. consist mainly of API entries. The latter are generic documents meant for
  436. human reading.
  437. To differentiate both types (documents and APIs), the index generator will add
  438. to the index of documents an entry with the title of the document. Since the
  439. title is the topmost element, it will be added with a second field containing
  440. just the filename without any HTML anchor. By convention, this entry without
  441. anchor is the *title entry*, and since entries in the index file are added as
  442. they are scanned, the title entry will be the first line. The title for APIs
  443. is not present because it can be generated concatenating the name of the file
  444. to the word **Module**.
  445. Normal symbols are added to the index with surrounding whitespaces removed. An
  446. exception to this are the table of content (TOC) entries. TOC entries are added to
  447. the index file with their third column having as much prefix spaces as their
  448. level is in the TOC (at least 1 character). The prefix whitespace helps to
  449. filter TOC entries from API or text symbols. This is important because the
  450. amount of spaces is used to replicate the hierarchy for document TOCs in the
  451. final index, and TOC entries found in ``.nim`` files are discarded.
  452. Additional resources
  453. ====================
  454. * `Nim Compiler User Guide <nimc.html#compiler-usage-commandminusline-switches>`_
  455. * Documentation for `rst module <rst.html>`_ -- Nim RST/Markdown parser.
  456. * `RST Quick Reference
  457. <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
  458. The output for HTML and LaTeX comes from the ``config/nimdoc.cfg`` and
  459. ``config/nimdoc.tex.cfg`` configuration files. You can add and modify these
  460. files to your project to change the look of the docgen output.
  461. You can import the `packages/docutils/rstgen module <rstgen.html>`_ in your
  462. programs if you want to reuse the compiler's documentation generation procs.