docgen.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. Related Options
  175. ===============
  176. Project switch
  177. --------------
  178. .. code:: cmd
  179. nim doc --project filename.nim
  180. This will recursively generate documentation of all Nim modules imported
  181. into the input module that belong to the Nimble package that ``filename.nim``
  182. belongs to. The index files and the corresponding ``theindex.html`` will
  183. also be generated.
  184. Index switch
  185. ------------
  186. .. code:: cmd
  187. nim doc --index:on filename.nim
  188. This will generate an index of all the exported symbols in the input Nim
  189. module, and put it into a neighboring file with the extension of ``.idx``. The
  190. index file is line-oriented (newlines have to be escaped). Each line
  191. represents a tab-separated record of several columns, the first two mandatory,
  192. the rest optional. See the `Index (idx) file format`_ section for details.
  193. Once index files have been generated for one or more modules, the Nim
  194. compiler command `buildIndex directory` can be run to go over all the index
  195. files in the specified directory to generate a `theindex.html <theindex.html>`_
  196. file.
  197. See source switch
  198. -----------------
  199. .. code:: cmd
  200. nim doc --git.url:<url> filename.nim
  201. With the `git.url`:option: switch the *See source* hyperlink will appear below each
  202. documented item in your source code pointing to the implementation of that
  203. item on a GitHub repository.
  204. You can click the link to see the implementation of the item.
  205. The `git.commit`:option: switch overrides the hardcoded `devel` branch in
  206. ``config/nimdoc.cfg``.
  207. This is useful to link to a different branch e.g. `--git.commit:master`:option:,
  208. or to a tag e.g. `--git.commit:1.2.3`:option: or a commit.
  209. Source URLs are generated as ``href="${url}/tree/${commit}/${path}#L${line}"``
  210. by default and thus compatible with GitHub but not with GitLab.
  211. Similarly, `git.devel`:option: switch overrides the hardcoded `devel` branch
  212. for the `Edit` link which is also useful if you have a different working
  213. branch than `devel` e.g. `--git.devel:master`:option:.
  214. Edit URLs are generated as ``href="${url}/tree/${devel}/${path}#L${line}"``
  215. by default.
  216. You can edit ``config/nimdoc.cfg`` and modify the ``doc.item.seesrc`` value
  217. with a hyperlink to your own code repository.
  218. In the case of Nim's own documentation, the `commit` value is just a commit
  219. hash to append to a formatted URL to https://github.com/nim-lang/Nim. The
  220. ``tools/nimweb.nim`` helper queries the current git commit hash during the doc
  221. generation, but since you might be working on an unpublished repository, it
  222. also allows specifying a `githash` value in ``web/website.ini`` to force a
  223. specific commit in the output.
  224. Other Input Formats
  225. ===================
  226. The *Nim compiler* also has support for RST (reStructuredText) files with
  227. the `rst2html`:option: and `rst2tex`:option: commands. Documents like this one are
  228. initially written in a dialect of RST which adds support for Nim source code
  229. highlighting with the ``.. code-block:: nim`` prefix. ``code-block`` also
  230. supports highlighting of a few other languages supported by the
  231. `packages/docutils/highlite module <highlite.html>`_.
  232. Usage:
  233. .. code:: cmd
  234. nim rst2html docgen.rst
  235. Output::
  236. You're reading it!
  237. The `rst2tex`:option: command is invoked identically to `rst2html`:option:,
  238. but outputs a ``.tex`` file instead of ``.html``.
  239. HTML anchor generation
  240. ======================
  241. When you run the `rst2html`:option: command, all sections in the RST document will
  242. get an anchor you can hyperlink to. Usually, you can guess the anchor lower
  243. casing the section title and replacing spaces with dashes, and in any case, you
  244. can get it from the table of contents. But when you run the `doc`:option:
  245. command to generate API documentation, some symbol get one or two anchors at
  246. the same time: a numerical identifier, or a plain name plus a complex name.
  247. The numerical identifier is just a random number. The number gets assigned
  248. according to the section and position of the symbol in the file being processed
  249. and you should not rely on it being constant: if you add or remove a symbol the
  250. numbers may shuffle around.
  251. The plain name of a symbol is a simplified version of its fully exported
  252. signature. Variables or constants have the same plain name symbol as their
  253. complex name. The plain name for procs, templates, and other callable types
  254. will be their unquoted value after removing parameters, return types, and
  255. pragmas. The plain name allows short and nice linking of symbols that works
  256. unless you have a module with collisions due to overloading.
  257. If you hyperlink a plain name symbol and there are other matches on the same
  258. HTML file, most browsers will go to the first one. To differentiate the rest,
  259. you will need to use the complex name. A complex name for a callable type is
  260. made up of several parts:
  261. (**plain symbol**)(**.type**),(**first param**)?(**,param type**)\*
  262. The first thing to note is that all callable types have at least a comma, even
  263. if they don't have any parameters. If there are parameters, they are
  264. represented by their types and will be comma-separated. To the plain symbol a
  265. suffix may be added depending on the type of the callable:
  266. ============== ==============
  267. Callable type Suffix
  268. ============== ==============
  269. `proc`, `func` *empty string*
  270. `macro` ``.m``
  271. `method` ``.e``
  272. `iterator` ``.i``
  273. `template` ``.t``
  274. `converter` ``.c``
  275. ============== ==============
  276. The relationship of type to suffix is made by the proc `complexName` in the
  277. ``compiler/docgen.nim`` file. Here are some examples of complex names for
  278. symbols in the `system module <system.html>`_.
  279. * `type SomeSignedInt = int | int8 | int16 | int32 | int64` **=>**
  280. `#SomeSignedInt <system.html#SomeSignedInt>`_
  281. * `var globalRaiseHook: proc (e: ref E_Base): bool {.nimcall.}` **=>**
  282. `#globalRaiseHook <system.html#globalRaiseHook>`_
  283. * `const NimVersion = "0.0.0"` **=>**
  284. `#NimVersion <system.html#NimVersion>`_
  285. * `proc getTotalMem(): int {.rtl, raises: [], tags: [].}` **=>**
  286. `#getTotalMem, <system.html#getTotalMem>`_
  287. * `proc len[T](x: seq[T]): int {.magic: "LengthSeq", noSideEffect.}` **=>**
  288. `#len,seq[T] <system.html#len,seq[T]>`_
  289. * `iterator pairs[T](a: seq[T]): tuple[key: int, val: T] {.inline.}` **=>**
  290. `#pairs.i,seq[T] <iterators.html#pairs.i,seq[T]>`_
  291. * `template newException[](exceptn: typedesc; message: string;
  292. parentException: ref Exception = nil): untyped` **=>**
  293. `#newException.t,typedesc,string,ref.Exception
  294. <system.html#newException.t,typedesc,string,ref.Exception>`_
  295. Index (idx) file format
  296. =======================
  297. Files with the ``.idx`` extension are generated when you use the `Index
  298. switch <#related-options-index-switch>`_ along with commands to generate
  299. documentation from source or text files. You can programmatically generate
  300. indices with the `setIndexTerm()
  301. <rstgen.html#setIndexTerm,RstGenerator,string,string,string,string,string>`_
  302. and `writeIndexFile() <rstgen.html#writeIndexFile,RstGenerator,string>`_ procs.
  303. The purpose of `idx` files is to hold the interesting symbols and their HTML
  304. references so they can be later concatenated into a big index file with
  305. `mergeIndexes() <rstgen.html#mergeIndexes,string>`_. This section documents
  306. the file format in detail.
  307. Index files are line-oriented and tab-separated (newline and tab characters
  308. have to be escaped). Each line represents a record with at least two fields
  309. but can have up to four (additional columns are ignored). The content of these
  310. columns is:
  311. 1. Mandatory term being indexed. Terms can include quoting according to
  312. Nim's rules (e.g. \`^\`).
  313. 2. Base filename plus anchor hyperlink (e.g. ``algorithm.html#*,int,SortOrder``).
  314. 3. Optional human-readable string to display as a hyperlink. If the value is not
  315. present or is the empty string, the hyperlink will be rendered
  316. using the term. Prefix whitespace indicates that this entry is
  317. not for an API symbol but for a TOC entry.
  318. 4. Optional title or description of the hyperlink. Browsers usually display
  319. this as a tooltip after hovering a moment over the hyperlink.
  320. The index generation tools try to differentiate between documentation
  321. generated from ``.nim`` files and documentation generated from ``.txt`` or
  322. ``.rst`` files. The former are always closely related to source code and
  323. consist mainly of API entries. The latter are generic documents meant for
  324. human reading.
  325. To differentiate both types (documents and APIs), the index generator will add
  326. to the index of documents an entry with the title of the document. Since the
  327. title is the topmost element, it will be added with a second field containing
  328. just the filename without any HTML anchor. By convention, this entry without
  329. anchor is the *title entry*, and since entries in the index file are added as
  330. they are scanned, the title entry will be the first line. The title for APIs
  331. is not present because it can be generated concatenating the name of the file
  332. to the word **Module**.
  333. Normal symbols are added to the index with surrounding whitespaces removed. An
  334. exception to this are the table of content (TOC) entries. TOC entries are added to
  335. the index file with their third column having as much prefix spaces as their
  336. level is in the TOC (at least 1 character). The prefix whitespace helps to
  337. filter TOC entries from API or text symbols. This is important because the
  338. amount of spaces is used to replicate the hierarchy for document TOCs in the
  339. final index, and TOC entries found in ``.nim`` files are discarded.
  340. Additional resources
  341. ====================
  342. * `Nim Compiler User Guide <nimc.html#compiler-usage-commandminusline-switches>`_
  343. * Documentation for `rst module <rst.html>`_ -- Nim RST/Markdown parser.
  344. * `RST Quick Reference
  345. <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_
  346. The output for HTML and LaTeX comes from the ``config/nimdoc.cfg`` and
  347. ``config/nimdoc.tex.cfg`` configuration files. You can add and modify these
  348. files to your project to change the look of the docgen output.
  349. You can import the `packages/docutils/rstgen module <rstgen.html>`_ in your
  350. programs if you want to reuse the compiler's documentation generation procs.