From 08cd14c37f17b4ed5fbbdadf2b0d1a36a11dc3c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 15 Sep 2024 23:28:02 +0200 Subject: [PATCH] m.code: allow inline code and code blocks without any language. Inline code supported that already, but omitted the .m-code CSS class in that case, and also all other classes. Code block didn't support that. Now both do, and both preserve both the .m-code CSS as well as any custom ones. --- doc/plugins/math-and-code.rst | 10 +++++++++- plugins/m/code.py | 14 +++++++++++--- plugins/m/test/code/page.html | 10 ++++++++-- plugins/m/test/code/page.rst | 21 +++++++++++++++++++-- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/doc/plugins/math-and-code.rst b/doc/plugins/math-and-code.rst index 9839c638..cf805dcc 100644 --- a/doc/plugins/math-and-code.rst +++ b/doc/plugins/math-and-code.rst @@ -368,6 +368,10 @@ option to highlight lines; if you want to add additional CSS classes, use the return 0; } +Omitting the language parameter renders a plain code block without any +highlighing. In that case the :rst:`class:` option is still recognized (and the +:css:`.m-code` CSS class added as well) but :rst:`:hl-lines:` is ignored. + The `builtin include directive `_ is also patched to use the improved code directive, and: @@ -402,7 +406,11 @@ supported as well. of the source file. For inline code highlighting, use :rst:`:code:` interpreted text role. To -specify which language should be highlighted, derive a custom role from it: +specify which language should be highlighted, derive a custom role from it. +Like with the :rst:`.. code::` directive it's possible to supply CSS classes +via the :rst:`:class:` option, and if you omit the :rst:`:language:` option you +get a plain inline code without any highlighting but :rst:`:class:` still +applied, and the :css:`.m-code` CSS class added as well. .. code-figure:: diff --git a/plugins/m/code.py b/plugins/m/code.py index eeef7503..c2fc0286 100644 --- a/plugins/m/code.py +++ b/plugins/m/code.py @@ -26,6 +26,7 @@ # DEALINGS IN THE SOFTWARE. # +import html import os.path import docutils @@ -123,8 +124,8 @@ def _highlight(code, language, options, *, is_block, filters=[]): return class_, highlighted class Code(Directive): - required_arguments = 1 - optional_arguments = 0 + required_arguments = 0 + optional_arguments = 1 final_argument_whitespace = True option_spec = { 'hl-lines': directives.unchanged, @@ -144,6 +145,13 @@ class Code(Directive): classes += self.options['classes'] del self.options['classes'] + # If language is not specified, render a simple block + if not self.arguments: + content = nodes.raw('', html.escape('\n'.join(self.content)), format='html') + pre = nodes.literal_block('', classes=['m-code'] + classes) + pre.append(content) + return [pre] + # Legacy alias to hl-lines if 'hl_lines' in self.options: self.options['hl-lines'] = self.options['hl_lines'] @@ -297,7 +305,7 @@ def code(role, rawtext, text, lineno, inliner, options={}, content=[]): # If language is not specified, render a simple literal if not 'language' in options: content = nodes.raw('', utils.unescape(text), format='html') - node = nodes.literal(rawtext, '', **options) + node = nodes.literal(rawtext, '', classes=['m-code'] + classes, **options) node.append(content) return [node], [] diff --git a/plugins/m/test/code/page.html b/plugins/m/test/code/page.html index 59fdeb41..2cb4e0e1 100644 --- a/plugins/m/test/code/page.html +++ b/plugins/m/test/code/page.html @@ -38,8 +38,7 @@ highlighting:

int main() {
     return 1;
 }
-

Inline code is here: constexpr. Code without a language should be -rendered as plain monospace text: code.

+

Inline code is here: constexpr int foo = 5;.

![mosra@don-perverzo m.css]$ ls
 CONTRIBUTING.rst  CREDITS.rst  doc            plugins        README.rst
 COPYING           css          documentation  pelican-theme  site
@@ -163,6 +162,13 @@ ASan reports:
 
        nope();
     return false;
 }
+
+

Code with no language specified

+
This is a plain block.
+
This is a plain block,
+    which is colored.
+

This is a plain inline code and one which is also colored.

+

Advanced file inclusion