Resolve Substitutions in RestructuredText

I want to take the following restructured text snippet that contains a substitution definition:

text = """

|python|

.. |python| image:: python.jpg
"""

And resolve the definitions so the substitution text is displayed:

resolved_text = """
.. image:: python.jpg

"""

Is there a function or utility in docutils or another module that can do this?


docutils provides publisher functions to use docutils as a library.

So using docutils.core.publish_string could be an option for your usecase.

In [90]: from docutils import core

In [91]: text = '|python|nn.. |python| image:: python.jpgn'

In [92]: print core.publish_string(text)
<document source="<string>">
    <paragraph>
        <image alt="python" uri="python.jpg">
    <substitution_definition names="python">
        <image alt="python" uri="python.jpg">

By default puplish_string uses a pseudoxml writer, which you can see in the output. However if you really want to have the plain text output from your question, you need a custom writer class derived from docutils.writers.Writer . I'm not sure how to implement this, maybe the Sphinx TextWriter could be a starting point.

Seems that if you really only need the simple substitution, using replace on your text would be a simpler solution, if you need more complicated things, implement this using docutils is complicated, too.


Have a look at the Docutils Hacker's Guide. It explains how docutils works.

You might get away with applying an appropriate Transform to the node tree that is generated by parsing the input file. After applying the transform you should use a Writer object to output ReStructuredText again. This writer doesn't exist yet, so you'd have to create that first.


Not sure that I fully understand the question, but here is a stab at the extraction using list comprehension:

extracted_line = [x for x in text.split("n") if x[:2] == ".."][0]
resolved_text = """{}""".format(extracted_line.replace("|python|",""))

You'll need to add some logic if more than one occurrence of the sub def is expected.

链接地址: http://www.djcxy.com/p/69910.html

上一篇: 如何跟踪checkedListBox C#中的检查?

下一篇: 在RestructuredText中解决替换