reST.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. ``*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. .. sidebar:: Image processing
  215. With the directives from :ref:`linuxdoc <linuxdoc:kfigure>` the build process
  216. is flexible. To get best results in the generated output format, install
  217. ImageMagick_ and Graphviz_.
  218. Searx's sphinx setup includes: :ref:`linuxdoc:kfigure`. Scalable here means;
  219. scalable in sense of the build process. Normally in absence of a converter
  220. tool, the build process will break. From the authors POV it’s annoying to care
  221. about the build process when handling with images, especially since he has no
  222. access to the build process. With :ref:`linuxdoc:kfigure` the build process
  223. continues and scales output quality in dependence of installed image processors.
  224. If you want to add an image, you should use the ``kernel-figure`` and
  225. ``kernel-image`` directives. E.g. to insert a figure with a scalable image
  226. format use SVG (:ref:`svg image example`):
  227. .. code:: reST
  228. .. _svg image example:
  229. .. kernel-figure:: svg_image.svg
  230. :alt: SVG image example
  231. simple SVG image
  232. To refer the figure, a caption block is needed: :ref:`svg image example`.
  233. .. _svg image example:
  234. .. kernel-figure:: svg_image.svg
  235. :alt: SVG image example
  236. simple SVG image
  237. To refer the figure, a caption block is needed: :ref:`svg image example`.
  238. DOT files (aka Graphviz)
  239. ------------------------
  240. With :ref:`linuxdoc:kernel-figure` reST support for **DOT** formatted files is
  241. given.
  242. - `Graphviz's dot`_
  243. - DOT_
  244. - Graphviz_
  245. A simple example is shown in :ref:`dot file example`:
  246. .. code:: reST
  247. .. _dot file example:
  248. .. kernel-figure:: hello.dot
  249. :alt: hello world
  250. DOT's hello world example
  251. .. admonition:: hello.dot
  252. :class: rst-example
  253. .. _dot file example:
  254. .. kernel-figure:: hello.dot
  255. :alt: hello world
  256. DOT's hello world example
  257. ``kernel-render`` DOT
  258. ---------------------
  259. Embed *render* markups (or languages) like Graphviz's **DOT** is provided by the
  260. :ref:`linuxdoc:kernel-render` directive. A simple example of embedded DOT_ is
  261. shown in figure :ref:`dot render example`:
  262. .. code-block:: rst
  263. .. _dot render example:
  264. .. kernel-render:: DOT
  265. :alt: digraph
  266. :caption: Embedded DOT (Graphviz) code
  267. digraph foo {
  268. "bar" -> "baz";
  269. }
  270. Attribute ``caption`` is needed, if you want to refer the figure: :ref:`dot
  271. render example`.
  272. Please note :ref:`build tools <linuxdoc:kfigure_build_tools>`. If Graphviz_ is
  273. installed, you will see an vector image. If not, the raw markup is inserted as
  274. *literal-block*.
  275. .. admonition:: kernel-render DOT
  276. :class: rst-example
  277. .. _dot render example:
  278. .. kernel-render:: DOT
  279. :alt: digraph
  280. :caption: Embedded DOT (Graphviz) code
  281. digraph foo {
  282. "bar" -> "baz";
  283. }
  284. Attribute ``caption`` is needed, if you want to refer the figure: :ref:`dot
  285. render example`.
  286. ``kernel-render`` SVG
  287. ---------------------
  288. A simple example of embedded SVG_ is shown in figure :ref:`svg render example`:
  289. .. code-block:: rst
  290. .. _svg render example:
  291. .. kernel-render:: SVG
  292. :caption: Embedded **SVG** markup
  293. :alt: so-nw-arrow
  294. ..
  295. .. code:: xml
  296. <?xml version="1.0" encoding="UTF-8"?>
  297. <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
  298. baseProfile="full" width="70px" height="40px"
  299. viewBox="0 0 700 400"
  300. >
  301. <line x1="180" y1="370"
  302. x2="500" y2="50"
  303. stroke="black" stroke-width="15px"
  304. />
  305. <polygon points="585 0 525 25 585 50"
  306. transform="rotate(135 525 25)"
  307. />
  308. </svg>
  309. .. admonition:: kernel-render SVG
  310. :class: rst-example
  311. .. _svg render example:
  312. .. kernel-render:: SVG
  313. :caption: Embedded **SVG** markup
  314. :alt: so-nw-arrow
  315. <?xml version="1.0" encoding="UTF-8"?>
  316. <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
  317. baseProfile="full" width="70px" height="40px"
  318. viewBox="0 0 700 400"
  319. >
  320. <line x1="180" y1="370"
  321. x2="500" y2="50"
  322. stroke="black" stroke-width="15px"
  323. />
  324. <polygon points="585 0 525 25 585 50"
  325. transform="rotate(135 525 25)"
  326. />
  327. </svg>
  328. .. _reST lists:
  329. List markups
  330. ============
  331. Bullet list
  332. -----------
  333. List markup (:duref:`ref <bullet-lists>`) is simple:
  334. .. code:: reST
  335. - This is a bulleted list.
  336. 1. Nested lists are possible, but be aware that they must be separated from
  337. the parent list items by blank line
  338. 2. Second item of nested list
  339. - It has two items, the second
  340. item uses two lines.
  341. #. This is a numbered list.
  342. #. It has two items too.
  343. .. admonition:: bullet list
  344. :class: rst-example
  345. - This is a bulleted list.
  346. 1. Nested lists are possible, but be aware that they must be separated from
  347. the parent list items by blank line
  348. 2. Second item of nested list
  349. - It has two items, the second
  350. item uses two lines.
  351. #. This is a numbered list.
  352. #. It has two items too.
  353. Definition list
  354. ---------------
  355. .. sidebar:: definition term
  356. Note that the term cannot have more than one line of text.
  357. Definition lists (:duref:`ref <definition-lists>`) are created as follows:
  358. .. code:: reST
  359. term (up to a line of text)
  360. Definition of the term, which must be indented
  361. and can even consist of multiple paragraphs
  362. next term
  363. Description.
  364. .. admonition:: definition list
  365. :class: rst-example
  366. term (up to a line of text)
  367. Definition of the term, which must be indented
  368. and can even consist of multiple paragraphs
  369. next term
  370. Description.
  371. Quoted paragraphs
  372. -----------------
  373. Quoted paragraphs (:duref:`ref <block-quotes>`) are created by just indenting
  374. them more than the surrounding paragraphs. Line blocks (:duref:`ref
  375. <line-blocks>`) are a way of preserving line breaks:
  376. .. code:: reST
  377. normal paragraph ...
  378. lorem ipsum.
  379. Quoted paragraph ...
  380. lorem ipsum.
  381. | These lines are
  382. | broken exactly like in
  383. | the source file.
  384. .. admonition:: Quoted paragraph and line block
  385. :class: rst-example
  386. normal paragraph ...
  387. lorem ipsum.
  388. Quoted paragraph ...
  389. lorem ipsum.
  390. | These lines are
  391. | broken exactly like in
  392. | the source file.
  393. .. _reST field list:
  394. Field Lists
  395. -----------
  396. .. _Sphinx Field Lists:
  397. https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html
  398. .. sidebar:: bibliographic fields
  399. First lines fields are bibliographic fields, see `Sphinx Field Lists`_.
  400. Field lists are used as part of an extension syntax, such as options for
  401. directives, or database-like records meant for further processing. Field lists
  402. are mappings from field names to field bodies. They marked up like this:
  403. .. code:: reST
  404. :fieldname: Field content
  405. :foo: first paragraph in field foo
  406. second paragraph in field foo
  407. :bar: Field content
  408. .. admonition:: Field List
  409. :class: rst-example
  410. :fieldname: Field content
  411. :foo: first paragraph in field foo
  412. second paragraph in field foo
  413. :bar: Field content
  414. They are commonly used in Python documentation:
  415. .. code:: python
  416. def my_function(my_arg, my_other_arg):
  417. """A function just for me.
  418. :param my_arg: The first of my arguments.
  419. :param my_other_arg: The second of my arguments.
  420. :returns: A message (just for me, of course).
  421. """
  422. Further list blocks
  423. -------------------
  424. - field lists (:duref:`ref <field-lists>`, with caveats noted in
  425. :ref:`reST field list`)
  426. - option lists (:duref:`ref <option-lists>`)
  427. - quoted literal blocks (:duref:`ref <quoted-literal-blocks>`)
  428. - doctest blocks (:duref:`ref <doctest-blocks>`)
  429. Admonitions
  430. ===========
  431. Admonitions: :dudir:`hint`, :dudir:`note`, :dudir:`tip` :dudir:`attention`,
  432. :dudir:`caution`, :dudir:`danger`, :dudir:`error`, , :dudir:`important`, ,
  433. :dudir:`warning` and the generic :dudir:`admonition <admonitions>`.
  434. .. code:: reST
  435. .. admonition:: generic admonition title
  436. lorem ipsum ..
  437. .. hint::
  438. lorem ipsum ..
  439. .. note::
  440. lorem ipsum ..
  441. .. warning::
  442. lorem ipsum ..
  443. .. admonition:: generic admonition title
  444. lorem ipsum ..
  445. .. hint::
  446. lorem ipsum ..
  447. .. note::
  448. lorem ipsum ..
  449. .. tip::
  450. lorem ipsum ..
  451. .. attention::
  452. lorem ipsum ..
  453. .. caution::
  454. lorem ipsum ..
  455. .. danger::
  456. lorem ipsum ..
  457. .. important::
  458. lorem ipsum ..
  459. .. error::
  460. lorem ipsum ..
  461. .. warning::
  462. lorem ipsum ..
  463. .. _KISS: https://en.wikipedia.org/wiki/KISS_principle
  464. .. _readability: https://docs.python-guide.org/writing/style/
  465. .. _Sphinx-Primer:
  466. http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html
  467. .. _reST: https://docutils.sourceforge.io/rst.html
  468. .. _Sphinx Roles:
  469. https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html
  470. .. _Sphinx: http://www.sphinx-doc.org
  471. .. _`sphinx-doc FAQ`: http://www.sphinx-doc.org/en/stable/faq.html
  472. .. _Sphinx markup constructs:
  473. http://www.sphinx-doc.org/en/stable/markup/index.html
  474. .. _`sphinx cross references`:
  475. http://www.sphinx-doc.org/en/stable/markup/inline.html#cross-referencing-arbitrary-locations
  476. .. _sphinx.ext.extlinks:
  477. https://www.sphinx-doc.org/en/master/usage/extensions/extlinks.html
  478. .. _intersphinx: http://www.sphinx-doc.org/en/stable/ext/intersphinx.html
  479. .. _sphinx config: http://www.sphinx-doc.org/en/stable/config.html
  480. .. _Sphinx's autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
  481. .. _Sphinx's Python domain:
  482. http://www.sphinx-doc.org/en/stable/domains.html#the-python-domain
  483. .. _Sphinx's C domain:
  484. http://www.sphinx-doc.org/en/stable/domains.html#cross-referencing-c-constructs
  485. .. _doctree:
  486. http://www.sphinx-doc.org/en/master/extdev/tutorial.html?highlight=doctree#build-phases
  487. .. _docutils: http://docutils.sourceforge.net/docs/index.html
  488. .. _docutils FAQ: http://docutils.sourceforge.net/FAQ.html
  489. .. _linuxdoc: https://return42.github.io/linuxdoc
  490. .. _SVG: https://www.w3.org/TR/SVG11/expanded-toc.html
  491. .. _DOT: https://graphviz.gitlab.io/_pages/doc/info/lang.html
  492. .. _`Graphviz's dot`: https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf
  493. .. _Graphviz: https://graphviz.gitlab.io
  494. .. _ImageMagick: https://www.imagemagick.org