sphinx.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. Introduction
  2. ============
  3. The Linux kernel uses `Sphinx`_ to generate pretty documentation from
  4. `reStructuredText`_ files under ``Documentation``. To build the documentation in
  5. HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
  6. documentation is placed in ``Documentation/output``.
  7. .. _Sphinx: http://www.sphinx-doc.org/
  8. .. _reStructuredText: http://docutils.sourceforge.net/rst.html
  9. The reStructuredText files may contain directives to include structured
  10. documentation comments, or kernel-doc comments, from source files. Usually these
  11. are used to describe the functions and types and design of the code. The
  12. kernel-doc comments have some special structure and formatting, but beyond that
  13. they are also treated as reStructuredText.
  14. Finally, there are thousands of plain text documentation files scattered around
  15. ``Documentation``. Some of these will likely be converted to reStructuredText
  16. over time, but the bulk of them will remain in plain text.
  17. .. _sphinx_install:
  18. Sphinx Install
  19. ==============
  20. The ReST markups currently used by the Documentation/ files are meant to be
  21. built with ``Sphinx`` version 1.3 or upper. If you're desiring to build
  22. PDF outputs, it is recommended to use version 1.4.6 or upper.
  23. There's a script that checks for the Sphinx requirements. Please see
  24. :ref:`sphinx-pre-install` for further details.
  25. Most distributions are shipped with Sphinx, but its toolchain is fragile,
  26. and it is not uncommon that upgrading it or some other Python packages
  27. on your machine would cause the documentation build to break.
  28. A way to get rid of that is to use a different version than the one shipped
  29. on your distributions. In order to do that, it is recommended to install
  30. Sphinx inside a virtual environment, using ``virtualenv-3``
  31. or ``virtualenv``, depending on how your distribution packaged Python 3.
  32. .. note::
  33. #) Sphinx versions below 1.5 don't work properly with Python's
  34. docutils version 0.13.1 or upper. So, if you're willing to use
  35. those versions, you should run ``pip install 'docutils==0.12'``.
  36. #) It is recommended to use the RTD theme for html output. Depending
  37. on the Sphinx version, it should be installed in separate,
  38. with ``pip install sphinx_rtd_theme``.
  39. #) Some ReST pages contain math expressions. Due to the way Sphinx work,
  40. those expressions are written using LaTeX notation. It needs texlive
  41. installed with amdfonts and amsmath in order to evaluate them.
  42. In summary, if you want to install Sphinx version 1.4.9, you should do::
  43. $ virtualenv sphinx_1.4
  44. $ . sphinx_1.4/bin/activate
  45. (sphinx_1.4) $ pip install -r Documentation/sphinx/requirements.txt
  46. After running ``. sphinx_1.4/bin/activate``, the prompt will change,
  47. in order to indicate that you're using the new environment. If you
  48. open a new shell, you need to rerun this command to enter again at
  49. the virtual environment before building the documentation.
  50. Image output
  51. ------------
  52. The kernel documentation build system contains an extension that
  53. handles images on both GraphViz and SVG formats (see
  54. :ref:`sphinx_kfigure`).
  55. For it to work, you need to install both GraphViz and ImageMagick
  56. packages. If those packages are not installed, the build system will
  57. still build the documentation, but won't include any images at the
  58. output.
  59. PDF and LaTeX builds
  60. --------------------
  61. Such builds are currently supported only with Sphinx versions 1.4 and upper.
  62. For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265.
  63. Depending on the distribution, you may also need to install a series of
  64. ``texlive`` packages that provide the minimal set of functionalities
  65. required for ``XeLaTeX`` to work.
  66. .. _sphinx-pre-install:
  67. Checking for Sphinx dependencies
  68. --------------------------------
  69. There's a script that automatically check for Sphinx dependencies. If it can
  70. recognize your distribution, it will also give a hint about the install
  71. command line options for your distro::
  72. $ ./scripts/sphinx-pre-install
  73. Checking if the needed tools for Fedora release 26 (Twenty Six) are available
  74. Warning: better to also install "texlive-luatex85".
  75. You should run:
  76. sudo dnf install -y texlive-luatex85
  77. /usr/bin/virtualenv sphinx_1.4
  78. . sphinx_1.4/bin/activate
  79. pip install -r Documentation/sphinx/requirements.txt
  80. Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
  81. By default, it checks all the requirements for both html and PDF, including
  82. the requirements for images, math expressions and LaTeX build, and assumes
  83. that a virtual Python environment will be used. The ones needed for html
  84. builds are assumed to be mandatory; the others to be optional.
  85. It supports two optional parameters:
  86. ``--no-pdf``
  87. Disable checks for PDF;
  88. ``--no-virtualenv``
  89. Use OS packaging for Sphinx instead of Python virtual environment.
  90. Sphinx Build
  91. ============
  92. The usual way to generate the documentation is to run ``make htmldocs`` or
  93. ``make pdfdocs``. There are also other formats available, see the documentation
  94. section of ``make help``. The generated documentation is placed in
  95. format-specific subdirectories under ``Documentation/output``.
  96. To generate documentation, Sphinx (``sphinx-build``) must obviously be
  97. installed. For prettier HTML output, the Read the Docs Sphinx theme
  98. (``sphinx_rtd_theme``) is used if available. For PDF output you'll also need
  99. ``XeLaTeX`` and ``convert(1)`` from ImageMagick (https://www.imagemagick.org).
  100. All of these are widely available and packaged in distributions.
  101. To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
  102. variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
  103. output.
  104. To remove the generated documentation, run ``make cleandocs``.
  105. Writing Documentation
  106. =====================
  107. Adding new documentation can be as simple as:
  108. 1. Add a new ``.rst`` file somewhere under ``Documentation``.
  109. 2. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
  110. .. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
  111. This is usually good enough for simple documentation (like the one you're
  112. reading right now), but for larger documents it may be advisable to create a
  113. subdirectory (or use an existing one). For example, the graphics subsystem
  114. documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
  115. and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
  116. the main index.
  117. See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
  118. with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
  119. to get started with reStructuredText. There are also some `Sphinx specific
  120. markup constructs`_.
  121. .. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
  122. .. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
  123. Specific guidelines for the kernel documentation
  124. ------------------------------------------------
  125. Here are some specific guidelines for the kernel documentation:
  126. * Please don't go overboard with reStructuredText markup. Keep it
  127. simple. For the most part the documentation should be plain text with
  128. just enough consistency in formatting that it can be converted to
  129. other formats.
  130. * Please keep the formatting changes minimal when converting existing
  131. documentation to reStructuredText.
  132. * Also update the content, not just the formatting, when converting
  133. documentation.
  134. * Please stick to this order of heading adornments:
  135. 1. ``=`` with overline for document title::
  136. ==============
  137. Document title
  138. ==============
  139. 2. ``=`` for chapters::
  140. Chapters
  141. ========
  142. 3. ``-`` for sections::
  143. Section
  144. -------
  145. 4. ``~`` for subsections::
  146. Subsection
  147. ~~~~~~~~~~
  148. Although RST doesn't mandate a specific order ("Rather than imposing a fixed
  149. number and order of section title adornment styles, the order enforced will be
  150. the order as encountered."), having the higher levels the same overall makes
  151. it easier to follow the documents.
  152. * For inserting fixed width text blocks (for code examples, use case
  153. examples, etc.), use ``::`` for anything that doesn't really benefit
  154. from syntax highlighting, especially short snippets. Use
  155. ``.. code-block:: <language>`` for longer code blocks that benefit
  156. from highlighting.
  157. the C domain
  158. ------------
  159. The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a
  160. function prototype:
  161. .. code-block:: rst
  162. .. c:function:: int ioctl( int fd, int request )
  163. The C domain of the kernel-doc has some additional features. E.g. you can
  164. *rename* the reference name of a function with a common name like ``open`` or
  165. ``ioctl``:
  166. .. code-block:: rst
  167. .. c:function:: int ioctl( int fd, int request )
  168. :name: VIDIOC_LOG_STATUS
  169. The func-name (e.g. ioctl) remains in the output but the ref-name changed from
  170. ``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also
  171. changed to ``VIDIOC_LOG_STATUS`` and the function can now referenced by:
  172. .. code-block:: rst
  173. :c:func:`VIDIOC_LOG_STATUS`
  174. list tables
  175. -----------
  176. We recommend the use of *list table* formats. The *list table* formats are
  177. double-stage lists. Compared to the ASCII-art they might not be as
  178. comfortable for
  179. readers of the text files. Their advantage is that they are easy to
  180. create or modify and that the diff of a modification is much more meaningful,
  181. because it is limited to the modified content.
  182. The ``flat-table`` is a double-stage list similar to the ``list-table`` with
  183. some additional features:
  184. * column-span: with the role ``cspan`` a cell can be extended through
  185. additional columns
  186. * row-span: with the role ``rspan`` a cell can be extended through
  187. additional rows
  188. * auto span rightmost cell of a table row over the missing cells on the right
  189. side of that table-row. With Option ``:fill-cells:`` this behavior can
  190. changed from *auto span* to *auto fill*, which automatically inserts (empty)
  191. cells instead of spanning the last cell.
  192. options:
  193. * ``:header-rows:`` [int] count of header rows
  194. * ``:stub-columns:`` [int] count of stub columns
  195. * ``:widths:`` [[int] [int] ... ] widths of columns
  196. * ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells
  197. roles:
  198. * ``:cspan:`` [int] additional columns (*morecols*)
  199. * ``:rspan:`` [int] additional rows (*morerows*)
  200. The example below shows how to use this markup. The first level of the staged
  201. list is the *table-row*. In the *table-row* there is only one markup allowed,
  202. the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` )
  203. and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row
  204. <last row>`).
  205. .. code-block:: rst
  206. .. flat-table:: table title
  207. :widths: 2 1 1 3
  208. * - head col 1
  209. - head col 2
  210. - head col 3
  211. - head col 4
  212. * - column 1
  213. - field 1.1
  214. - field 1.2 with autospan
  215. * - column 2
  216. - field 2.1
  217. - :rspan:`1` :cspan:`1` field 2.2 - 3.3
  218. * .. _`last row`:
  219. - column 3
  220. Rendered as:
  221. .. flat-table:: table title
  222. :widths: 2 1 1 3
  223. * - head col 1
  224. - head col 2
  225. - head col 3
  226. - head col 4
  227. * - column 1
  228. - field 1.1
  229. - field 1.2 with autospan
  230. * - column 2
  231. - field 2.1
  232. - :rspan:`1` :cspan:`1` field 2.2 - 3.3
  233. * .. _`last row`:
  234. - column 3
  235. .. _sphinx_kfigure:
  236. Figures & Images
  237. ================
  238. If you want to add an image, you should use the ``kernel-figure`` and
  239. ``kernel-image`` directives. E.g. to insert a figure with a scalable
  240. image format use SVG (:ref:`svg_image_example`)::
  241. .. kernel-figure:: svg_image.svg
  242. :alt: simple SVG image
  243. SVG image example
  244. .. _svg_image_example:
  245. .. kernel-figure:: svg_image.svg
  246. :alt: simple SVG image
  247. SVG image example
  248. The kernel figure (and image) directive support **DOT** formated files, see
  249. * DOT: http://graphviz.org/pdf/dotguide.pdf
  250. * Graphviz: http://www.graphviz.org/content/dot-language
  251. A simple example (:ref:`hello_dot_file`)::
  252. .. kernel-figure:: hello.dot
  253. :alt: hello world
  254. DOT's hello world example
  255. .. _hello_dot_file:
  256. .. kernel-figure:: hello.dot
  257. :alt: hello world
  258. DOT's hello world example
  259. Embed *render* markups (or languages) like Graphviz's **DOT** is provided by the
  260. ``kernel-render`` directives.::
  261. .. kernel-render:: DOT
  262. :alt: foobar digraph
  263. :caption: Embedded **DOT** (Graphviz) code
  264. digraph foo {
  265. "bar" -> "baz";
  266. }
  267. How this will be rendered depends on the installed tools. If Graphviz is
  268. installed, you will see an vector image. If not the raw markup is inserted as
  269. *literal-block* (:ref:`hello_dot_render`).
  270. .. _hello_dot_render:
  271. .. kernel-render:: DOT
  272. :alt: foobar digraph
  273. :caption: Embedded **DOT** (Graphviz) code
  274. digraph foo {
  275. "bar" -> "baz";
  276. }
  277. The *render* directive has all the options known from the *figure* directive,
  278. plus option ``caption``. If ``caption`` has a value, a *figure* node is
  279. inserted. If not, a *image* node is inserted. A ``caption`` is also needed, if
  280. you want to refer it (:ref:`hello_svg_render`).
  281. Embedded **SVG**::
  282. .. kernel-render:: SVG
  283. :caption: Embedded **SVG** markup
  284. :alt: so-nw-arrow
  285. <?xml version="1.0" encoding="UTF-8"?>
  286. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...>
  287. ...
  288. </svg>
  289. .. _hello_svg_render:
  290. .. kernel-render:: SVG
  291. :caption: Embedded **SVG** markup
  292. :alt: so-nw-arrow
  293. <?xml version="1.0" encoding="UTF-8"?>
  294. <svg xmlns="http://www.w3.org/2000/svg"
  295. version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
  296. <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
  297. <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
  298. </svg>