reST.rst 19 KB

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