Debian Codegraph @

docs/source/usage/config.rst

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