rst and pypandoc

alb al.basili at gmail.com
Mon Mar 2 17:30:02 EST 2015


Hi Dave,

Dave Angel <davea at davea.name> wrote:
[]
>> Rst escapes with "\", but unfortunately python also uses "\" for escaping!
> 
> Only when the string is in a literal.  If you've read it from a file, or 
> built it by combining other strings, or...  then the backslash is just 
> another character to Python.

Holy s***t! that is enlightning. I'm not going to ask why is that so, 
but essentially this changes everything. Indeed I'm passing some strings 
as literal (as my example), some others are simply read from a file 
(well the file is read into a list of dictionaries and then I convert 
one of those keys into latex).

The it would mean that the following text (in a file) should be 
swallowed by python as if the backslash was just another character:

<test.txt>
this is \some text
</test.txt>

unfortunately when I pass that to pypandoc, as if it was restructured 
text, I get the following:

In [36]: f = open('test.txt', 'r')

In [37]: s = f.read()

In [38]: print s
this is \some restructured text.


In [39]: print pypandoc.convert(s, 'latex', format='rst')
this is some restructured text.

what happened to my backslash???

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.

[]
>> As said above, the string is converted by pandoc first and then printed.
>> At this point the escaping becomes tricky (at least to me).
>>
>> In [17]: inp = '\\ref{fig:abc}'
>>
>> In [18]: print pypandoc.convert(inp, 'latex', format='rst')
>> ref\{fig:abc\}
>>
> 
> What did you expect/desire the pyandoc output to be?  Now that you don't 
> have the embedded 0x0a, is there something else that's wrong?

I need to get \ref{fig:abc} in my latex file in order to get a 
reference. It seems to me I'm not able to pass inline text to pandoc and 
every backslash is treated...somehow.

> If it's in the internals of pyandoc, I'll probably be of no help.  But 
> your first question was about escaping;  I'm not sure what it's about now.

It's still about escaping in both python and restructured text since I 
want my substring (is part of the text) to pass unchanged through 
pypandoc. 

Al



More information about the Python-list mailing list