Discussion:
[Docutils-develop] I'm generating reStructuredText from literate source files. How can I post-process error messages to point to the original sources?
Clément Pit-Claudel
2017-06-01 20:29:42 UTC
Permalink
Hi all,

I'm using docutils to compile documents obtained by preprocessing literate source files. I currently use a two-phase build system, in which I first translate to reStructuredText using a custom pre-processor, and then export to HTML with docutils. The preprocessing phase is very simple: it removes comment markers here and there and indent code to produce a valid reStructuredText document.

Unfortunately, this setup means that line numbers in error messages refer to the preprocessed version of the document — not the original one. I'd like to teach the reStructuredText parser to report errors relative to the original document instead (and, when snippets are printed as part of an error message, to print snippets from the original document too). I have already extended my preprocessor to create a table mapping each line in the reStructuredText output to the corresponding line in the original source file, so it's mostly a matter of knowing where to plug this table.

What's the right way to proceed? (and, is this the right place to ask?)

Thanks,
Clément.

PS: For completeness, the parser that I currently use looks like this::

class LiterateParser(docutils.parsers.Parser):
supported = ('lit')
settings_spec = ('Literate Parser Options', None, ())
config_section = 'Literate parser'
config_section_dependencies = ('parsers',)

def __init__(self):
self.parser = docutils.parsers.rst.Parser()

def get_transforms(self):
return self.parser.get_transforms()

@staticmethod
def preprocess(literate_string):


def parse(self, literate_string, document):
"""Parse `inputstring` and populate `document`, a document tree."""
linemap, rst_string = LiterateParser.preprocess(literate_string)
self.parser.parse(rst_string, document)

The source files look like this::

/// A literate file!
/// ================
///
/// Here is some *code*.

int main() { return 0; }

And the generated reStructuredText looks like this::

A literate file!
================

Here is some *code*.

.. literate-block::

int main() { return 0; }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Docutils-develop mailing list
Docutils-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-develop

Please use "Reply All" to rep
Guenter Milde
2017-06-01 21:17:52 UTC
Permalink
Post by Clément Pit-Claudel
Hi all,
I'm using docutils to compile documents obtained by preprocessing
literate source files.
...
Post by Clément Pit-Claudel
Unfortunately, this setup means that line numbers in error messages
refer to the preprocessed version of the document — not the original
one.
...
Post by Clément Pit-Claudel
What's the right way to proceed? (and, is this the right place to ask?)
I recommend trying PyLit__, the 2-way text<->code converter that keeps line
numbers.

Günter

__ https://pypi.python.org/pypi/pylit


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Docutils-develop mailing list
Docutils-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-develop

Please

Loading...