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:
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::
# DEALINGS IN THE SOFTWARE.
#
+import html
import os.path
import docutils
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,
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']
# 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], []
<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>
<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">
.. role:: py(code)
:language: py
+.. role:: text-success(code)
+ :class: m-text m-success
+
.. code:: c++
int main() {
: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
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`_
==========================