Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86795

Re: rst and pypandoc

Date 2015-03-02 18:23 -0500
From Dave Angel <davea@davea.name>
Subject Re: rst and pypandoc
References <cliji5FvctU1@mid.individual.net> <54f458a5$0$13003$c3e8da3$5496439d@news.astraweb.com> <clj866F68tpU1@mid.individual.net> <mailman.39.1425305311.13471.python-list@python.org> <clk777Feal8U3@mid.individual.net>
Newsgroups comp.lang.python
Message-ID <mailman.71.1425339347.13471.python-list@python.org> (permalink)

Show all headers | View raw


On 03/02/2015 05:40 PM, alb wrote:
> Hi Dave,
>
> Dave Angel <davea@davea.name> wrote:
> []
>>>> or use a raw string:
>>>>
>>>> i = r'\\ref{fig:abc}'
>>
>> Actually that'd be:
>>     i = r'\ref{fig:abc}'
>
> Could you explain why I then see the following difference:
>
> In [56]: inp = r'\\ref{fig:abc}'

print inp
    and you should get
      \\ref{fig:abc}

>
> In [57]: print pypandoc.convert(inp, 'latex', format='rst')
> \textbackslash{}ref\{fig:abc\}
>
>
> In [58]: inp = r'\ref{fig:abc}'

print inp
     and you should get
        \ref{fig:abc}

This is NOT the same.

The rules are not arbitrary.  They're quite necessary, and it's the same 
for lots of different languages.  When in a regular literal, the 
backslash is an escape character that combines with the following 
character.  When in a raw literal, the backslash is a backslash, unless 
it's at the end of the string, in which case it's not the end of the 
string, it's an escaped quotation.  (Or something.  Just don't use 
*trailing* backslash in a raw literal)

>
> In [59]: print pypandoc.convert(inp, 'latex', format='rst')
> ref\{fig:abc\}
>
> The two results are clearly *not* the same, even though the two inp
> /claim/ to be the same...
>

When I said backslashes are not special in data read from a file, I 
should also say neither are quotes, or tabs, or anything else.  Python 
just reads them in, and stuffs them into a string object.  Newlines are 
special if you use readline(), but if you use read(), they're not 
special either (except on MSDOS compatible variants, which use two bytes 
for newline.  Even there, if you read a file in "b" mode, they're not 
special either.

So your code is going to mostly be getting strings from files, or from 
calculations, and these backslashes won't be special.  It's only in 
*testing* that you usually deal with this literal stuff.  Or in places 
where the data is fixed, and hardcoded in the source.


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 07:59 +0000
  Re: rst and pypandoc Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-03-02 12:03 +0100
  Re: rst and pypandoc Dave Angel <davea@davea.name> - 2015-03-02 07:03 -0500
    Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 12:36 +0000
  Re: rst and pypandoc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-02 23:33 +1100
    Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 13:51 +0000
      Re: rst and pypandoc Dave Angel <davea@davea.name> - 2015-03-02 09:08 -0500
        Re: rst and pypandoc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 01:43 +1100
          Re: rst and pypandoc Dave Angel <davea@davea.name> - 2015-03-02 13:55 -0500
          Re: rst and pypandoc Ben Finney <ben+python@benfinney.id.au> - 2015-03-03 06:09 +1100
          Re: rst and pypandoc Dave Angel <davea@davea.name> - 2015-03-02 14:16 -0500
        Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 22:30 +0000
          Re: rst and pypandoc Chris Angelico <rosuav@gmail.com> - 2015-03-03 09:51 +1100
          Re: rst and pypandoc Ben Finney <ben+python@benfinney.id.au> - 2015-03-03 10:18 +1100
          Re: rst and pypandoc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 10:32 +1100
            Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-03 20:35 +0000
        Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 22:40 +0000
          Re: rst and pypandoc Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-02 23:08 +0000
            Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-03 20:37 +0000
          Re: rst and pypandoc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 10:22 +1100
            Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-03 20:46 +0000
          Re: rst and pypandoc Dave Angel <davea@davea.name> - 2015-03-02 18:23 -0500
      Re: rst and pypandoc MRAB <python@mrabarnett.plus.com> - 2015-03-02 14:37 +0000
        Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-02 22:37 +0000
          Re: rst and pypandoc Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-03-03 19:40 +1300
            Re: rst and pypandoc al.basili@gmail.com (alb) - 2015-03-03 20:50 +0000
              Re: rst and pypandoc Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-03-04 11:27 +1300
      Re: rst and pypandoc MRAB <python@mrabarnett.plus.com> - 2015-03-02 14:40 +0000
      Re: rst and pypandoc Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 02:09 +1100

csiph-web