Debian Codegraph @

core/docs/source/usage/config.rst

raw
1 Configuration
2 =============
3
4 .. contents::
5
6 ``rstcheck-core``'s config system knows three sources:
7
8 - Inline comments (*for config and flow control instructions*)
9 - CLI options
10 - Config files (*INI and TOML*)
11
12
13 Order of application
14 --------------------
15
16 The config sources apply according to a set of rules:
17
18 #. *Flow control instructions* from inline comments:
19
20 - **always apply** regardless of other config.
21 - are **expressive** and say for themselves what they apply to.
22
23 #. Config from inline comments:
24
25 - **always apply** regardless of other config.
26 - **always apply for the whole file** regardless where they are placed.
27 - is **added** to the remaining config and does **not overwrite** it.
28
29 #. CLI options **always overwrite** config coming from a file.
30 #. File config has the lowest priority.
31
32
33 Configuration sources
34 ---------------------
35
36 Now let's take a deeper look at the different sources of config.
37
38
39 Inline comments
40 ~~~~~~~~~~~~~~~
41
42 Inline comments are simply rst comments starting with ``rstcheck:``.
43 There are two types of inline comments:
44
45 - Simple inline config e.g. ``ignore-languages=python`` which follows the syntax of ``key=value``.
46 - Flow control instructions e.g. ``ignore-next-code-block`` which follows the syntax of
47 ``words-divided-by-dashes``.
48
49 Here is an example with both of them:
50
51 .. code-block:: rst
52
53 Example
54 =======
55
56 .. rstcheck: ignore-next-code-block
57 .. code-block:: python
58
59 print("Hello World")
60
61 .. rstcheck: ignore-languages=python
62
63
64 Configuration files
65 ~~~~~~~~~~~~~~~~~~~
66
67 ``rstcheck-core`` has an automatic config file detection mechanic. This mechanic includes
68 the following config files sorted by priority:
69
70 #. .rstcheck.cfg
71 #. pyproject.toml
72 #. setup.cfg
73
74 When a directory is searched for a config file, the first found config section is
75 taken and the search is not aborted on the first file found.
76 This means that if you for example have a pyproject.toml file without a matching config
77 section and an setup.cfg file with a matching section, the section from setup.cfg would be used.
78 pyproject.toml would be searched first, but nothing would be found and so the search would continue.
79
80 For each rst source file that is linted, its parent directory is searched for a config file.
81 If the parent directory has no config file with a matching config section the parent's
82 parent directory is searched next. This continues up the directory tree until the root directory.
83 If no config file is found, the default values for each setting apply.
84
85 This whole mechanic is deactivated, when a config file or directory is explicitly set.
86 See the `Configuration file`_ section for more information on setting a config file/directory.
87
88 ``rstcheck-core`` supports two types of config formats: **INI** and **TOML**.
89 They are written pretty similar, but have some differences.
90 The two following sections explain both formats.
91
92 Files ending on ``.toml`` are parsed as TOML files.
93 Every other file is parsed as an INI file.
94
95 If ``.rstcheck.cfg`` does not contain a valid section a warning is printed.
96
97
98 INI format
99 ^^^^^^^^^^
100
101 In INI format all config related to ``rstcheck-core`` must go into a ``[rstcheck]`` section.
102
103 The default INI format applies: ``key = value``.
104 Lists are comma-separated strings, which can be multiline.
105 Whitespace before and after a key or value is ignored.
106 Trailing commas are optional.
107
108 Here is an example:
109
110 .. code-block:: ini
111
112 [rstcheck]
113 report_level=WARNING
114 ignore_directives =
115 one,
116 two,
117 three,
118 ignore_roles=src, RFC
119 ignore_substitutions=
120 image_link
121 ignore_languages=
122 python,
123 cpp
124 ignore_messages=(Document or section may not begin with a transition\.$)
125
126
127 TOML format
128 ^^^^^^^^^^^
129
130 .. note::
131
132 TOML format is only supported when the python library ``tomli`` is importable if the used
133 python version is lower than 3.11.
134 See the :ref:`installation:Installation` section for more information.
135
136 In TOML format all config related to ``rstcheck-core`` must go into the ``[tool.rstcheck]``
137 dictionary. This is due to the python convention for the ``pyproject.toml`` file, which
138 ``rstcheck-core`` uses for all TOML files.
139
140 The official TOML syntax applies here, so strings are strings and lists are lists for example.
141
142 Here is an example:
143
144 .. code-block:: toml
145
146 [tool.rstcheck]
147 report_level = "WARNING"
148 ignore_directives = [
149 "one",
150 "two",
151 "three",
152 ]
153 ignore_roles = ["src", "RFC"]
154 ignore_substitutions = [
155 "image_link"
156 ]
157 ignore_languages = [
158 "python",
159 "cpp"
160 ]
161 ignore_messages = "(Document or section may not begin with a transition\.$)"
162
163
164 Configuration options
165 ---------------------
166
167 Now it's time for all the available settings you can set.
168
169
170 Configuration file
171 ~~~~~~~~~~~~~~~~~~
172
173 Supported sources:
174
175 - CLI (``--config PATH`` )
176
177 With the ``--config`` CLI option you can set a config file or directory.
178 The path may be relative or absolute.
179
180 If the passed path does not exist the runner exits with an error, which is logged.
181
182 If the path is a literal ``NONE``, no file is loaded or directory searched, this includes
183 the automatic config file detection mechanic.
184
185 When the path points to a file, this concrete file is read and searched for a matching
186 config section.
187 If no section is found a warning is logged and no file config is used.
188
189 When the path is a directory, this directory is search for a config file, like described
190 in the earlier `Configuration files`_ section, except that only this directory is search and
191 not the directory tree.
192
193
194 Recursive resolution
195 ~~~~~~~~~~~~~~~~~~~~
196
197 Supported sources:
198
199 - CLI (``--recursive`` or ``-r``)
200
201 By default only files passed to the CLI runner are checked and directories are ignored.
202 When this config is set, passed directories are searched recursively for rst source files.
203
204
205 Report level
206 ~~~~~~~~~~~~
207
208 Supported sources:
209
210 - CLI (``--report-level LEVEL``)
211 - File (key: ``report_level``, value: LEVEL)
212
213 The level at which linting issues should be printed. The following levels are supported:
214
215 - INFO (default)
216 - WARNING
217 - ERROR
218 - SEVERE
219 - NONE
220
221 This currently only applies to issues with rst source.
222 Issues in code blocks are on ERROR level and always printed,
223 even if the level is set to SEVERE or NONE.
224
225 The level can be set case insensitive.
226
227
228 Logging level
229 ~~~~~~~~~~~~~
230
231 Supported sources:
232
233 - CLI (``--log-level LEVEL``)
234
235 The level at which additional information besides linting issues should be printed.
236 The following levels are supported:
237
238 - DEBUG
239 - INFO
240 - WARNING (default)
241 - ERROR
242 - CRITICAL
243
244 The level can be set case insensitive.
245
246
247 Ignore directives
248 ~~~~~~~~~~~~~~~~~
249
250 Supported sources:
251
252 - Inline comments (key: ``ignore-directives``, value: list of directives)
253 - CLI (``--ignore-directives D1,D2,...``)
254 - File (key: ``ignore_directives``, value: list of directives)
255
256 A list of directives to ignore while checking rst source.
257
258
259 Ignore roles
260 ~~~~~~~~~~~~
261
262 Supported sources:
263
264 - Inline comments (key: ``ignore-roles``, value: list of roles)
265 - CLI (``--ignore-roles R1,R2,...``)
266 - File (key: ``ignore_roles``, value: list of roles)
267
268 A list of roles to ignore while checking rst source.
269
270
271 Ignore substitutions
272 ~~~~~~~~~~~~~~~~~~~~
273
274 Supported sources:
275
276 - Inline comments (key: ``ignore-substitutions``, value: list of substitutions)
277 - CLI (``--ignore-substitutions S1,S2,...``)
278 - File (key: ``ignore_substitutions``, value: list of substitutions)
279
280 A list of substitutions to ignore while checking rst source.
281
282
283 Ignore specific code-block languages
284 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285
286 Supported sources:
287
288 - Inline comments (key: ``ignore-languages``, value: list of languages)
289 - CLI (``--ignore-languages L1,L2,...``)
290 - File (key: ``ignore_languages``, value: list of languages)
291
292 A list of languages to ignore for code blocks in rst source.
293 Unsupported languages are ignored automatically.
294
295 Supported languages are:
296
297 - Bash
298 - Doctest
299 - C (C99)
300 - C++ (C++11)
301 - JSON
302 - XML
303 - Python
304 - reStructuredText
305 - YAML
306
307
308 Ignore specific error messages
309 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310
311 Supported sources:
312
313 - CLI (``--ignore-messages REGEX_STRING``)
314 - File (key: ``ignore_messages``, value: regular expression string)
315
316 A list of linting issue messages to ignore while checking rst source and code blocks.
317
318 .. note::
319
320 In TOML format a list of strings is also valid. The list's entries will be
321 concatenated with the OR operator "|" between each entry.
322
323
324 Control Flow instructions
325 -------------------------
326
327 There are also control flow instructions which are only available as inline comments.
328 They change the flow of checking the rst source, hence the name.
329
330
331 .. _skipping-code-blocks:
332
333 Skipping code blocks
334 ~~~~~~~~~~~~~~~~~~~~
335
336 With the ``ignore-next-code-block`` flow control instruction you can skip single code blocks.
337 This way you don't have to use the heavy tools like ignoring a whole language or directive.
338
339 The instruction **must** be placed in the line directly above the code block directive like so:
340
341
342 .. code-block:: rst
343
344 .. rstcheck: ignore-next-code-block
345 .. code-block:: python
346
347 print("Hello world")
348
349
350 Examples with explanation
351 -------------------------
352
353 These examples are cases to show concepts of configuration in ``rstcheck-core``.
354 They don't always follow best practices.
355
356
357 Only inline comments
358 ~~~~~~~~~~~~~~~~~~~~
359
360 .. code-block:: rst
361
362 Example
363 =======
364
365 .. rstcheck: ignore-next-code-block
366 .. code-block:: python
367
368 print("Here is an error."
369
370 .. rstcheck: ignore-languages=python
371
372 In this example the code-block would be ignored/skipped due to the flow control instruction.
373 But the code-block's language is python which is on the ignore list for languages, because of the
374 config at the bottom. This means if you remove the flow control instruction, the
375 code-block would still be skipped and the error inside would ignored.