chiark / gitweb /
m.code: allow inline code and code blocks without any language.
authorVladimír Vondruš <mosra@centrum.cz>
Sun, 15 Sep 2024 21:28:02 +0000 (23:28 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Sun, 15 Sep 2024 22:04:47 +0000 (00:04 +0200)
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
plugins/m/code.py
plugins/m/test/code/page.html
plugins/m/test/code/page.rst

index 9839c638fc23ad9a0261ae5c2bd709718b5431cc..cf805dcc6f11f0877447fa8271477455f3069199 100644 (file)
@@ -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 <http://docutils.sourceforge.net/docs/ref/rst/directives.html#include>`_
 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::
 
index eeef7503845bad6283470f18a1ed126717ea0d53..c2fc028615fb28ed18a2715ccd033aefe4361dc9 100644 (file)
@@ -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], []
 
index 59fdeb41c2e9c985e41efa2214c6499286d7135d..2cb4e0e1acd1f9f6f32062d5b909adc8d891e4f8 100644 (file)
@@ -38,8 +38,7 @@ highlighting:</p>
 <pre class="m-inverted m-code"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
 <span class="hll">    <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
 </span><span class="p">}</span></pre>
-<p>Inline code is here: <code class="cpp m-code"><span class="k">constexpr</span></code>. Code without a language should be
-rendered as plain monospace text: <code>code</code>.</p>
+<p>Inline code is here: <code class="cpp m-code"><span class="k">constexpr</span> <span class="kt">int</span> <span class="n">foo</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span></code>.</p>
 <pre class="m-console">!<span class="g g-AnsiBlue">[</span><span class="g g-AnsiBrightWhite">mosra@don-perverzo </span><span class="g g-AnsiWhite">m.css</span><span class="g g-AnsiBlue">]</span><span class="g g-AnsiBrightCyan">$ </span>ls
 CONTRIBUTING.rst  CREDITS.rst  <span class="g g-AnsiBrightBlue">doc</span>            <span class="g g-AnsiBrightBlue">plugins</span>        README.rst
 COPYING           <span class="g g-AnsiBrightBlue">css</span>          <span class="g g-AnsiBrightBlue">documentation</span>  <span class="g g-AnsiBrightBlue">pelican-theme</span>  <span class="g g-AnsiBrightBlue">site</span>
@@ -163,6 +162,13 @@ ASan reports:
 <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>
+<section id="code-with-no-language-specified">
+<h2><a href="#code-with-no-language-specified">Code with no language specified</a></h2>
+<pre class="m-code">This is a plain block.</pre>
+<pre class="m-code m-text m-info">This is a plain block,
+    which is colored.</pre>
+<p>This is a <code class="m-code">plain inline code</code> and <code class="m-code m-text m-success">one which is also colored</code>.</p>
+</section>
 <section id="advanced-file-inclusion">
 <h2><a href="#advanced-file-inclusion">Advanced file inclusion</a></h2>
 <section id="sphinx-gallery-alike-self-contained-code-file">
index 09a13fda0788384d0542b4ef680428b8984c2cdd..2803d8e5065f426af4dbe4d8eab89675c719bf1c 100644 (file)
@@ -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`_
 ==========================