reST.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. ------
  14. .. sidebar:: Further reading
  15. - Sphinx-Primer_
  16. - `Sphinx markup constructs`_
  17. - reST_, docutils_, `docutils FAQ`_
  18. - Sphinx_, `sphinx-doc FAQ`_
  19. - `sphinx config`_, doctree_
  20. - `sphinx cross references`_
  21. - linuxdoc_
  22. - intersphinx_
  23. - `Sphinx's autodoc`_
  24. - `Sphinx's Python domain`_, `Sphinx's C domain`_
  25. .. contents:: Contents
  26. :depth: 3
  27. :local:
  28. :backlinks: entry
  29. ------
  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. describe chronological order matters, describe it. Name the *out-of-limits
  63. condition* and all the side effects a external developer will not know.
  64. .. _reST inline markup:
  65. Basic inline markup
  66. ===================
  67. ``*italics*`` -- *italics*
  68. one asterisk for emphasis
  69. ``**boldface**`` -- **boldface**
  70. two asterisks for strong emphasis and
  71. ````foo()```` -- ``foo()``
  72. backquotes for code samples and literals.
  73. ``\*foo is a pointer`` -- \*foo is a pointer
  74. If asterisks or backquotes appear in running text and could be confused with
  75. inline markup delimiters, they have to be escaped with a backslash (``\*foo is
  76. a pointer``).
  77. .. _reST basic structure:
  78. Basic article structure
  79. =======================
  80. The basic structure of an article makes use of heading adornments to markup
  81. chapter, sections and subsections.
  82. #. ``=`` with overline for document title
  83. #. ``=`` for chapters
  84. #. ``-`` for sections
  85. #. ``~`` for subsections
  86. .. _reST template:
  87. .. admonition:: reST template
  88. :class: rst-example
  89. .. code:: reST
  90. .. _document title:
  91. ==============
  92. Document title
  93. ==============
  94. Lorem ipsum dolor sit amet, consectetur adipisici elit ..
  95. Further read :ref:`chapter title`.
  96. .. _chapter title:
  97. Chapters
  98. ========
  99. Ut enim ad minim veniam, quis nostrud exercitation ullamco
  100. laboris nisi ut aliquid ex ea commodi consequat ...
  101. Section
  102. -------
  103. lorem ..
  104. Subsection
  105. ~~~~~~~~~~
  106. lorem ..
  107. Anchors & Links
  108. ===============
  109. .. _reST anchor:
  110. Anchors
  111. -------
  112. .. _ref role:
  113. https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-ref
  114. To refer a point in the documentation a anchor is needed. The :ref:`reST
  115. template <reST template>` shows an example where a chapter titled *"Chapters"*
  116. gets an anchor named ``chapter title``. Another example from *this* document,
  117. where the anchor named ``reST anchor``:
  118. .. code:: reST
  119. .. _reST anchor:
  120. Anchors
  121. -------
  122. To refer a point in the documentation a anchor is needed ...
  123. To refer anchors use the `ref role`_ markup:
  124. .. code:: reST
  125. Visit chapter :ref:`reST anchor`.
  126. Or set hyperlink text manualy :ref:`foo bar <reST anchor>`.
  127. .. admonition:: ``:ref:`` role
  128. :class: rst-example
  129. Visist chapter :ref:`reST anchor`
  130. Or set hyperlink text manualy :ref:`foo bar <reST anchor>`.
  131. .. _reST ordinary ref:
  132. link ordinary URL
  133. -----------------
  134. If you need to reference external URLs use *named* hyperlinks to maintain
  135. readability of reST sources. Here is a example taken from *this* article:
  136. .. code:: reST
  137. .. _Sphinx Field Lists:
  138. https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html
  139. With the *named* hyperlink `Sphinx Field Lists`_, the raw text is much more
  140. readable.
  141. And this shows the alternative (less readable) hyperlink markup `Sphinx Field
  142. Lists
  143. <https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html>`__.
  144. .. admonition:: Named hyperlink
  145. :class: rst-example
  146. With the *named* hyperlink `Sphinx Field Lists`_, the raw text is much more
  147. readable.
  148. And this shows the alternative (less readable) hyperlink markup `Sphinx Field
  149. Lists
  150. <https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html>`__.
  151. .. _reST smart ref:
  152. smart references
  153. ----------------
  154. With the power of sphinx.ext.extlinks_ and intersphinx_ referencing external
  155. content becomes smart.
  156. ========================== ================================== ====================================
  157. refer ... rendered example markup
  158. ========================== ================================== ====================================
  159. :rst:role:`rfc` :rfc:`822` ``:rfc:`822```
  160. :rst:role:`pep` :pep:`8` ``:pep:`8```
  161. sphinx.ext.extlinks_
  162. --------------------------------------------------------------------------------------------------
  163. project's wiki article :wiki:`Searx-instances` ``:wiki:`Searx-instances```
  164. to docs public URL :docs:`dev/reST.html` ``:docs:`dev/reST.html```
  165. files & folders origin :origin:`docs/dev/reST.rst` ``:origin:`docs/dev/reST.rst```
  166. pull request :pull:`1756` ``:pull:`1756```
  167. patch :patch:`af2cae6` ``:patch:`af2cae6```
  168. PyPi package :pypi:`searx` ``:pypi:`searx```
  169. manual page man :man:`bash` ``:man:`bash```
  170. intersphinx_
  171. --------------------------------------------------------------------------------------------------
  172. external anchor :ref:`python:and` ``:ref:`python:and```
  173. external doc anchor :doc:`jinja:templates` ``:doc:`jinja:templates```
  174. python code object :py:obj:`datetime.datetime` ``:py:obj:`datetime.datetime```
  175. flask code object :py:obj:`flask.Flask` ``:py:obj:`flask.Flask```
  176. ========================== ================================== ====================================
  177. Intersphinx is configured in :origin:`docs/conf.py`:
  178. .. code:: python
  179. intersphinx_mapping = {
  180. "python": ("https://docs.python.org/3/", None),
  181. "flask": ("https://flask.palletsprojects.com/", None),
  182. "jinja": ("https://jinja.palletsprojects.com/", None),
  183. "linuxdoc" : ("https://return42.github.io/linuxdoc/", None),
  184. "sphinx" : ("https://www.sphinx-doc.org/en/master/", None),
  185. }
  186. To list all anchors of the inventory (e.g. ``python``) use:
  187. .. code:: sh
  188. $ python -m sphinx.ext.intersphinx https://docs.python.org/3/objects.inv
  189. Roles
  190. =====
  191. A *custom interpreted text role* (:duref:`ref <roles>`) is an inline piece of
  192. explicit markup. It signifies that that the enclosed text should be interpreted
  193. in a specific way. The general syntax is ``:rolename:`content```.
  194. ========================== ================================== ====================================
  195. role rendered example markup
  196. ========================== ================================== ====================================
  197. :rst:role:`guilabel` :guilabel:`&Cancel` ``:guilabel:`&Cancel```
  198. :rst:role:`kbd` :kbd:`C-x C-f` ``:kbd:`C-x C-f```
  199. :rst:role:`menuselection` :menuselection:`Open --> File` ``:menuselection:`Open --> File```
  200. :rst:role:`download` :download:`this file <reST.rst>` ``:download:`this file <reST.rst>```
  201. :rst:role:`math` :math:`a^2 + b^2 = c^2` ``:math:`a^2 + b^2 = c^2```
  202. :rst:role:`ref` :ref:`svg image example` ``:ref:`svg image example```
  203. :rst:role:`command` :command:`ls -la` ``:command:`ls -la```
  204. :durole:`emphasis` :emphasis:`italic` ``:emphasis:`italic```
  205. :durole:`strong` :strong:`bold` ``:strong:`bold```
  206. :durole:`literal` :literal:`foo()` ``:literal:`foo()```
  207. :durole:`subscript` H\ :sub:`2`\ O ``H\ :sub:`2`\ O``
  208. :durole:`superscript` E = mc\ :sup:`2` ``E = mc\ :sup:`2```
  209. :durole:`title-reference` :title:`Time` ``:title:`Time```
  210. ========================== ================================== ====================================
  211. Refer to `Sphinx Roles`_ for roles added by Sphinx.
  212. Figures & Images
  213. ================
  214. Searx's sphinx setup includes: :ref:`linuxdoc:kfigure`. Scalable here means;
  215. scalable in sense of the build process. Normally in absence of a converter
  216. tool, the build process will break. From the authors POV it’s annoying to care
  217. about the build process when handling with images, especially since he has no
  218. access to the build process. With :ref:`linuxdoc:kfigure` the build process
  219. continues and scales output quality in dependence of installed image processors.
  220. If you want to add an image, you should use the ``kernel-figure`` and
  221. ``kernel-image`` directives. E.g. to insert a figure with a scalable image
  222. format use SVG (:ref:`svg_image_example`):
  223. .. code:: reST
  224. .. _svg image example:
  225. .. kernel-figure:: svg_image.svg
  226. :alt: simple SVG image
  227. SVG image example
  228. .. _svg image example:
  229. .. kernel-figure:: svg_image.svg
  230. :alt: simple SVG image
  231. SVG image example
  232. DOT files (aka Graphviz)
  233. ------------------------
  234. The kernel figure (and image) directive support **DOT** formated files, see
  235. * DOT: http://graphviz.org/pdf/dotguide.pdf
  236. * Graphviz: http://www.graphviz.org/content/dot-language
  237. A simple example (:ref:`hello_dot_file`):
  238. .. code:: reST
  239. .. kernel-figure:: hello.dot
  240. :alt: hello world
  241. DOT's hello world example
  242. .. admonition:: hello.dot
  243. :class: rst-example
  244. .. kernel-figure:: hello.dot
  245. :alt: hello world
  246. DOT's hello world example
  247. ``kernel-render`` DOT
  248. ---------------------
  249. Embed *render* markups (or languages) like Graphviz's **DOT** is provided by the
  250. ``kernel-render`` directives.
  251. .. code:: reST
  252. .. kernel-render:: DOT markup
  253. :alt: foobar digraph
  254. :caption: Embedded **DOT** (Graphviz) code
  255. digraph foo {
  256. "bar" -> "baz";
  257. }
  258. How this will be rendered depends on the installed tools. If Graphviz is
  259. installed, you will see an vector image. If not the raw markup is inserted as
  260. *literal-block* (:ref:`hello_dot_render`).
  261. .. admonition:: DOT markup
  262. :class: rst-example
  263. .. _hello_dot_render:
  264. .. kernel-render:: DOT
  265. :alt: foobar digraph
  266. :caption: Embedded **DOT** (Graphviz) code
  267. digraph foo {
  268. "bar" -> "baz";
  269. }
  270. The *render* directive has all the options known from the *figure* directive,
  271. plus option ``caption``. If ``caption`` has a value, a *figure* node is
  272. inserted. If not, a *image* node is inserted. A ``caption`` is also needed, if
  273. you want to refer it (:ref:`hello_svg_render`).
  274. ``kernel-render`` SVG
  275. ---------------------
  276. .. code:: reST
  277. .. kernel-render:: SVG markup
  278. :caption: Embedded **SVG** markup
  279. :alt: so-nw-arrow
  280. ..
  281. .. code:: xml
  282. <?xml version="1.0" encoding="UTF-8"?>
  283. <svg xmlns="http://www.w3.org/2000/svg"
  284. version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
  285. <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
  286. <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
  287. </svg>
  288. .. admonition:: SVG markup
  289. :class: rst-example
  290. .. _hello_svg_render:
  291. .. kernel-render:: SVG
  292. :caption: Embedded **SVG** markup
  293. :alt: so-nw-arrow
  294. <?xml version="1.0" encoding="UTF-8"?>
  295. <svg xmlns="http://www.w3.org/2000/svg"
  296. version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
  297. <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
  298. <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
  299. </svg>
  300. .. _reST lists:
  301. List markups
  302. ============
  303. Bullet list
  304. -----------
  305. List markup (:duref:`ref <bullet-lists>`) is simple:
  306. .. code:: reST
  307. - This is a bulleted list.
  308. 1. Nested lists are possible, but be aware that they must be separated from
  309. the parent list items by blank line
  310. 2. Second item of nested list
  311. - It has two items, the second
  312. item uses two lines.
  313. #. This is a numbered list.
  314. #. It has two items too.
  315. .. admonition:: bullet list
  316. :class: rst-example
  317. - This is a bulleted list.
  318. 1. Nested lists are possible, but be aware that they must be separated from
  319. the parent list items by blank line
  320. 2. Second item of nested list
  321. - It has two items, the second
  322. item uses two lines.
  323. #. This is a numbered list.
  324. #. It has two items too.
  325. Definition list
  326. ---------------
  327. .. sidebar:: definition term
  328. Note that the term cannot have more than one line of text.
  329. Definition lists (:duref:`ref <definition-lists>`) are created as follows:
  330. .. code:: reST
  331. term (up to a line of text)
  332. Definition of the term, which must be indented
  333. and can even consist of multiple paragraphs
  334. next term
  335. Description.
  336. .. admonition:: definition list
  337. :class: rst-example
  338. term (up to a line of text)
  339. Definition of the term, which must be indented
  340. and can even consist of multiple paragraphs
  341. next term
  342. Description.
  343. Quoted paragraphs
  344. -----------------
  345. Quoted paragraphs (:duref:`ref <block-quotes>`) are created by just indenting
  346. them more than the surrounding paragraphs. Line blocks (:duref:`ref
  347. <line-blocks>`) are a way of preserving line breaks:
  348. .. code:: reST
  349. normal paragraph ...
  350. lorem ipsum.
  351. Quoted paragraph ...
  352. lorem ipsum.
  353. | These lines are
  354. | broken exactly like in
  355. | the source file.
  356. .. admonition:: Quoted paragraph and line block
  357. :class: rst-example
  358. normal paragraph ...
  359. lorem ipsum.
  360. Quoted paragraph ...
  361. lorem ipsum.
  362. | These lines are
  363. | broken exactly like in
  364. | the source file.
  365. .. _reST field list:
  366. Field Lists
  367. -----------
  368. .. _Sphinx Field Lists:
  369. https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html
  370. .. sidebar:: bibliographic fields
  371. First lines fields are bibliographic fields, see `Sphinx Field Lists`_.
  372. Field lists are used as part of an extension syntax, such as options for
  373. directives, or database-like records meant for further processing. Field lists
  374. are mappings from field names to field bodies. They marked up like this:
  375. .. code:: reST
  376. :fieldname: Field content
  377. :foo: first paragraph in field foo
  378. second paragraph in field foo
  379. :bar: Field content
  380. .. admonition:: Field List
  381. :class: rst-example
  382. :fieldname: Field content
  383. :foo: first paragraph in field foo
  384. second paragraph in field foo
  385. :bar: Field content
  386. They are commonly used in Python documentation:
  387. .. code:: python
  388. def my_function(my_arg, my_other_arg):
  389. """A function just for me.
  390. :param my_arg: The first of my arguments.
  391. :param my_other_arg: The second of my arguments.
  392. :returns: A message (just for me, of course).
  393. """
  394. Further list blocks
  395. -------------------
  396. - field lists (:duref:`ref <field-lists>`, with caveats noted in
  397. :ref:`reST field list`)
  398. - option lists (:duref:`ref <option-lists>`)
  399. - quoted literal blocks (:duref:`ref <quoted-literal-blocks>`)
  400. - doctest blocks (:duref:`ref <doctest-blocks>`)
  401. .. _KISS: https://en.wikipedia.org/wiki/KISS_principle
  402. .. _readability: https://docs.python-guide.org/writing/style/
  403. .. _Sphinx-Primer:
  404. http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
  405. .. _reST: https://docutils.sourceforge.io/rst.html
  406. .. _Sphinx Roles:
  407. https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html
  408. .. _Sphinx: http://www.sphinx-doc.org
  409. .. _`sphinx-doc FAQ`: http://www.sphinx-doc.org/en/stable/faq.html
  410. .. _Sphinx markup constructs:
  411. http://www.sphinx-doc.org/en/stable/markup/index.html
  412. .. _`sphinx cross references`:
  413. http://www.sphinx-doc.org/en/stable/markup/inline.html#cross-referencing-arbitrary-locations
  414. .. _sphinx.ext.extlinks:
  415. https://www.sphinx-doc.org/en/master/usage/extensions/extlinks.html
  416. .. _intersphinx: http://www.sphinx-doc.org/en/stable/ext/intersphinx.html
  417. .. _sphinx config: http://www.sphinx-doc.org/en/stable/config.html
  418. .. _Sphinx's autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
  419. .. _Sphinx's Python domain:
  420. http://www.sphinx-doc.org/en/stable/domains.html#the-python-domain
  421. .. _Sphinx's C domain:
  422. http://www.sphinx-doc.org/en/stable/domains.html#cross-referencing-c-constructs
  423. .. _doctree:
  424. http://www.sphinx-doc.org/en/master/extdev/tutorial.html?highlight=doctree#build-phases
  425. .. _docutils: http://docutils.sourceforge.net/docs/index.html
  426. .. _docutils FAQ: http://docutils.sourceforge.net/FAQ.html
  427. .. _linuxdoc: https://return42.github.io/linuxdoc