|
1 | 1 | Writing about code
|
2 | 2 | ==================
|
3 | 3 |
|
| 4 | +When writing articles, you need to format code specially, separating it from |
| 5 | +other text. This document will guide you through typical cases when |
| 6 | +it is recommended to use code highlighting. |
| 7 | + |
| 8 | +Defining what code is |
| 9 | +--------------------- |
| 10 | + |
| 11 | +In general, code is any text, processed by a machine. It is also probably code |
| 12 | +if the expression contains characters that ordinary words do not have, |
| 13 | +such as ``_, {}, [ ], .``. |
| 14 | +Also, you should format the expression as code if it fits at least one |
| 15 | +of the items in the list below: |
| 16 | + |
| 17 | +* parts of a programming language: names of classes, variables, and functions, |
| 18 | + short expressions, data types and so on, |
| 19 | +* multiline fragments of application logs, |
| 20 | +* example link which the reader will not open: ``example.com``, ``https://example.com:80``, |
| 21 | +* parts of URL, like port number, |
| 22 | +* package names, |
| 23 | +* CLI app names. |
| 24 | + |
| 25 | +Items we don't format as code: |
| 26 | + |
| 27 | +* names of products, organizations and services, for example, Tarantool, |
| 28 | + memtx, vinyl |
| 29 | +* well-established terms such as stdin and stdout |
| 30 | + |
| 31 | +Keep in mind that grammar doesn't apply to code, even inline. |
| 32 | + |
| 33 | +* Correct: "use ``shellcheck`` to analyze your Bash code". |
| 34 | +* Incorrect: "``shellcheck`` your Bash code". Please do not use code |
| 35 | + as a verb. |
| 36 | +* Even worse: "shellcheck your Bash code". There's no such word in English |
| 37 | + and we don't explain what to use. |
| 38 | +* Cursed: "try ``shellchecking`` your Bash code". There's no such word |
| 39 | + and no such application. |
| 40 | + |
| 41 | +Code blocks and inline code |
| 42 | +--------------------------- |
| 43 | + |
| 44 | +If you have to choose between inline code and code block highlighting, |
| 45 | +pay attention to the following guidelines: |
| 46 | + |
4 | 47 | Code snippets
|
5 |
| -------------- |
| 48 | +~~~~~~~~~~~~~ |
| 49 | + |
| 50 | +Use code blocks when you have to highlight multiple lines of code. |
| 51 | +Also, use it if your code snippet contains a standalone element |
| 52 | +that is not a part of the article's text. |
6 | 53 |
|
7 | 54 | For code snippets, we use the ``code-block:: language``
|
8 |
| -`directive <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block>`_ |
9 |
| -with an appropriate highlighting language. |
| 55 | +`directive <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block>`_. |
| 56 | +You can enable syntax highlighting if you specify the language for the snippet. |
10 | 57 | The most commonly used highlighting languages are:
|
11 | 58 |
|
12 | 59 | * ``tarantoolsession``—interactive Tarantool session,
|
13 | 60 | where command lines start with ``tarantlool>`` prompt.
|
14 |
| -* ``console``—interactive console session, where command lines start with ``$`` or ``#``. |
| 61 | +* ``console``—interactive console session, where command lines |
| 62 | + start with ``$`` or ``#``. |
15 | 63 | * ``lua``, ``bash`` or ``c`` for programming languages.
|
16 | 64 | * ``text`` for cases when we want the code block to have no highlighting.
|
17 | 65 |
|
18 |
| -Sphinx uses the Pygments library for highlighing source code. |
| 66 | +Sphinx uses the Pygments library for highlighting source code. |
19 | 67 | For a complete list of possible languages, see the
|
20 | 68 | `list of Pygments lexers <https://pygments.org/docs/lexers/>`_.
|
21 | 69 |
|
22 | 70 | For example, a code snippet in Lua:
|
23 | 71 |
|
24 |
| -.. literalinclude:: _includes/lua.rst |
25 |
| - :language: rst |
| 72 | + .. literalinclude:: _includes/lua.rst |
| 73 | + :language: rst |
26 | 74 |
|
27 | 75 | Lua syntax is highlighted in the output:
|
28 | 76 |
|
29 |
| -.. include:: _includes/lua.rst |
| 77 | + .. include:: _includes/lua.rst |
| 78 | + |
| 79 | +Note that in code blocks you can write comments and translate them: |
| 80 | + |
| 81 | + .. literalinclude:: _includes/comment.rst |
| 82 | + :language: rst |
30 | 83 |
|
31 | 84 | Inline code
|
32 |
| ------------ |
| 85 | +~~~~~~~~~~~ |
| 86 | + |
| 87 | +Use inline code when you need to wrap a short snippet of code in text, such as |
| 88 | +variable name or function definition. Keep in mind that inline code |
| 89 | +doesn't have syntax highlighting. |
33 | 90 |
|
34 | 91 | To format some inline text as ``code``, enclose it with double ````` characters
|
35 | 92 | or use the ``:code:`` role:
|
36 | 93 |
|
37 |
| -.. literalinclude:: _includes/inline-code.rst |
38 |
| - :language: rst |
| 94 | + .. literalinclude:: _includes/inline-code.rst |
| 95 | + :language: rst |
39 | 96 |
|
40 | 97 | Both options produce the same output:
|
41 | 98 |
|
42 |
| -.. include:: _includes/inline-code.rst |
| 99 | + .. include:: _includes/inline-code.rst |
| 100 | + |
| 101 | + |
| 102 | +Notes on using inline-code |
| 103 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 104 | + |
| 105 | +* If you have expressions such as ``id==4``, you should format the whole |
| 106 | + expression as code inline. Also, you can use the words "equals", |
| 107 | + "doesn't equal" or other similar words without formatting expression |
| 108 | + as code. Both variants are correct. |
| 109 | + |
| 110 | +* Inline code can be used to highlight expressions that are hard to read, |
| 111 | + for example, words containing ``il``, ``Il`` or ``O0``. |
| 112 | + |
43 | 113 |
|
44 | 114 | Highlighting variables in code
|
45 | 115 | ------------------------------
|
46 | 116 |
|
47 |
| -If you need to highlight variables in code inline, use the ``:samp:`` role, |
48 |
| -like this: |
| 117 | +If you need to mark up a placeholder inside code inline, use the ``:samp:`` |
| 118 | +role, like this: |
| 119 | + |
| 120 | + .. literalinclude:: _includes/samp.rst |
| 121 | + :language: rst |
| 122 | + |
| 123 | +And you will get this: |
| 124 | + |
| 125 | + .. include:: _includes/samp.rst |
| 126 | + |
| 127 | +If you need to mark up a placeholder in code block, use |
| 128 | +the following syntax: |
| 129 | + |
| 130 | + .. literalinclude:: _includes/highlight.rst |
| 131 | + :language: rst |
| 132 | + |
| 133 | +The output will look like this: |
| 134 | + |
| 135 | + .. include:: _includes/highlight.rst |
| 136 | + |
| 137 | +Formatting file and directory names |
| 138 | +----------------------------------- |
| 139 | + |
| 140 | +If you need to highlight some file standalone name or path to file in text, use |
| 141 | +the ``:file:`` role. |
| 142 | +You can use curly braces inside this role |
| 143 | +to mark up a replaceable part: |
49 | 144 |
|
50 |
| -.. literalinclude:: _includes/samp.rst |
51 |
| - :language: rst |
| 145 | + .. literalinclude:: _includes/file.rst |
| 146 | + :language: rst |
52 | 147 |
|
53 | 148 | And you will get this:
|
54 | 149 |
|
55 |
| -.. include:: _includes/samp.rst |
| 150 | + .. include:: _includes/file.rst |
0 commit comments