From 76a476fd2b25b6b8835df00594e0f48d03b3ecd1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 9 Jun 2020 18:02:55 +0200 Subject: [PATCH] m.code: change to :hl-lines: instead of :hl_lines:. I can't stress how much I hated the underscore. Turns out it's there ONLY because Pygments have an underscore, otherwise there would be a dash like with all other directives/options/roles. Eh. The legacy option is still recognized of course. --- doc/plugins/math-and-code.rst | 6 +++--- plugins/m/code.py | 12 ++++++++++++ plugins/m/test/code/page.html | 4 ++++ plugins/m/test/code/page.rst | 10 ++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/doc/plugins/math-and-code.rst b/doc/plugins/math-and-code.rst index 6de8a130..6987dda4 100644 --- a/doc/plugins/math-and-code.rst +++ b/doc/plugins/math-and-code.rst @@ -330,7 +330,7 @@ replaces `Pelican code-block directive `_ into the :rst:`.. code::` -directive and specify the language via a parameter. Use :rst:`:hl_lines:` +directive and specify the language via a parameter. Use :rst:`:hl-lines:` option to highlight lines; if you want to add additional CSS classes, use the :rst:`:class:` option. @@ -345,7 +345,7 @@ option to highlight lines; if you want to add additional CSS classes, use the .. code:: rst .. code:: c++ - :hl_lines: 4 5 + :hl-lines: 4 5 :class: m-inverted #include @@ -356,7 +356,7 @@ option to highlight lines; if you want to add additional CSS classes, use the } .. code:: c++ - :hl_lines: 4 5 + :hl-lines: 4 5 :class: m-inverted #include diff --git a/plugins/m/code.py b/plugins/m/code.py index c854176f..62cbad2f 100644 --- a/plugins/m/code.py +++ b/plugins/m/code.py @@ -62,6 +62,11 @@ def _highlight(code, language, options, *, is_block, filters=[]): else: class_ = 'm-code' + # Pygments wants the underscored option + if 'hl-lines' in options: + options['hl_lines'] = options['hl-lines'] + del options['hl-lines'] + if isinstance(lexer, ansilexer.AnsiLexer): formatter = ansilexer.HtmlAnsiFormatter(**options) else: @@ -97,6 +102,8 @@ class Code(Directive): optional_arguments = 0 final_argument_whitespace = True option_spec = { + 'hl-lines': directives.unchanged, + # Legacy alias to hl-lines (I hate underscores) 'hl_lines': directives.unchanged, 'class': directives.class_option, 'filters': directives.unchanged @@ -112,6 +119,11 @@ class Code(Directive): classes += self.options['classes'] del self.options['classes'] + # Legacy alias to hl-lines + if 'hl_lines' in self.options: + self.options['hl-lines'] = self.options['hl_lines'] + del self.options['hl_lines'] + filters = self.options.pop('filters', '').split() class_, highlighted = _highlight('\n'.join(self.content), self.arguments[0], self.options, is_block=True, filters=filters) diff --git a/plugins/m/test/code/page.html b/plugins/m/test/code/page.html index 9ae6384b..00608a47 100644 --- a/plugins/m/test/code/page.html +++ b/plugins/m/test/code/page.html @@ -29,6 +29,10 @@
int main() {
     return 1;
 }
+

Legacy :hl_lines: option should work the same:

+
int main() {
+    return 1;
+}

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

![mosra@don-perverzo m.css]$ ls
diff --git a/plugins/m/test/code/page.rst b/plugins/m/test/code/page.rst
index 91e819a2..655041eb 100644
--- a/plugins/m/test/code/page.rst
+++ b/plugins/m/test/code/page.rst
@@ -16,6 +16,16 @@ m.code
         return 0;
     }
 
+.. code:: c++
+    :class: m-inverted
+    :hl-lines: 2
+
+    int main() {
+        return 1;
+    }
+
+Legacy :rst:`:hl_lines:` option should work the same:
+
 .. code:: c++
     :class: m-inverted
     :hl_lines: 2
-- 
2.30.2