Anonymous avatar Anonymous committed 5015e1f

Educate quotes in the LaTeX writer.

Comments (0)

Files changed (2)

sphinx/latexwriter.py

 
 from sphinx import addnodes
 from sphinx import highlighting
+from sphinx.util.smartypants import educateQuotesLatex
 
 # XXX: Move to a template?
 HEADER = r'''%% Generated by Sphinx.
         if self.verbatim is not None:
             self.verbatim += node.astext()
         else:
-            self.body.append(self.encode(node.astext()))
+            text = self.encode(node.astext())
+            self.body.append(educateQuotesLatex(text))
     def depart_Text(self, node):
         pass
 

sphinx/util/smartypants.py

     return s.replace('"', "“")
 
 
+def educateQuotesLatex(s):
+    """
+    Parameter:  String.
+
+    Returns:    The string, with double quotes corrected to LaTeX quotes.
+
+    Example input:  "Isn't this fun?"
+    Example output: ``Isn't this fun?'';
+    """
+
+    # Special case if the very first character is a quote
+    # followed by punctuation at a non-word-break. Close the quotes by brute force:
+    s = single_quote_start_re.sub("\x04", s)
+    s = double_quote_start_re.sub("\x02", s)
+
+    # Special case for double sets of quotes, e.g.:
+    #   <p>He said, "'Quoted' words in a larger quote."</p>
+    s = double_quote_sets_re.sub("\x01\x03", s)
+    s = single_quote_sets_re.sub("\x03\x01", s)
+
+    # Special case for decade abbreviations (the '80s):
+    s = decade_abbr_re.sub("\x04", s)
+
+    s = opening_single_quotes_regex.sub("\\1\x03", s)
+    s = closing_single_quotes_regex.sub("\\1\x04", s)
+    s = closing_single_quotes_regex_2.sub("\\1\x04\\2", s)
+
+    # Any remaining single quotes should be opening ones:
+    s = s.replace("'", "\x03")
+
+    s = opening_double_quotes_regex.sub("\\1\x01", s)
+    s = closing_double_quotes_regex.sub("\x02", s)
+    s = closing_double_quotes_regex_2.sub("\\1\x02", s)
+
+    # Any remaining quotes should be opening ones.
+    s = s.replace('"', "\x01")
+
+    # Finally, replace all helpers with quotes.
+    return s.replace("\x01", "``").replace("\x02", "''").\
+           replace("\x03", "`").replace("\x04", "'")
+
+
 def educateBackticks(s):
     """
     Parameter:  String.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.