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_,
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()
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):
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')
# 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')
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>