[Python-checkins] r65273 - in doctools/branches/0.4.x: CHANGES Makefile sphinx/htmlwriter.py tests/test_markup.py

georg.brandl python-checkins at python.org
Tue Jul 29 11:05:38 CEST 2008


Author: georg.brandl
Date: Tue Jul 29 11:05:37 2008
New Revision: 65273

Log:
Correct rendering of ``samp``.


Modified:
   doctools/branches/0.4.x/CHANGES
   doctools/branches/0.4.x/Makefile
   doctools/branches/0.4.x/sphinx/htmlwriter.py
   doctools/branches/0.4.x/tests/test_markup.py

Modified: doctools/branches/0.4.x/CHANGES
==============================================================================
--- doctools/branches/0.4.x/CHANGES	(original)
+++ doctools/branches/0.4.x/CHANGES	Tue Jul 29 11:05:37 2008
@@ -1,6 +1,8 @@
 Release 0.4.2 (in development)
 ==============================
 
+* Fix rendering of the ``samp`` role in HTML.
+
 * Fix a bug with LaTeX links to headings leading to a wrong page.
 
 * Reread documents with globbed toctrees when source files are

Modified: doctools/branches/0.4.x/Makefile
==============================================================================
--- doctools/branches/0.4.x/Makefile	(original)
+++ doctools/branches/0.4.x/Makefile	Tue Jul 29 11:05:37 2008
@@ -27,4 +27,4 @@
 	@$(PYTHON) utils/reindent.py -r -B .
 
 test:
-	@cd tests; $(PYTHON) run.py
+	@cd tests; $(PYTHON) run.py -d

Modified: doctools/branches/0.4.x/sphinx/htmlwriter.py
==============================================================================
--- doctools/branches/0.4.x/sphinx/htmlwriter.py	(original)
+++ doctools/branches/0.4.x/sphinx/htmlwriter.py	Tue Jul 29 11:05:37 2008
@@ -56,6 +56,7 @@
         self.builder = builder
         self.highlightlang = 'python'
         self.highlightlinenothreshold = sys.maxint
+        self.protect_literal_text = 0
 
     def visit_desc(self, node):
         self.body.append(self.starttag(node, 'dl', CLASS=node['desctype']))
@@ -206,7 +207,11 @@
         if len(node.children) == 1 and \
                node.children[0] in ('None', 'True', 'False'):
             node['classes'].append('xref')
-        BaseTranslator.visit_literal(self, node)
+        self.body.append(self.starttag(node, 'tt', '', CLASS='docutils literal'))
+        self.protect_literal_text += 1
+    def depart_literal(self, node):
+        self.protect_literal_text -= 1
+        self.body.append('</tt>')
 
     def visit_productionlist(self, node):
         self.body.append(self.starttag(node, 'pre'))
@@ -285,6 +290,33 @@
     def depart_module(self, node):
         pass
 
+    def bulk_text_processor(self, text):
+        return text
+
+    # overwritten
+    def visit_Text(self, node):
+        text = node.astext()
+        encoded = self.encode(text)
+        if self.protect_literal_text:
+            # moved here from base class's visit_literal to support
+            # more formatting in literal nodes
+            for token in self.words_and_spaces.findall(encoded):
+                if token.strip():
+                    # protect literal text from line wrapping
+                    self.body.append('<span class="pre">%s</span>' % token)
+                elif token in ' \n':
+                    # allow breaks at whitespace
+                    self.body.append(token)
+                else:
+                    # protect runs of multiple spaces; the last one can wrap
+                    self.body.append('&nbsp;' * (len(token)-1) + ' ')
+        else:
+            if self.in_mailto and self.settings.cloak_email_addresses:
+                encoded = self.cloak_email(encoded)
+            else:
+                encoded = self.bulk_text_processor(encoded)
+            self.body.append(encoded)
+
     # these are all for docutils 0.5 compatibility
 
     def visit_note(self, node):
@@ -333,7 +365,6 @@
     def depart_tip(self, node):
         self.depart_admonition()
 
-
     # these are only handled specially in the SmartyPantsHTMLTranslator
     def visit_literal_emphasis(self, node):
         return self.visit_emphasis(node)
@@ -397,11 +428,7 @@
         finally:
             self.no_smarty -= 1
 
-    def visit_Text(self, node):
-        text = node.astext()
-        encoded = self.encode(text)
-        if self.in_mailto and self.settings.cloak_email_addresses:
-            encoded = self.cloak_email(encoded)
-        elif self.no_smarty <= 0:
-            encoded = sphinx_smarty_pants(encoded)
-        self.body.append(encoded)
+    def bulk_text_processor(self, text):
+        if self.no_smarty <= 0:
+            return sphinx_smarty_pants(text)
+        return text

Modified: doctools/branches/0.4.x/tests/test_markup.py
==============================================================================
--- doctools/branches/0.4.x/tests/test_markup.py	(original)
+++ doctools/branches/0.4.x/tests/test_markup.py	Tue Jul 29 11:05:37 2008
@@ -60,17 +60,32 @@
 
 
 def test_inline():
-    # correct interpretation of code with spaces
-    verify('``code sample``', None, '\\code{code sample}')
-    verify(':samp:`code sample`', None, '\\samp{code sample}')
+    # correct interpretation of code with whitespace
+    _html = ('<p><tt class="docutils literal"><span class="pre">'
+             'code</span>&nbsp;&nbsp; <span class="pre">sample</span></tt></p>')
+    verify('``code   sample``', _html, '\\code{code   sample}')
+    verify(':samp:`code   sample`', _html, '\\samp{code   sample}')
 
     # interpolation of braces in samp and file roles (HTML only)
-    verify(':samp:`a{b}c`', '<p><tt class="docutils literal">a<em>b</em>c</tt></p>',
+    verify(':samp:`a{b}c`',
+           '<p><tt class="docutils literal"><span class="pre">a</span>'
+           '<em><span class="pre">b</span></em><span class="pre">c</span></tt></p>',
            '\\samp{abc}')
 
     # interpolation of arrows in menuselection
-    verify(':menuselection:`a --> b`', u'<p><em>a \N{TRIANGULAR BULLET} b</em></p>',
+    verify(':menuselection:`a --> b`',
+           u'<p><em>a \N{TRIANGULAR BULLET} b</em></p>',
            '\\emph{a $\\rightarrow$ b}')
 
     # non-interpolation of dashes in option role
-    verify(':option:`--with-option`', '<p><em>--with-option</em></p>', None)
+    verify(':option:`--with-option`',
+           '<p><em>--with-option</em></p>',
+           r'\emph{\texttt{--with-option}}')
+
+    # verify smarty-pants quotes
+    verify('"John"', '<p>&#8220;John&#8221;</p>', "``John''")
+    # ... but not in literal text
+    verify('``"John"``',
+           '<p><tt class="docutils literal"><span class="pre">'
+           '&quot;John&quot;</span></tt></p>',
+           '\\code{"John"}')


More information about the Python-checkins mailing list