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


Groups > comp.lang.python > #86790

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> <clk6j8Feal8U1@mid.individual.net>
Date 2015-03-03 09:51 +1100
Subject Re: rst and pypandoc
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.68.1425336728.13471.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Mar 3, 2015 at 9:30 AM, alb <al.basili@gmail.com> wrote:
> Hi Dave,
>
> Dave Angel <davea@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).

You have two different things happening here. The first is the concept
of a "string literal", and the second is how pandoc handles things.

Python's string literals come in a few different forms, but the most
common is the one that looks the same as in several other languages.
You start with a quote character, you put all your stuff in the
middle, and you finish with another quote:

"Hello, world!"

Trouble is, this makes it really hard to put quotes into your string:

"I said, "Hello, world!""

That's not going to work properly! So we need to tell Python that
those interior quotes aren't the end of the string. That's done with a
backslash:

"I said, \"Hello, world!\""

And of course, that means you have to escape the backslash if you want
to have one in the text. But all of this is just for putting *string
literals* into your source code. If it's not Python source code, these
rules don't apply. You can read a line of text from the user and it'll
be unchanged:

>>> msg = input("Enter a string: ")
Enter a string: This is a string, but not a "string literal".
>>> print(msg)
This is a string, but not a "string literal".

(in Python 2, use raw_input instead of input)

Same applies to reading from a file, or anywhere else. If it's not
Python source code, it doesn't matter what characters are in the
string, they're all just characters.

> 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???

That's something you'll have to figure out with pypandoc. I don't know
how it interprets the backslash, so you'll have to dig into its
documentation. At least now, though, you can print out your string and
see that it really does have its backslash in it.

ChrisA

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