reST.rst 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. .. _reST primer:
  2. ===========
  3. reST primer
  4. ===========
  5. .. sidebar:: KISS_ and readability_
  6. Instead of defining more and more roles, we at searx encourage our
  7. contributors to follow principles like KISS_ and readability_.
  8. We at searx are using reStructuredText (aka reST_) markup for all kind of
  9. documentation, with the builders from the Sphinx_ project a HTML output is
  10. generated and deployed at :docs:`github.io <.>`.
  11. The sources of Searx's documentation are located at :origin:`docs`. Run
  12. :ref:`make docs-live <make docs-live>` to build HTML while editing.
  13. .. sidebar:: Further reading
  14. - Sphinx-Primer_
  15. - `Sphinx markup constructs`_
  16. - reST_, docutils_, `docutils FAQ`_
  17. - Sphinx_, `sphinx-doc FAQ`_
  18. - `sphinx config`_, doctree_
  19. - `sphinx cross references`_
  20. - linuxdoc_
  21. - intersphinx_
  22. - `Sphinx's autodoc`_
  23. - `Sphinx's Python domain`_, `Sphinx's C domain`_
  24. - SVG_, ImageMagick_
  25. - DOT_, `Graphviz's dot`_, Graphviz_
  26. .. contents:: Contents
  27. :depth: 3
  28. :local:
  29. :backlinks: entry
  30. Sphinx_ and reST_ have their place in the python ecosystem. Over that reST is
  31. used in popular projects, e.g the Linux kernel documentation `[kernel doc]`_.
  32. .. _[kernel doc]: https://www.kernel.org/doc/html/latest/doc-guide/sphinx.html
  33. .. sidebar:: Content matters
  34. The readability_ of the reST sources has its value, therefore we recommend to
  35. make sparse usage of reST markup / .. content matters!
  36. **reST** is a plaintext markup language, its markup is *mostly* intuitive and
  37. you will not need to learn much to produce well formed articles with. I use the
  38. word *mostly*: like everything in live, reST has its advantages and
  39. disadvantages, some markups feel a bit grumpy (especially if you are used to
  40. other plaintext markups).
  41. Soft skills
  42. ===========
  43. Before going any deeper into the markup let's face on some **soft skills** a
  44. trained author brings with, to reach a well feedback from readers:
  45. - Documentation is dedicated to an audience and answers questions from the
  46. audience point of view.
  47. - Don't detail things which are general knowledge from the audience point of
  48. view.
  49. - Limit the subject, use cross links for any further reading.
  50. To be more concrete what a *point of view* means. In the (:origin:`docs`)
  51. folder we have three sections (and the *blog* folder), each dedicate to a
  52. different group of audience.
  53. User's POV: :origin:`docs/user`
  54. A typical user knows about search engines and might have heard about
  55. meta crawlers and privacy.
  56. Admin's POV: :origin:`docs/admin`
  57. A typical Admin knows about setting up services on a linux system, but he does
  58. not know all the pros and cons of a searx setup.
  59. Developer's POV: :origin:`docs/dev`
  60. Depending on the readability_ of code, a typical developer is able to read and
  61. understand source code. Describe what a item aims to do (e.g. a function).
  62. If the chronological order matters, describe it. Name the *out-of-limits
  63. conditions* and all the side effects a external developer will not know.
  64. .. _reST inline markup:
  65. Basic inline markup
  66. ===================
  67. .. sidebar:: Inline markup
  68. - :ref:`reST roles`
  69. - :ref:`reST smart ref`
  70. Basic inline markup is done with asterisks and backquotes. If asterisks or
  71. backquotes appear in running text and could be confused with inline markup
  72. delimiters, they have to be escaped with a backslash (``\*pointer``).
  73. .. table:: basic inline markup
  74. :widths: 4 3 7
  75. ================================================ ==================== ========================
  76. description rendered markup
  77. ================================================ ==================== ========================
  78. one asterisk for emphasis *italics* ``*italics*``
  79. two asterisks for strong emphasis **boldface** ``**boldface**``
  80. backquotes for code samples and literals ``foo()`` ````foo()````
  81. quote asterisks or backquotes \*foo is a pointer ``\*foo is a pointer``
  82. ================================================ ==================== ========================
  83. .. _reST basic structure:
  84. Basic article structure
  85. =======================
  86. The basic structure of an article makes use of heading adornments to markup
  87. chapter, sections and subsections.
  88. .. _reST template:
  89. reST template
  90. -------------
  91. reST template for an simple article:
  92. .. code:: reST
  93. .. _doc refname:
  94. ==============
  95. Document title
  96. ==============
  97. Lorem ipsum dolor sit amet, consectetur adipisici elit .. Further read
  98. :ref:`chapter refname`.
  99. .. _chapter refname:
  100. Chapter
  101. =======
  102. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
  103. aliquid ex ea commodi consequat ...
  104. .. _section refname:
  105. Section
  106. -------
  107. lorem ..
  108. .. _subsection refname:
  109. Subsection
  110. ~~~~~~~~~~
  111. lorem ..
  112. Headings
  113. --------
  114. #. title - with overline for document title:
  115. .. code:: reST
  116. ==============
  117. Document title
  118. ==============
  119. #. chapter - with anchor named ``anchor name``:
  120. .. code:: reST
  121. .. _anchor name:
  122. Chapter
  123. =======
  124. #. section
  125. .. code:: reST
  126. Section
  127. -------
  128. #. subsection
  129. .. code:: reST
  130. Subsection
  131. ~~~~~~~~~~
  132. Anchors & Links
  133. ===============
  134. .. _reST anchor:
  135. Anchors
  136. -------
  137. .. _ref role:
  138. https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-ref
  139. To refer a point in the documentation a anchor is needed. The :ref:`reST
  140. template <reST template>` shows an example where a chapter titled *"Chapters"*
  141. gets an anchor named ``chapter title``. Another example from *this* document,
  142. where the anchor named ``reST anchor``:
  143. .. code:: reST
  144. .. _reST anchor:
  145. Anchors
  146. -------
  147. To refer a point in the documentation a anchor is needed ...
  148. To refer anchors use the `ref role`_ markup:
  149. .. code:: reST
  150. Visit chapter :ref:`reST anchor`. Or set hyperlink text manualy :ref:`foo
  151. bar <reST anchor>`.
  152. .. admonition:: ``:ref:`` role
  153. :class: rst-example
  154. Visist chapter :ref:`reST anchor`. Or set hyperlink text manualy :ref:`foo
  155. bar <reST anchor>`.
  156. .. _reST ordinary ref:
  157. Link ordinary URL
  158. -----------------
  159. If you need to reference external URLs use *named* hyperlinks to maintain
  160. readability of reST sources. Here is a example taken from *this* article:
  161. .. code:: reST
  162. .. _Sphinx Field Lists:
  163. https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html
  164. With the *named* hyperlink `Sphinx Field Lists`_, the raw text is much more
  165. readable.
  166. And this shows the alternative (less readable) hyperlink markup `Sphinx Field
  167. Lists
  168. <https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html>`__.
  169. .. admonition:: Named hyperlink
  170. :class: rst-example
  171. With the *named* hyperlink `Sphinx Field Lists`_, the raw text is much more
  172. readable.
  173. And this shows the alternative (less readable) hyperlink markup `Sphinx Field
  174. Lists
  175. <https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html>`__.
  176. .. _reST smart ref:
  177. Smart refs
  178. ----------
  179. With the power of sphinx.ext.extlinks_ and intersphinx_ referencing external
  180. content becomes smart.
  181. .. table:: smart refs with sphinx.ext.extlinks_ and intersphinx_
  182. :widths: 4 3 7
  183. ========================== ================================== ====================================
  184. refer ... rendered example markup
  185. ========================== ================================== ====================================
  186. :rst:role:`rfc` :rfc:`822` ``:rfc:`822```
  187. :rst:role:`pep` :pep:`8` ``:pep:`8```
  188. sphinx.ext.extlinks_
  189. --------------------------------------------------------------------------------------------------
  190. project's wiki article :wiki:`Searx-instances` ``:wiki:`Searx-instances```
  191. to docs public URL :docs:`dev/reST.html` ``:docs:`dev/reST.html```
  192. files & folders origin :origin:`docs/dev/reST.rst` ``:origin:`docs/dev/reST.rst```
  193. pull request :pull:`1756` ``:pull:`1756```
  194. patch :patch:`af2cae6` ``:patch:`af2cae6```
  195. PyPi package :pypi:`searx` ``:pypi:`searx```
  196. manual page man :man:`bash` ``:man:`bash```
  197. intersphinx_
  198. --------------------------------------------------------------------------------------------------
  199. external anchor :ref:`python:and` ``:ref:`python:and```
  200. external doc anchor :doc:`jinja:templates` ``:doc:`jinja:templates```
  201. python code object :py:obj:`datetime.datetime` ``:py:obj:`datetime.datetime```
  202. flask code object :py:obj:`flask.Flask` ``:py:obj:`flask.Flask```
  203. ========================== ================================== ====================================
  204. Intersphinx is configured in :origin:`docs/conf.py`:
  205. .. code:: python
  206. intersphinx_mapping = {
  207. "python": ("https://docs.python.org/3/", None),
  208. "flask": ("https://flask.palletsprojects.com/", None),
  209. "jinja": ("https://jinja.palletsprojects.com/", None),
  210. "linuxdoc" : ("https://return42.github.io/linuxdoc/", None),
  211. "sphinx" : ("https://www.sphinx-doc.org/en/master/", None),
  212. }
  213. To list all anchors of the inventory (e.g. ``python``) use:
  214. .. code:: sh
  215. $ python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv
  216. .. _reST roles:
  217. Roles
  218. =====
  219. .. sidebar:: Further reading
  220. - `Sphinx Roles`_
  221. - :doc:`sphinx:usage/restructuredtext/domains`
  222. A *custom interpreted text role* (:duref:`ref <roles>`) is an inline piece of
  223. explicit markup. It signifies that that the enclosed text should be interpreted
  224. in a specific way.
  225. The general markup is one of:
  226. .. code:: reST
  227. :rolename:`ref-name`
  228. :rolename:`ref text <ref-name>`
  229. .. table:: smart refs with sphinx.ext.extlinks_ and intersphinx_
  230. :widths: 4 3 7
  231. ========================== ================================== ====================================
  232. role rendered example markup
  233. ========================== ================================== ====================================
  234. :rst:role:`guilabel` :guilabel:`&Cancel` ``:guilabel:`&Cancel```
  235. :rst:role:`kbd` :kbd:`C-x C-f` ``:kbd:`C-x C-f```
  236. :rst:role:`menuselection` :menuselection:`Open --> File` ``:menuselection:`Open --> File```
  237. :rst:role:`download` :download:`this file <reST.rst>` ``:download:`this file <reST.rst>```
  238. :rst:role:`math` :math:`a^2 + b^2 = c^2` ``:math:`a^2 + b^2 = c^2```
  239. :rst:role:`ref` :ref:`svg image example` ``:ref:`svg image example```
  240. :rst:role:`command` :command:`ls -la` ``:command:`ls -la```
  241. :durole:`emphasis` :emphasis:`italic` ``:emphasis:`italic```
  242. :durole:`strong` :strong:`bold` ``:strong:`bold```
  243. :durole:`literal` :literal:`foo()` ``:literal:`foo()```
  244. :durole:`subscript` H\ :sub:`2`\ O ``H\ :sub:`2`\ O``
  245. :durole:`superscript` E = mc\ :sup:`2` ``E = mc\ :sup:`2```
  246. :durole:`title-reference` :title:`Time` ``:title:`Time```
  247. ========================== ================================== ====================================
  248. Figures & Images
  249. ================
  250. .. sidebar:: Image processing
  251. With the directives from :ref:`linuxdoc <linuxdoc:kfigure>` the build process
  252. is flexible. To get best results in the generated output format, install
  253. ImageMagick_ and Graphviz_.
  254. Searx's sphinx setup includes: :ref:`linuxdoc:kfigure`. Scalable here means;
  255. scalable in sense of the build process. Normally in absence of a converter
  256. tool, the build process will break. From the authors POV it’s annoying to care
  257. about the build process when handling with images, especially since he has no
  258. access to the build process. With :ref:`linuxdoc:kfigure` the build process
  259. continues and scales output quality in dependence of installed image processors.
  260. If you want to add an image, you should use the ``kernel-figure`` and
  261. ``kernel-image`` directives. E.g. to insert a figure with a scalable image
  262. format use SVG (:ref:`svg image example`):
  263. .. code:: reST
  264. .. _svg image example:
  265. .. kernel-figure:: svg_image.svg
  266. :alt: SVG image example
  267. simple SVG image
  268. To refer the figure, a caption block is needed: :ref:`svg image example`.
  269. .. _svg image example:
  270. .. kernel-figure:: svg_image.svg
  271. :alt: SVG image example
  272. simple SVG image
  273. To refer the figure, a caption block is needed: :ref:`svg image example`.
  274. DOT files (aka Graphviz)
  275. ------------------------
  276. With :ref:`linuxdoc:kernel-figure` reST support for **DOT** formatted files is
  277. given.
  278. - `Graphviz's dot`_
  279. - DOT_
  280. - Graphviz_
  281. A simple example is shown in :ref:`dot file example`:
  282. .. code:: reST
  283. .. _dot file example:
  284. .. kernel-figure:: hello.dot
  285. :alt: hello world
  286. DOT's hello world example
  287. .. admonition:: hello.dot
  288. :class: rst-example
  289. .. _dot file example:
  290. .. kernel-figure:: hello.dot
  291. :alt: hello world
  292. DOT's hello world example
  293. ``kernel-render`` DOT
  294. ---------------------
  295. Embed *render* markups (or languages) like Graphviz's **DOT** is provided by the
  296. :ref:`linuxdoc:kernel-render` directive. A simple example of embedded DOT_ is
  297. shown in figure :ref:`dot render example`:
  298. .. code:: reST
  299. .. _dot render example:
  300. .. kernel-render:: DOT
  301. :alt: digraph
  302. :caption: Embedded DOT (Graphviz) code
  303. digraph foo {
  304. "bar" -> "baz";
  305. }
  306. Attribute ``caption`` is needed, if you want to refer the figure: :ref:`dot
  307. render example`.
  308. Please note :ref:`build tools <linuxdoc:kfigure_build_tools>`. If Graphviz_ is
  309. installed, you will see an vector image. If not, the raw markup is inserted as
  310. *literal-block*.
  311. .. admonition:: kernel-render DOT
  312. :class: rst-example
  313. .. _dot render example:
  314. .. kernel-render:: DOT
  315. :alt: digraph
  316. :caption: Embedded DOT (Graphviz) code
  317. digraph foo {
  318. "bar" -> "baz";
  319. }
  320. Attribute ``caption`` is needed, if you want to refer the figure: :ref:`dot
  321. render example`.
  322. ``kernel-render`` SVG
  323. ---------------------
  324. A simple example of embedded SVG_ is shown in figure :ref:`svg render example`:
  325. .. code:: reST
  326. .. _svg render example:
  327. .. kernel-render:: SVG
  328. :caption: Embedded **SVG** markup
  329. :alt: so-nw-arrow
  330. ..
  331. .. code:: xml
  332. <?xml version="1.0" encoding="UTF-8"?>
  333. <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
  334. baseProfile="full" width="70px" height="40px"
  335. viewBox="0 0 700 400"
  336. >
  337. <line x1="180" y1="370"
  338. x2="500" y2="50"
  339. stroke="black" stroke-width="15px"
  340. />
  341. <polygon points="585 0 525 25 585 50"
  342. transform="rotate(135 525 25)"
  343. />
  344. </svg>
  345. .. admonition:: kernel-render SVG
  346. :class: rst-example
  347. .. _svg render example:
  348. .. kernel-render:: SVG
  349. :caption: Embedded **SVG** markup
  350. :alt: so-nw-arrow
  351. <?xml version="1.0" encoding="UTF-8"?>
  352. <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
  353. baseProfile="full" width="70px" height="40px"
  354. viewBox="0 0 700 400"
  355. >
  356. <line x1="180" y1="370"
  357. x2="500" y2="50"
  358. stroke="black" stroke-width="15px"
  359. />
  360. <polygon points="585 0 525 25 585 50"
  361. transform="rotate(135 525 25)"
  362. />
  363. </svg>
  364. .. _reST lists:
  365. List markups
  366. ============
  367. Bullet list
  368. -----------
  369. List markup (:duref:`ref <bullet-lists>`) is simple:
  370. .. code:: reST
  371. - This is a bulleted list.
  372. 1. Nested lists are possible, but be aware that they must be separated from
  373. the parent list items by blank line
  374. 2. Second item of nested list
  375. - It has two items, the second
  376. item uses two lines.
  377. #. This is a numbered list.
  378. #. It has two items too.
  379. .. admonition:: bullet list
  380. :class: rst-example
  381. - This is a bulleted list.
  382. 1. Nested lists are possible, but be aware that they must be separated from
  383. the parent list items by blank line
  384. 2. Second item of nested list
  385. - It has two items, the second
  386. item uses two lines.
  387. #. This is a numbered list.
  388. #. It has two items too.
  389. Definition list
  390. ---------------
  391. .. sidebar:: definition term
  392. Note that the term cannot have more than one line of text.
  393. Definition lists (:duref:`ref <definition-lists>`) are created as follows:
  394. .. code:: reST
  395. term (up to a line of text)
  396. Definition of the term, which must be indented
  397. and can even consist of multiple paragraphs
  398. next term
  399. Description.
  400. .. admonition:: definition list
  401. :class: rst-example
  402. term (up to a line of text)
  403. Definition of the term, which must be indented
  404. and can even consist of multiple paragraphs
  405. next term
  406. Description.
  407. Quoted paragraphs
  408. -----------------
  409. Quoted paragraphs (:duref:`ref <block-quotes>`) are created by just indenting
  410. them more than the surrounding paragraphs. Line blocks (:duref:`ref
  411. <line-blocks>`) are a way of preserving line breaks:
  412. .. code:: reST
  413. normal paragraph ...
  414. lorem ipsum.
  415. Quoted paragraph ...
  416. lorem ipsum.
  417. | These lines are
  418. | broken exactly like in
  419. | the source file.
  420. .. admonition:: Quoted paragraph and line block
  421. :class: rst-example
  422. normal paragraph ...
  423. lorem ipsum.
  424. Quoted paragraph ...
  425. lorem ipsum.
  426. | These lines are
  427. | broken exactly like in
  428. | the source file.
  429. .. _reST field list:
  430. Field Lists
  431. -----------
  432. .. _Sphinx Field Lists:
  433. https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html
  434. .. sidebar:: bibliographic fields
  435. First lines fields are bibliographic fields, see `Sphinx Field Lists`_.
  436. Field lists are used as part of an extension syntax, such as options for
  437. directives, or database-like records meant for further processing. Field lists
  438. are mappings from field names to field bodies. They marked up like this:
  439. .. code:: reST
  440. :fieldname: Field content
  441. :foo: first paragraph in field foo
  442. second paragraph in field foo
  443. :bar: Field content
  444. .. admonition:: Field List
  445. :class: rst-example
  446. :fieldname: Field content
  447. :foo: first paragraph in field foo
  448. second paragraph in field foo
  449. :bar: Field content
  450. They are commonly used in Python documentation:
  451. .. code:: python
  452. def my_function(my_arg, my_other_arg):
  453. """A function just for me.
  454. :param my_arg: The first of my arguments.
  455. :param my_other_arg: The second of my arguments.
  456. :returns: A message (just for me, of course).
  457. """
  458. Further list blocks
  459. -------------------
  460. - field lists (:duref:`ref <field-lists>`, with caveats noted in
  461. :ref:`reST field list`)
  462. - option lists (:duref:`ref <option-lists>`)
  463. - quoted literal blocks (:duref:`ref <quoted-literal-blocks>`)
  464. - doctest blocks (:duref:`ref <doctest-blocks>`)
  465. Admonitions
  466. ===========
  467. Sidebar
  468. -------
  469. Sidebar is an eye catcher, often used for admonitions pointing further stuff or
  470. site effects. Here is the source of the sidebar :ref:`on top of this page <reST
  471. primer>`.
  472. .. code:: reST
  473. .. sidebar:: KISS_ and readability_
  474. Instead of defining more and more roles, we at searx encourage our
  475. contributors to follow principles like KISS_ and readability_.
  476. Generic admonition
  477. ------------------
  478. The generic :dudir:`admonition <admonitions>` needs a title:
  479. .. code:: reST
  480. .. admonition:: generic admonition title
  481. lorem ipsum ..
  482. .. admonition:: generic admonition title
  483. lorem ipsum ..
  484. Specific admonitions
  485. --------------------
  486. Specific admonitions: :dudir:`hint`, :dudir:`note`, :dudir:`tip` :dudir:`attention`,
  487. :dudir:`caution`, :dudir:`danger`, :dudir:`error`, , :dudir:`important`, and
  488. :dudir:`warning` .
  489. .. code:: reST
  490. .. hint::
  491. lorem ipsum ..
  492. .. note::
  493. lorem ipsum ..
  494. .. warning::
  495. lorem ipsum ..
  496. .. hint::
  497. lorem ipsum ..
  498. .. note::
  499. lorem ipsum ..
  500. .. tip::
  501. lorem ipsum ..
  502. .. attention::
  503. lorem ipsum ..
  504. .. caution::
  505. lorem ipsum ..
  506. .. danger::
  507. lorem ipsum ..
  508. .. important::
  509. lorem ipsum ..
  510. .. error::
  511. lorem ipsum ..
  512. .. warning::
  513. lorem ipsum ..
  514. Tables
  515. ======
  516. .. sidebar:: Nested tables
  517. Nested tables are ugly! Not all builder support nested tables, don't use
  518. them!
  519. ASCII-art tables like :ref:`reST simple table` and :ref:`reST grid table` might
  520. be comfortable for readers of the text-files, but they have huge disadvantages
  521. in the creation and modifying. First, they are hard to edit. Think about
  522. adding a row or a column to a ASCII-art table or adding a paragraph in a cell,
  523. it is a nightmare on big tables.
  524. .. sidebar:: List tables
  525. For meaningful patch and diff use :ref:`reST flat table`.
  526. Second the diff of modifying ASCII-art tables is not meaningful, e.g. widening a
  527. cell generates a diff in which also changes are included, which are only
  528. ascribable to the ASCII-art. Anyway, if you prefer ASCII-art for any reason,
  529. here are some helpers:
  530. * `Emacs Table Mode`_
  531. * `Online Tables Generator`_
  532. .. _reST simple table:
  533. Simple tables
  534. -------------
  535. :duref:`Simple tables <simple-tables>` allow *colspan* but not *rowspan*. If
  536. your table need some metadata (e.g. a title) you need to add the ``.. table::
  537. directive`` :dudir:`(ref) <table>` in front and place the table in its body:
  538. .. code:: reST
  539. .. table:: foo gate truth table
  540. :widths: grid
  541. :align: left
  542. ====== ====== ======
  543. Inputs Output
  544. ------------- ------
  545. A B A or B
  546. ====== ====== ======
  547. False
  548. --------------------
  549. True
  550. --------------------
  551. True False True
  552. (foo)
  553. ------ ------ ------
  554. False True
  555. (foo)
  556. ====== =============
  557. .. admonition:: Simple ASCII table
  558. :class: rst-example
  559. .. table:: foo gate truth table
  560. :widths: grid
  561. :align: left
  562. ====== ====== ======
  563. Inputs Output
  564. ------------- ------
  565. A B A or B
  566. ====== ====== ======
  567. False
  568. --------------------
  569. True
  570. --------------------
  571. True False True
  572. (foo)
  573. ------ ------ ------
  574. False True
  575. (foo)
  576. ====== =============
  577. .. _reST grid table:
  578. Grid tables
  579. -----------
  580. :duref:`Grid tables <grid-tables>` allow colspan *colspan* and *rowspan*:
  581. .. code:: reST
  582. .. table:: grid table example
  583. :widths: 1 1 5
  584. +------------+------------+-----------+
  585. | Header 1 | Header 2 | Header 3 |
  586. +============+============+===========+
  587. | body row 1 | column 2 | column 3 |
  588. +------------+------------+-----------+
  589. | body row 2 | Cells may span columns.|
  590. +------------+------------+-----------+
  591. | body row 3 | Cells may | - Cells |
  592. +------------+ span rows. | - contain |
  593. | body row 4 | | - blocks. |
  594. +------------+------------+-----------+
  595. .. admonition:: ASCII grid table
  596. :class: rst-example
  597. .. table:: grid table example
  598. :widths: 1 1 5
  599. +------------+------------+-----------+
  600. | Header 1 | Header 2 | Header 3 |
  601. +============+============+===========+
  602. | body row 1 | column 2 | column 3 |
  603. +------------+------------+-----------+
  604. | body row 2 | Cells may span columns.|
  605. +------------+------------+-----------+
  606. | body row 3 | Cells may | - Cells |
  607. +------------+ span rows. | - contain |
  608. | body row 4 | | - blocks. |
  609. +------------+------------+-----------+
  610. .. _reST flat table:
  611. flat-table
  612. ----------
  613. The ``flat-table`` is a further developed variant of the :ref:`list tables
  614. <linuxdoc:list-table-directives>`. It is a double-stage list similar to the
  615. :dudir:`list-table` with some additional features:
  616. column-span: ``cspan``
  617. with the role ``cspan`` a cell can be extended through additional columns
  618. row-span: ``rspan``
  619. with the role ``rspan`` a cell can be extended through additional rows
  620. auto-span:
  621. spans rightmost cell of a table row over the missing cells on the right side
  622. of that table-row. With Option ``:fill-cells:`` this behavior can changed
  623. from *auto span* to *auto fill*, which automatically inserts (empty) cells
  624. instead of spanning the last cell.
  625. options:
  626. :header-rows: [int] count of header rows
  627. :stub-columns: [int] count of stub columns
  628. :widths: [[int] [int] ... ] widths of columns
  629. :fill-cells: instead of auto-span missing cells, insert missing cells
  630. roles:
  631. :cspan: [int] additional columns (*morecols*)
  632. :rspan: [int] additional rows (*morerows*)
  633. The example below shows how to use this markup. The first level of the staged
  634. list is the *table-row*. In the *table-row* there is only one markup allowed,
  635. the list of the cells in this *table-row*. Exception are *comments* ( ``..`` )
  636. and *targets* (e.g. a ref to :ref:`row 2 of table's body <row body 2>`).
  637. .. code:: reST
  638. .. flat-table:: ``flat-table`` example
  639. :header-rows: 2
  640. :stub-columns: 1
  641. :widths: 1 1 1 1 2
  642. * - :rspan:`1` head / stub
  643. - :cspan:`3` head 1.1-4
  644. * - head 2.1
  645. - head 2.2
  646. - head 2.3
  647. - head 2.4
  648. * .. row body 1 / this is a comment
  649. - row 1
  650. - :rspan:`2` cell 1-3.1
  651. - cell 1.2
  652. - cell 1.3
  653. - cell 1.4
  654. * .. Comments and targets are allowed on *table-row* stage.
  655. .. _`row body 2`:
  656. - row 2
  657. - cell 2.2
  658. - :rspan:`1` :cspan:`1`
  659. cell 2.3 with a span over
  660. * col 3-4 &
  661. * row 2-3
  662. * - row 3
  663. - cell 3.2
  664. * - row 4
  665. - cell 4.1
  666. - cell 4.2
  667. - cell 4.3
  668. - cell 4.4
  669. * - row 5
  670. - cell 5.1 with automatic span to rigth end
  671. * - row 6
  672. - cell 6.1
  673. - ..
  674. .. admonition:: List table
  675. :class: rst-example
  676. .. flat-table:: ``flat-table`` example
  677. :header-rows: 2
  678. :stub-columns: 1
  679. :widths: 1 1 1 1 2
  680. * - :rspan:`1` head / stub
  681. - :cspan:`3` head 1.1-4
  682. * - head 2.1
  683. - head 2.2
  684. - head 2.3
  685. - head 2.4
  686. * .. row body 1 / this is a comment
  687. - row 1
  688. - :rspan:`2` cell 1-3.1
  689. - cell 1.2
  690. - cell 1.3
  691. - cell 1.4
  692. * .. Comments and targets are allowed on *table-row* stage.
  693. .. _`row body 2`:
  694. - row 2
  695. - cell 2.2
  696. - :rspan:`1` :cspan:`1`
  697. cell 2.3 with a span over
  698. * col 3-4 &
  699. * row 2-3
  700. * - row 3
  701. - cell 3.2
  702. * - row 4
  703. - cell 4.1
  704. - cell 4.2
  705. - cell 4.3
  706. - cell 4.4
  707. * - row 5
  708. - cell 5.1 with automatic span to rigth end
  709. * - row 6
  710. - cell 6.1
  711. - ..
  712. CSV table
  713. ---------
  714. CSV table might be the choice if you want to include CSV-data from a outstanding
  715. (build) process into your documentation.
  716. .. code:: reST
  717. .. csv-table:: CSV table example
  718. :header: .. , Column 1, Column 2
  719. :widths: 2 5 5
  720. :stub-columns: 1
  721. :file: csv_table.txt
  722. Content of file ``csv_table.txt``:
  723. .. literalinclude:: csv_table.txt
  724. .. admonition:: CSV table
  725. :class: rst-example
  726. .. csv-table:: CSV table example
  727. :header: .. , Column 1, Column 2
  728. :widths: 3 5 5
  729. :stub-columns: 1
  730. :file: csv_table.txt
  731. .. _KISS: https://en.wikipedia.org/wiki/KISS_principle
  732. .. _readability: https://docs.python-guide.org/writing/style/
  733. .. _Sphinx-Primer:
  734. http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
  735. .. _reST: https://docutils.sourceforge.io/rst.html
  736. .. _Sphinx Roles:
  737. https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html
  738. .. _Sphinx: http://www.sphinx-doc.org
  739. .. _`sphinx-doc FAQ`: http://www.sphinx-doc.org/en/stable/faq.html
  740. .. _Sphinx markup constructs:
  741. http://www.sphinx-doc.org/en/stable/markup/index.html
  742. .. _`sphinx cross references`:
  743. http://www.sphinx-doc.org/en/stable/markup/inline.html#cross-referencing-arbitrary-locations
  744. .. _sphinx.ext.extlinks:
  745. https://www.sphinx-doc.org/en/master/usage/extensions/extlinks.html
  746. .. _intersphinx: http://www.sphinx-doc.org/en/stable/ext/intersphinx.html
  747. .. _sphinx config: http://www.sphinx-doc.org/en/stable/config.html
  748. .. _Sphinx's autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
  749. .. _Sphinx's Python domain:
  750. http://www.sphinx-doc.org/en/stable/domains.html#the-python-domain
  751. .. _Sphinx's C domain:
  752. http://www.sphinx-doc.org/en/stable/domains.html#cross-referencing-c-constructs
  753. .. _doctree:
  754. http://www.sphinx-doc.org/en/master/extdev/tutorial.html?highlight=doctree#build-phases
  755. .. _docutils: http://docutils.sourceforge.net/docs/index.html
  756. .. _docutils FAQ: http://docutils.sourceforge.net/FAQ.html
  757. .. _linuxdoc: https://return42.github.io/linuxdoc
  758. .. _SVG: https://www.w3.org/TR/SVG11/expanded-toc.html
  759. .. _DOT: https://graphviz.gitlab.io/_pages/doc/info/lang.html
  760. .. _`Graphviz's dot`: https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf
  761. .. _Graphviz: https://graphviz.gitlab.io
  762. .. _ImageMagick: https://www.imagemagick.org
  763. .. _`Emacs Table Mode`: https://www.emacswiki.org/emacs/TableMode
  764. .. _`Online Tables Generator`: http://www.tablesgenerator.com/text_tables
  765. .. _`OASIS XML Exchange Table Model`: https://www.oasis-open.org/specs/tm9901.html