Skip to content

Add escape sequences to SmartyPants quote processing #498

@shimizukawa

Description

@shimizukawa

Sphinx's smartypants.py is based on an older version dated Sat, 13 Aug 2005. A later version is available at http://web.chad.org/projects/smartypants.py/ dated 27 Jul 2007. I didn't look at all the differences but the newer version does include this function:

def processEscapes(str):
      r"""
      Parameter:  String.
      Returns:    The string, with after processing the following backslash
                  escape sequences. This is useful if you want to force a "dumb"
                  quote or other character to appear.

                  Escape  Value
                  ------  -----
                  \\      \
                  \"      "
                  \'      '
                  \.      .
                  \-      -
                  \`      `
      """
      str = re.sub(r"""\\\\""", r"""\""", str)
      str = re.sub(r'''\\"''', r""""""", str)
      str = re.sub(r"""\\'""", r"""'""", str)
      str = re.sub(r"""\\\.""", r""".""", str)
      str = re.sub(r"""\\-""", r"""-""", str)
      str = re.sub(r"""\\`""", r"""`""", str)

      return str

Changing the existing sphinx_smarty_pants() in sphinx/util/smartypants.py to:

  def sphinx_smarty_pants(t):
      t = processEscapes(t)
      t = t.replace('"', '"')
      t = educateDashesOldSchool(t)
      t = educateQuotes(t)
      t = t.replace('"', '"')
      return t

Lets you say \\' when SmartyPant insists on using the wrong type of single quote. (Note: you have to double the \ to get this to work.)

I'd personally prefer to have \\``be replaced with the left single quote entity ‘ and'` be replaced with the right single quote entity ’. Maybe this behavior could be turned on with a suitable HTML option?

There are probably other improvements in the newer smartypants.py (the whole topic is fraught with special cases), but I didn't investigate them.


Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions