From: Vladimír Vondruš Date: Fri, 22 Feb 2019 20:36:05 +0000 (+0100) Subject: m.code: don't strip leading whitespace in blocks. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=fc2fcd1cc0a6c0744832477bbee64f90ae917149;p=blog.git m.code: don't strip leading whitespace in blocks. --- diff --git a/doxygen/dox2html5.py b/doxygen/dox2html5.py index 49119342..ba43f27a 100755 --- a/doxygen/dox2html5.py +++ b/doxygen/dox2html5.py @@ -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}'.format( 'pre' if code_block else 'code', class_, diff --git a/pelican-plugins/m/code.py b/pelican-plugins/m/code.py index a2ca38df..4290e01b 100644 --- a/pelican-plugins/m/code.py +++ b/pelican-plugins/m/code.py @@ -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') diff --git a/pelican-plugins/m/test/code/page.html b/pelican-plugins/m/test/code/page.html index 6be7cc2e..b18893c5 100644 --- a/pelican-plugins/m/test/code/page.html +++ b/pelican-plugins/m/test/code/page.html @@ -36,6 +36,10 @@ CONTRIBUTING.rst CREDITS.rst doc COPYING css doxygen pelican-theme site
// this language is not highlighted

Properly preserve backslashes: \frac{a}{b}

+

Don't trim leading spaces in blocks:

+
        nope();
+    return false;
+}
diff --git a/pelican-plugins/m/test/code/page.rst b/pelican-plugins/m/test/code/page.rst index 48c5906a..0e92fae8 100644 --- a/pelican-plugins/m/test/code/page.rst +++ b/pelican-plugins/m/test/code/page.rst @@ -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; + }