From: Vladimír Vondruš
Date: Sun, 15 Sep 2024 21:28:02 +0000 (+0200)
Subject: m.code: allow inline code and code blocks without any language.
X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=08cd14c37f17b4ed5fbbdadf2b0d1a36a11dc3c4;p=blog.git
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.
---
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;
}
+
+
+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
.
+
diff --git a/plugins/m/test/code/page.rst b/plugins/m/test/code/page.rst
index 09a13fda..2803d8e5 100644
--- a/plugins/m/test/code/page.rst
+++ b/plugins/m/test/code/page.rst
@@ -12,6 +12,9 @@ m.code
.. role:: py(code)
:language: py
+.. role:: text-success(code)
+ :class: m-text m-success
+
.. code:: c++
int main() {
@@ -44,8 +47,7 @@ highlighting:
:class: m-inverted
:hl-lines: 2
-Inline code is here: :cpp:`constexpr`. Code without a language should be
-rendered as plain monospace text: :code:`code`.
+Inline code is here: :cpp:`constexpr int foo = 5;`.
.. include:: console.ansi
:code: ansi
@@ -77,6 +79,21 @@ Don't trim leading spaces in blocks:
return false;
}
+`Code with no language specified`_
+==================================
+
+.. code::
+
+ This is a plain block.
+
+.. code::
+ :class: m-text m-info
+
+ This is a plain block,
+ which is colored.
+
+This is a :code:`plain inline code` and :text-success:`one which is also colored`.
+
`Advanced file inclusion`_
==========================