Resolve Substitutions in RestructuredText
Asked Answered
V

3

7

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?

Vo answered 9/3, 2013 at 15:29 Comment(1)
Looks like a duplicate of Is there a reST Writer?Bubonocele
L
2

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|\n\n.. |python| image:: python.jpg\n'

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.

Lothario answered 18/3, 2013 at 16:36 Comment(1)
Thanks for this. It looks like this is the only approach to take, I was hoping there would be something already available, but it appears not.Vo
G
1

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.

Glut answered 12/3, 2013 at 22:51 Comment(1)
Thanks for the approach. bmu expanded slightly further on this.Vo
K
1

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.

Kurtz answered 15/3, 2013 at 5:15 Comment(1)
Thanks - this is fine solution for a few replacements, but I was hoping for a robust solution that would work on any rstVo

© 2022 - 2024 — McMap. All rights reserved.