chiark / gitweb /
m.code: don't strip leading whitespace in blocks.
authorVladimír Vondruš <mosra@centrum.cz>
Fri, 22 Feb 2019 20:36:05 +0000 (21:36 +0100)
committerVladimír Vondruš <mosra@centrum.cz>
Sat, 23 Feb 2019 00:00:22 +0000 (01:00 +0100)
doxygen/dox2html5.py
pelican-plugins/m/code.py
pelican-plugins/m/test/code/page.html
pelican-plugins/m/test/code/page.rst

index 4911934243fbc8d7136aec270c9072c7b031c3eb..ba43f27ae5556d3452e95abe61b22e1bba7709aa 100755 (executable)
@@ -1425,7 +1425,8 @@ def parse_desc_internal(state: State, element: ET.Element, immediate_parent: ET.
             highlighted = highlight(code, lexer, formatter)
             # Strip whitespace around if inline code, strip only trailing
             # whitespace if a block
-            highlighted = highlighted.rstrip() if code_block else highlighted.strip()
+            highlighted = highlighted.rstrip()
+            if code_block: highlighted = highlighted.lstrip()
             out.parsed += '<{0} class="{1}{2}">{3}</{0}>'.format(
                 'pre' if code_block else 'code',
                 class_,
index a2ca38dff90fb824f2b7f63497ddfb22f014cfa7..4290e01b138b8ae9f8e08aa645b4fccb8812289a 100644 (file)
@@ -42,7 +42,7 @@ logger = logging.getLogger(__name__)
 
 import ansilexer
 
-def _highlight(code, language, options):
+def _highlight(code, language, options, is_block):
     # Use our own lexer for ANSI
     if language == 'ansi':
         lexer = ansilexer.AnsiLexer()
@@ -60,7 +60,9 @@ def _highlight(code, language, options):
         class_ = 'm-code'
 
     formatter = HtmlFormatter(nowrap=True, **options)
-    parsed = highlight(code, lexer, formatter).strip()
+    parsed = highlight(code, lexer, formatter).rstrip()
+    if not is_block: parsed.lstrip()
+
     return class_, parsed
 
 class Code(Directive):
@@ -82,7 +84,7 @@ class Code(Directive):
             classes += self.options['classes']
             del self.options['classes']
 
-        class_, highlighted = _highlight('\n'.join(self.content), self.arguments[0], self.options)
+        class_, highlighted = _highlight('\n'.join(self.content), self.arguments[0], self.options, is_block=True)
         classes += [class_]
 
         content = nodes.raw('', highlighted, format='html')
@@ -195,7 +197,7 @@ def code(role, rawtext, text, lineno, inliner, options={}, content=[]):
     # Not sure why language is duplicated in classes?
     if language in classes: classes.remove(language)
 
-    class_, highlighted = _highlight(utils.unescape(text), language, options)
+    class_, highlighted = _highlight(utils.unescape(text), language, options, is_block=False)
     classes += [class_]
 
     content = nodes.raw('', highlighted, format='html')
index 6be7cc2eead9d37e4236b5879137641e407fa130..b18893c5b9c245c68301e2b106051af943e53163 100644 (file)
@@ -36,6 +36,10 @@ CONTRIBUTING.rst  CREDITS.rst  <span class="g g-AnsiBrightBlue">doc</span>
 COPYING           <span class="g g-AnsiBrightBlue">css</span>          <span class="g g-AnsiBrightBlue">doxygen</span>  <span class="g g-AnsiBrightBlue">pelican-theme</span>    <span class="g g-AnsiBrightBlue">site</span></pre>
 <pre class="m-code">// this language is not highlighted</pre>
 <p>Properly preserve backslashes: <code class="m-code"><span class="k">\frac</span><span class="nb">{</span>a<span class="nb">}{</span>b<span class="nb">}</span></code></p>
+<p>Don't trim leading spaces in blocks:</p>
+<pre class="m-code">        <span class="n">nope</span><span class="p">();</span>
+    <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>
+<span class="p">}</span></pre>
 <!-- /content -->
       </div>
     </div>
index 48c5906a6bcc09f08f02b2a9eb0f4450208bcc30..0e92fae8bab1759ef2515338e84770d735fe4c0c 100644 (file)
@@ -31,3 +31,11 @@ rendered as plain monospace text: :code:`code`.
     // this language is not highlighted
 
 Properly preserve backslashes: :tex:`\frac{a}{b}`
+
+Don't trim leading spaces in blocks:
+
+.. code:: c++
+
+            nope();
+        return false;
+    }