Includes
Two directives splice another file into this one: .. include:: brings in
reStructuredText, which is parsed as if it had been typed here, and
.. literalinclude:: brings in a source file, which is shown verbatim.
Both read their file while parsing, so under Bazel the file has to be
declared in the library's parse_data attribute. A file that is included
must not also appear in srcs, or it would be published as a page of its
own as well as being spliced in here.
Including reStructuredText
The whole of a fragment, parsed in place:
The text between the markers below is what a selective include picks up.
Every function in this example takes the same two parameters:
nameThe thing to greet, as a string.
timesHow often to repeat the greeting.
This closing paragraph is outside the markers, so a
:start-after:/:end-before: include leaves it behind.
Selecting part of a fragment
The same fragment carries BEGIN/END markers, so a document can take
just the part it wants. Note that the markers themselves stay out:
Every function in this example takes the same two parameters:
nameThe thing to greet, as a string.
timesHow often to repeat the greeting.
docutils' :start-line:/:end-line: select by index instead. They are
0-based and the end is exclusive — the one place in reStructuredText that
counts from zero:
The text between the markers below is what a selective include picks up.
Including a file verbatim
:literal: shows the fragment's own markup rather than interpreting it,
and :code: does the same with a language for highlighting:
Including source files
.. literalinclude:: takes a path as its argument and the language as an
option, unlike .. code-block:::
"""A tiny module, shown by ``includes.rst`` via ``.. literalinclude::``."""
# [greet]
"""Return a greeting for ``name``, repeated ``times`` times."""
= f
return
# [/greet]
return 0
With no :language: it inherits from the enclosing .. highlight::,
exactly as a .. code-block:: with no argument does.
Selecting lines
:lines: takes numbers and ranges, and :lineno-match: numbers the
result from the line it really starts at in the file:
7
8 """Return a greeting for ``name``, repeated ``times`` times."""
9 = f
10 return
11# [/greet]The marker comments in the file are the more maintainable way to say the same thing, since they survive an edit above them:
"""Return a greeting for ``name``, repeated ``times`` times."""
= f
return Framing the excerpt
:prepend: and :append: add lines around the selection, which is how an
excerpt from the middle of a file is made to stand on its own:
"""Return a greeting for ``name``, repeated ``times`` times."""
= f
return
# ... and so onShowing a change
:diff: shows the patch between another file and this one, rather than
either file's contents:
# [greet]
-def greet(name: str) -> str:
- """Return a greeting for ``name``."""
- return f"Hello, {name}!"
+def greet(name: str, times: int = 1) -> str:
+ """Return a greeting for ``name``, repeated ``times`` times."""
+ line = f"Hello, {name}!"
+ return "\n".join([line] * times)
# [/greet]
Line numbers and captions
:linenos:, :lineno-start:, :name: and :class: mean exactly
what they do on a .. code-block::, and a :caption: with no value takes
the filename:
100
101
102 return 0A reference to The entry point resolves to the block above.