parse generated output from program

20 views
Skip to first unread message

DexterMagnific

unread,
Jan 17, 2020, 4:56:38 AM1/17/20
to sphinx-users
Dear all,

I have some script that generates ReST content. I'd like to call this script from inside the rst document:

blah blah blah

.. execute-program:: generate-content.py myfile.bin

I know there is sphinxcontrib.programoutput, but this extension inserts the output of the program. What i'd like to is that this output be interpreted by the parser as a rest content.

Do you have any idea on how to achieve this ?

Thanks

Peter Burdine

unread,
Jan 17, 2020, 11:02:21 AM1/17/20
to sphinx-users
Make your own directive that runs your command.  You can probably use the programoutput extension code as a reference for how to make the directive.  Then use the nested_parse_with_titles function:
from docutils import nodes
from docutils.statemachine import ViewList    

def run(self):
    new_content
= command.output
    node
= nodes.Element()
    node
.document = self.state.document
    env
= self.state.document.settings.env
    sphinx
.util.nodes.nested_parse_with_titles(self.state,
                                               
ViewList(new_content.splitlines(), source=env.docname),
                                               node
)
   
return node.children

--Peter
Reply all
Reply to author
Forward
0 new messages