Discussion:
[Docutils-users] Using DocUtils to parse RST with unknown directives
Austin Bart
2014-07-21 18:07:02 UTC
Permalink
I have some RST files that use a number of custom directives. I need to get
a tree-based representation (e.g., like I'd get from publish_doctree) of
these files before I pass them into Sphinx (via Paver) and have them parsed
properly. I don't want any sophisticated processing of the directives - I
just want to know where in the document any given directive occurs, along
with its options and content.

If I try and just pass the files into publish_doctree, it gets upset
because it hasn't been told about all the custom directives that are
possible. Is my only option to implement my own parser? Or is there some
way to tell docutils to not Except on unknown directives? The following
conversation makes me think that it might not be possible:

http://article.gmane.org/gmane.text.docutils.user/4176/match=publish_doctree

Please note that I'm not subscribed to this listserv, so please CC me on
responses! Thanks for your time and help!
--
Austin Cory Bart
Computer Science PhD student at Virginia Tech
http://www.acbart.com
David Goodger
2014-07-22 16:46:03 UTC
Permalink
Post by Austin Bart
I have some RST files that use a number of custom directives. I need to get
a tree-based representation (e.g., like I'd get from publish_doctree) of
these files before I pass them into Sphinx (via Paver) and have them parsed
properly. I don't want any sophisticated processing of the directives - I
just want to know where in the document any given directive occurs, along
with its options and content.
If I try and just pass the files into publish_doctree, it gets upset because
it hasn't been told about all the custom directives that are possible. Is my
only option to implement my own parser? Or is there some way to tell
docutils to not Except on unknown directives? The following conversation
http://article.gmane.org/gmane.text.docutils.user/4176/match=publish_doctree
You can adjust the halt_level so that "error" level does not halt the
processing. You'd set halt_level=4 (one level above error = 3). Then
the resulting doctree would contain error messages with details about
each directive.

Another option would be some monkey-patching. Replace
docutils.parsers.rst.directives._directive_registry (in the
__init__.py file) with a collections.defaultdict(your_function),
duplicating the original _directive_registry's data. The
implementation of your_function would return a (module name, class
name) tuple; you would have to implement these as well (possibly also
monkey-patched).

I recommend the first option, much easier. If it doesn't do what you
want, the second option may do it for you.
--
David Goodger <http://python.net/~goodger>
Post by Austin Bart
Please note that I'm not subscribed to this listserv, so please CC me on
responses! Thanks for your time and help!
--
Austin Cory Bart
Computer Science PhD student at Virginia Tech
http://www.acbart.com
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Docutils-users mailing list
https://lists.sourceforge.net/lists/listinfo/docutils-users
Please use "Reply All" to reply to the list.
Loading...