1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
..
This file is part of m.css.
Copyright © 2017, 2018, 2019, 2020, 2021, 2022, 2023
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
..
Math and code
#############
:breadcrumb: {filename}/plugins.rst Plugins
:footer:
.. note-dim::
:class: m-text-center
`« Images <{filename}/plugins/images.rst>`_ | `Plugins <{filename}/plugins.rst>`_ | `Plots and graphs » <{filename}/plugins/plots-and-graphs.rst>`_
.. role:: css(code)
:language: css
.. role:: html(code)
:language: html
.. role:: ini(code)
:language: ini
.. role:: py(code)
:language: py
.. role:: rst(code)
:language: rst
.. role:: sh(code)
:language: sh
These plugins use external libraries to produce beautiful math and code
rendering directly from your :abbr:`reST <reStructuredText>` sources.
.. contents::
:class: m-block m-default
`Math`_
=======
For Pelican, download the `m/math.py and latex2svg.py <{filename}/plugins.rst>`_
files, put them including the ``m/`` directory into one of your
:py:`PLUGIN_PATHS` and add :py:`m.math` package to your :py:`PLUGINS` in
``pelicanconf.py``. This plugin assumes presence of
`m.htmlsanity <{filename}/plugins/htmlsanity.rst>`_.
.. code:: python
PLUGINS += ['m.htmlsanity', 'm.math']
M_MATH_RENDER_AS_CODE = False
M_MATH_CACHE_FILE = 'm.math.cache'
For the Python doc theme, it's enough to mention it in :py:`PLUGINS`. The
`m.htmlsanity`_ plugin is available always, no need to mention it explicitly:
.. code:: py
PLUGINS += ['m.code']
For the Doxygen theme, this feature is builtin. Use either the ``@f[`` command
for block-level math or the ``@f$`` command for inline math. It's possible to
add extra CSS classes by placing ``@m_class`` in a paragraph before the actual
math block (or right before inline math), see the
`Doxygen theme-specific commands <http://localhost:8000/documentation/doxygen/#theme-specific-commands>`_
for more information. The :ini:`M_MATH_CACHE_FILE` option is supported as well;
there's no equivalent to the :ini:`M_MATH_RENDER_AS_CODE` option implemented at
this point.
In addition you need some LaTeX distribution installed. Use your distribution
package manager, for example on Ubuntu:
.. code:: sh
sudo apt install \
texlive-base \
texlive-latex-extra \
texlive-fonts-extra \
texlive-fonts-recommended
.. block-warning:: Note for macOS users
On macOS 10.11 and up, if you use MacTex, there's some additional setup
needed `as detailed here <https://tex.stackexchange.com/a/249967>`_ --- you
need to update your :sh:`$PATH` so the binaries are properly found:
.. code:: sh
export PATH=$PATH:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin
.. note-success::
This plugin makes use of the ``latex2svg.py`` utility from :gh:`tuxu/latex2svg`,
© 2017 `Tino Wagner <http://www.tinowagner.com/>`_, licensed under
:gh:`MIT <tuxu/latex2svg$master/LICENSE.md>`.
The plugin overrides the builtin docutils
`math directive <http://docutils.sourceforge.net/docs/ref/rst/directives.html#math>`_
and `math interpreted text role <http://docutils.sourceforge.net/docs/ref/rst/roles.html#math>`_
and:
- Instead of relying on MathML or MathJax, converts input LaTeX math formula
to a SVG file, which is then embedded directly to the page. All glyphs are
converted to paths.
- Size is represented using CSS :css:`em` units so the formula follows
surrounding text size.
- Adds a possibility to color the whole formula or parts of it using colors
that follow the current theme.
- Adds a :html:`<title>` containing the original formula to the generated
:html:`<svg>` element for accessibility.
Put `math blocks <{filename}/css/components.rst#math>`_ into the :rst:`.. math::`
directive; if you want to color the equations, add corresponding
`CSS class <{filename}/css/components.rst#colors>`_ via a :rst:`:class:`
option.
.. code-figure::
.. code:: rst
.. math::
:class: m-success
\boldsymbol{A} = \begin{pmatrix}
\frac{2n}{s_x} & 0 & 0 & 0 \\
0 & \frac{2n}{s_y} & 0 & 0 \\
0 & 0 & \frac{n + f}{n - f} & \frac{2nf}{n - f} \\
0 & 0 & -1 & 0
\end{pmatrix}
.. math::
:class: m-success
\boldsymbol{A} = \begin{pmatrix}
\frac{2n}{s_x} & 0 & 0 & 0 \\
0 & \frac{2n}{s_y} & 0 & 0 \\
0 & 0 & \frac{n + f}{n - f} & \frac{2nf}{n - f} \\
0 & 0 & -1 & 0
\end{pmatrix}
Inline math can be wrapped in the :rst:`:math:` interpreted text role. If you
want to add additional CSS classes, derive a custom role from it.
.. code-figure::
.. code:: rst
.. role:: math-info(math)
:class: m-info
Quaternion-conjugated dual quaternion is :math-info:`\hat q^* = q_0^* + q_\epsilon^*`,
while dual-conjugation gives :math:`\overline{\hat q} = q_0 - \epsilon q_\epsilon`.
.. role:: math-info(math)
:class: m-info
Quaternion-conjugated dual quaternion is :math-info:`\hat q^* = q_0^* + q_\epsilon^*`,
while dual-conjugation gives :math:`\overline{\hat q} = q_0 - \epsilon q_\epsilon`.
The resulting SVG follows font size of surrounding text, so you can use math
even outside of main page copy:
.. code-figure::
.. code:: rst
.. button-success:: https://tauday.com/
The :math:`\tau` manifesto
they say :math:`\pi` is wrong
.. button-success:: https://tauday.com/
The :math:`\tau` manifesto
they say :math:`\pi` is wrong
The ``xcolor`` package is enabled by default together with names matching CSS
color classes. You can use it to highlight different parts of the formula:
.. code-figure::
.. code:: rst
.. math::
\boldsymbol{A} = \begin{pmatrix}
{\color{m-info} s_x} & 0 & 0 & {\color{m-success} t_x} \\
0 & {\color{m-info} s_y} & 0 & {\color{m-success} t_y} \\
0 & 0 & {\color{m-info} s_z} & {\color{m-success} t_z} \\
0 & 0 & 0 & 1
\end{pmatrix}
.. math::
\boldsymbol{A} = \begin{pmatrix}
{\color{m-info} s_x} & 0 & 0 & {\color{m-success} t_x} \\
0 & {\color{m-info} s_y} & 0 & {\color{m-success} t_y} \\
0 & 0 & {\color{m-info} s_z} & {\color{m-success} t_z} \\
0 & 0 & 0 & 1
\end{pmatrix}
The :py:`M_MATH_CACHE_FILE` setting (defaulting to ``m.math.cache`` in the
site root directory) describes a file used for caching rendered LaTeX math
formulas for speeding up subsequent runs. Cached output that's no longer needed
is periodically pruned and new formulas added to the file. Set it to :py:`None`
to disable caching.
.. note-info::
LaTeX can be sometimes a real pain to set up. In order to make it possible
to work on sites that use the :py:`m.math` plugin on machines without LaTeX
installed, you can enable a fallback option to render all math as code
blocks using the :py:`M_MATH_RENDER_AS_CODE` setting. That can be, for
example, combined with a check for presence of the LaTeX binary:
.. code:: py
import shutil
import logging
if not shutil.which('latex'):
logging.warning("LaTeX not found, fallback to rendering math as code")
M_MATH_RENDER_AS_CODE = True
`Math figure`_
--------------
See the `m.components <{filename}/plugins/components.rst#code-math-and-graph-figure>`__
plugin for details about code figures using the :rst:`.. math-figure::`
directive.
.. code-figure::
.. code:: rst
.. math-figure:: Infinite projection matrix
.. math::
:class: m-success
\boldsymbol{A} = \begin{pmatrix}
\frac{2n}{s_x} & 0 & 0 & 0 \\
0 & \frac{2n}{s_y} & 0 & 0 \\
0 & 0 & -1 & -2n \\
0 & 0 & -1 & 0
\end{pmatrix}
With :math:`f = \infty`.
.. math-figure:: Infinite projection matrix
.. math::
:class: m-success
\boldsymbol{A} = \begin{pmatrix}
\frac{2n}{s_x} & 0 & 0 & 0 \\
0 & \frac{2n}{s_y} & 0 & 0 \\
0 & 0 & -1 & -2n \\
0 & 0 & -1 & 0
\end{pmatrix}
.. class:: m-noindent
With :math:`f = \infty`.
`Code`_
=======
For Pelican, download the `m/code.py and ansilexer.py <{filename}/plugins.rst>`_
files, put them including the ``m/`` directory into one of your :py:`PLUGIN_PATHS`
and add :py:`m.code` package to your :py:`PLUGINS` in ``pelicanconf.py``. This
plugin assumes presence of `m.htmlsanity <{filename}/plugins/htmlsanity.rst>`_.
.. code:: python
PLUGINS += ['m-htmlsanity', 'm.code']
M_CODE_FILTERS_PRE = []
M_CODE_FILTERS_POST = []
For the Python doc theme, it's enough to mention it in :py:`PLUGINS`. The
`m.htmlsanity`_ plugin is available always, no need to mention it explicitly:
.. code:: py
PLUGINS += ['m.code']
For the Doxygen theme, this feature is builtin. Use the ``@code{.ext}`` command
either in a block or inline, the various ``@include`` and ``@snippet`` commands
support it as well. Language detection is done from the value of ``.ext`` in
the ``@code`` command and from the include/snippet filename for the others.
It's possible to add extra CSS classes by placing ``@m_class`` in a paragraph
before the actual code block (or right before inline code), see the
`Doxygen theme-specific commands <http://localhost:8000/documentation/doxygen/#theme-specific-commands>`_
for more information. There's no possibility to highlight particular code
lines.
In addition you need to have `Pygments <http://pygments.org>`_ installed. Get
it via ``pip`` or your distribution package manager:
.. code:: sh
pip3 install Pygments
The plugin overrides the builtin docutils
`code directive <http://docutils.sourceforge.net/docs/ref/rst/directives.html#code>`_
and `code interpreted text role <http://docutils.sourceforge.net/docs/ref/rst/roles.html#code>`_,
replaces `Pelican code-block directive <https://docs.getpelican.com/en/stable/content.html#syntax-highlighting>`_ and:
- Wraps Pygments output in :html:`<code>` element for inline code and
:html:`<pre>` element for code blocks with :css:`.m-code` CSS class
applied.
- Removes useless CSS classes from the output.
- Adds a :rst:`:filters:` option. See `Filters`_ below.
Put `code blocks <{filename}/css/components.rst#code>`_ into the :rst:`.. code::`
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.
.. note-dim::
Docutils (and Sphinx) have also :rst:`.. code-block::` and
:rst:`.. sourcecode::` aliases for the same thing. Those are included for
compatibility purposes and behave the same way as :rst:`.. code::`.
.. code-figure::
.. code:: rst
.. code:: c++
:hl-lines: 4 5
:class: m-inverted
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
.. code:: c++
:hl-lines: 4 5
:class: m-inverted
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
The `builtin include directive <http://docutils.sourceforge.net/docs/ref/rst/directives.html#include>`_
is also patched to use the improved code directive, and:
- Drops the rarely useful :rst:`:encoding:`, :rst:`:literal:` and
:rst:`:name:` options
- Adds a :rst:`:hl-lines:` option to have the same behavior as
the :rst:`.. code::` directive
- Adds a :rst:`:start-on:` and :rst:`:strip-prefix:` options, and improves
:rst:`:end-before:`. See `Advanced file inclusion`_ below.
Simply specify external code snippets filename and set the language using the
:rst:`:code:` option. All options of the :rst:`.. code::` directive are
supported as well.
.. code-figure::
.. code:: rst
.. include:: snippet.cpp
:code: c++
:start-line: 2
.. include:: math-and-code-snippet.cpp
:code: c++
:start-line: 2
.. note-info::
Note that the :rst:`.. include::` directives are processed before Pelican
comes into play, and thus no special internal linking capabilities are
supported. In particular, relative paths are assumed to be relative to path
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:
.. code-figure::
.. code:: rst
.. role:: cmake(code)
:language: cmake
.. role:: cpp(code)
:language: cpp
With the :cmake:`add_executable(foo bar.cpp)` CMake command you can create an
executable from a file that contains just :cpp:`int main() { return 666; }` and
nothing else.
.. role:: cmake(code)
:language: cmake
.. role:: cpp(code)
:language: cpp
With the :cmake:`add_executable(foo bar.cpp)` CMake command you can create
an executable from a file that contains just :cpp:`int main() { return 666; }`
and nothing else.
`Colored terminal output`_
--------------------------
Use the ``ansi`` pseudo-language for highlighting
`colored terminal output <{filename}/css/components.rst#colored-terminal-output>`_.
The plugin will take care of the rest like using the custom Pygments lexer and
assigning a proper CSS class. Because ANSI escape codes might cause problems
with some editors and look confusing when viewed via :sh:`git diff` on the
terminal, it's best to have the listings in external files and use
:rst:`.. include::`:
.. code-figure::
.. code:: rst
.. include:: console.ansi
:code: ansi
.. include:: math-and-code-console.ansi
:code: ansi
:class: m-nopad
There's support for the basic foreground and background color sets, 256 palette
colors using the ``\033[38;5;{p}m`` or ``\033[48;5;{p}m`` color sequences,
and 24bit colors using the ``\033[38;2;{r};{g};{b}m`` and
``\033[48;2;{r};{g};{b}m`` color sequences. The non-bright basic foreground
colors can be independently brightened using the ``\033[1m`` color sequence:
.. include:: math-and-code-console-colors.ansi
:code: ansi
`Code figure`_
--------------
See the `m.components <{filename}/plugins/components.rst#code-math-and-graph-figure>`__
plugin for details about code figures using the :rst:`.. code-figure::`
directive.
`Advanced file inclusion`_
--------------------------
Compared to the `builtin include directive`_, the m.css-patched variant
additionally provides a :rst:`:strip-prefix:` option that strips a prefix from
each included line. This can be used for example to remove excessive
indentation from code blocks. To avoid trailing whitespace, you can wrap the
value in quotes. Reusing the snippet from above, showing only the code inside
:cpp:`main()`:
.. code-figure::
.. code:: rst
.. include:: snippet.cpp
:code: c++
:start-line: 3
:end-line: 5
:strip-prefix: ' '
.. include:: math-and-code-snippet.cpp
:code: c++
:start-line: 3
:end-line: 5
:strip-prefix: ' '
This isn't limited to just whitespace though --- since the :rst:`.. include::`
directive works for including reStructuredText as well, it can be used to embed
parts of self-contained python scripts on the page. Consider this file,
``two-sins.py``:
.. include:: math-and-code-selfcontained.py
:code: py
Embedding it on a page, mixed together with other content (and unimportant
parts omitted), can look like below. The :rst:`:start-on:` option can be used
to pin to a particular line (instead of skipping it like :rst:`:start-after:`
does) and an empty :rst:`:end-before:` will include everything until the next
blank line. Finally, :rst:`:strip-prefix:` strips the leading :py:`#` from the
comments embedded in Python code:
.. code-figure::
.. code:: rst
.. include:: two-sins.py
:start-after: """
:end-before: """
.. code-figure::
.. include:: two-sins.py
:start-on: sin =
:end-before:
:code: py
0.13545234412104434
.. include:: two-sins.py
:start-on: # And a sum with itself
:strip-prefix: '# '
:end-before:
.. include:: two-sins.py
:start-on: two_sins
:code: py
.. include:: math-and-code-selfcontained.py
:start-after: """
:end-before: """
.. code-figure::
.. include:: math-and-code-selfcontained.py
:start-on: sin =
:end-before:
:code: py
0.13545234412104434
.. include:: math-and-code-selfcontained.py
:start-on: # And a sum with itself
:strip-prefix: '# '
:end-before:
.. include:: math-and-code-selfcontained.py
:start-on: two_sins
:code: py
`Filters`_
----------
It's possible to supply filters that get applied both before and after a
code snippet is rendered using the :py:`M_CODE_FILTERS_PRE` and
:py:`M_CODE_FILTERS_POST` options. It's a dict with keys being the lexer
name [1]_ and values being filter functions. Each function that gets string as
an input and is expected to return a modified string. In the following example,
all CSS code snippets have the hexadecimal color literals annotated with a
`color swatch <{filename}/css/components.rst#color-swatches-in-code-snippets>`_:
.. code:: py
:class: m-console-wrap
import re
_css_colors_src = re.compile(r"""<span class="mh">#(?P<hex>[0-9a-f]{6})</span>""")
_css_colors_dst = r"""<span class="mh">#\g<hex><span class="m-code-color" style="background-color: #\g<hex>;"></span></span>"""
M_CODE_FILTERS_POST = {
'CSS': lambda code: _css_colors_src.sub(_css_colors_dst, code)
}
.. code-figure::
.. code:: rst
.. code:: css
p.green {
color: #3bd267;
}
.. code:: css
p.green {
color: #3bd267;
}
In the above case, the filter gets applied globally to all code snippets of
given language. Sometimes it might be desirable to apply a filter only to
specific code snippet --- in that case, the dict key is a tuple of
:py:`(lexer, filter)` where the second item is a filter name. This filter name
is then referenced from the :rst:`:filters:` option of the :rst:`.. code::` and
:rst:`.. include::` directives as well as the inline :rst:`:code:` text role.
Multiple filters can be specified when separated by spaces.
.. code:: py
M_CODE_FILTERS_PRE = {
('C++', 'codename'): lambda code: code.replace('DirtyMess', 'P300::V1'),
('C++', 'fix_typography'): lambda code: code.replace(' :', ':'),
}
.. code-figure::
.. code:: rst
.. code:: cpp
:filters: codename fix_typography
for(auto& a : DirtyMess::managedEntities()) {
// ...
}
.. code:: cpp
:filters: codename fix_typography
for(auto& a : DirtyMess::managedEntities()) {
// ...
}
.. [1] In order to have an unique mapping, the filters can't use the aliases
--- for example C++ code can be highlighted using either ``c++`` or ``cpp``
as a language name and the dict would need to have an entry for each. An unique lexer name is the :py:`name` field used in the particular lexer
source, you can also see the names in the language dropdown on the
`official website <http://pygments.org/demo/>`_.
|