How I can use rst in nodes? For example I want to output icluded file about.rst
class Foo(Directive):
def run(self):
return [
nodes.Text("**adad**"), # <-- Must be a bold text
nodes.Text(".. include:: about.rst"), # <-- Must include file
]
An RST file is a text file that contains code written in the reStructuredText markup language. reStructuredText is used to apply basic styles and formatting to plain text documents.
As previously discussed, a directive is a generic block of explicit markup. While Docutils provides a number of directives, Sphinx provides many more and uses directives as one of the primary extension mechanisms. See Domains for roles added by domains.
reStructuredText (RST, ReST, or reST) is a file format for textual data used primarily in the Python programming language community for technical documentation.
The RST text for the code block example above is: The following examples show code blocks in different languages, with automatic syntax color coding. alert('Hello, World!') .. code-block:: javascript alert ('Hello, World!')
The directive class for the "note" directive simply derives from a generic admonition directive class: Note that the only thing distinguishing the various admonition directives is the element (node class) generated.
The syntax of directives is detailed in the reStructuredText Markup Specification, and standard directives are described in reStructuredText Directives. Directives are a reStructuredText markup/parser concept. There is no "directive" document tree element, no single element that corresponds exactly to the concept of directives.
Use a warning for information the user must understand to avoid negative consequences. Warnings are formatted in the same way as notes. In the same way, lines must be broken and indented under the warning tag. This is how the warning appears in RST:
You can construct a ViewList
of your raw rst data (one line per entry), get Sphinx to parse that content, and then return the nodes Sphinx gives you. The following worked for me:
from docutils import nodes
from docutils.statemachine import ViewList
from sphinx.util.compat import Directive
from sphinx.util.nodes import nested_parse_with_titles
class Foo(Directive):
def run(self):
rst = ViewList()
# Add the content one line at a time.
# Second argument is the filename to report in any warnings
# or errors, third argument is the line number.
rst.append("**adad**", "fakefile.rst", 10)
rst.append("", "fakefile.rst", 11)
rst.append(".. include:: about.rst", "fakefile.rst", 12)
# Create a node.
node = nodes.section()
node.document = self.state.document
# Parse the rst.
nested_parse_with_titles(self.state, rst, node)
# And return the result.
return node.children
def setup(app):
app.add_directive('foo', Foo)
I had to do something similar for a project --- in lieu of any (easily found) relevant documentation I used the source of the inbuilt autodoc extension as a guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With