Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add rst format in nodes for directive?

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
        ]
like image 441
uralbash Avatar asked Dec 18 '15 07:12

uralbash


People also ask

What is RST extension?

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.

What is a directive Sphinx?

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.

What is RST file in Python?

reStructuredText (RST, ReST, or reST) is a file format for textual data used primarily in the Python programming language community for technical documentation.

What is the RST text for the code block example above?

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!')

What is the directive class for the note directive?

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.

What is the syntax of a reStructuredText directive?

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.

How do I use warnings in RST?

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:


1 Answers

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.

like image 198
Blair Avatar answered Oct 12 '22 20:10

Blair