rst and pypandoc
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Mar 2 18:32:22 EST 2015
alb wrote:
> In [39]: print pypandoc.convert(s, 'latex', format='rst')
> this is some restructured text.
>
> what happened to my backslash???
You'll need to read your pypandoc documentation to see what it says about
backslashes.
> If I try to escape my backslash I get something worse:
>
> In [40]: f = open('test.txt', 'r')
>
> In [41]: s = f.read()
>
> In [42]: print s
> this is \\some restructured text.
>
>
> In [43]: print pypandoc.convert(s, 'latex', format='rst')
> this is \textbackslash{}some restructured text.
>
> since a literal backslash gets converted to a literal latex backslash.
Why is this a problem? Isn't the ultimate aim to pass it through latex,
which will then covert the \textbackslash{} back into a backslash? If not,
I have misunderstood something.
If not, you could do something like this:
s = 'this is %(b)ssome restructured text.'
t = pypandoc.convert(s, 'latex', format='rst')
assert t == 'this is %(b)ssome restructured text.'
print t % {'b': '\\'}
taking care to escape any actual percent signs in your text as '%%'.
To be clear, what I'm doing here is using Python's % string interpolation to
post-process the Latex output:
- replace every '%' in your input string with '%%';
- replace every backslash in your input string with '%(b)s';
- convert;
- post-process using %.
--
Steven
More information about the Python-list
mailing list