sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../pelican-plugins'))
import latex2svg
-import m.math
+import latex2svgextra
import ansilexer
class Trie:
# Assume that Doxygen wrapped the formula properly to distinguish
# between inline, block or special environment
- rendered = latex2svg.latex2svg('{}'.format(i.text), params=m.math.latex2svg_params)
+ rendered = latex2svg.latex2svg('{}'.format(i.text), params=latex2svgextra.params)
# We should have decided about block/inline above
assert formula_block is not None
has_block_elements = True
out.parsed += '<div class="m-math{}">{}</div>'.format(
' ' + add_css_class if add_css_class else '',
- m.math._patch(i.text, rendered, ''))
+ latex2svgextra.patch(i.text, rendered, ''))
else:
# CSS classes and styling for proper vertical alignment. Depth is relative
# to font size, describes how below the line the text is. Scaling it back
attribs = ' class="m-math{}" style="vertical-align: -{:.1f}pt;"'.format(
' ' + add_inline_css_class if add_inline_css_class else '',
(rendered['depth'] or 0.0)*12*1.25)
- out.parsed += m.math._patch(i.text, rendered, attribs)
+ out.parsed += latex2svgextra.patch(i.text, rendered, attribs)
# Inline elements
elif i.tag == 'linebreak':
def parse_xml(state: State, xml: str):
# Reset counter for unique math formulas
- m.math.counter = 0
+ latex2svgextra.counter = 0
state.current = os.path.basename(xml)
--- /dev/null
+#
+# This file is part of m.css.
+#
+# Copyright © 2017, 2018 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.
+#
+
+import re
+
+import latex2svg
+
+# Extracted common code used by both dox2html5.py and the m.math plugin to
+# avoid dependency of dox2html5.py on Pelican
+
+# Modified params to use for math rendering
+params = latex2svg.default_params.copy()
+params.update({
+ # Don't use libertine fonts as they mess up things
+ 'preamble': r"""
+\usepackage[utf8x]{inputenc}
+\usepackage{amsmath}
+\usepackage{amsfonts}
+\usepackage{amssymb}
+\usepackage{newtxtext}
+""",
+ # Zoom the letters a bit to match page font size
+ 'dvisvgm_cmd': 'dvisvgm --no-fonts -Z 1.25',
+ })
+
+_patch_src = re.compile(r"""<\?xml version='1.0' encoding='UTF-8'\?>
+<!-- This file was generated by dvisvgm \d+\.\d+\.\d+ -->
+<svg (?P<attribs>.+) xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
+""")
+
+_patch_dst = r"""<svg{attribs} \g<attribs>>
+<title>LaTeX Math</title>
+<desc>
+{formula}
+</desc>
+"""
+
+_unique_src = re.compile(r"""(?P<name> id|xlink:href)='(?P<ref>#?)(?P<id>g\d+-\d+|page\d+)'""")
+_unique_dst = r"""\g<name>='\g<ref>eq{counter}-\g<id>'"""
+
+# Counter to ensure unique IDs for multiple SVG elements on the same page.
+# Reset back to zero on start of a new page for reproducible behavior.
+counter = 0
+
+# Patches the output from dvisvgm
+# - w/o the XML preamble and needless xmlns attributes
+# - unique element IDs (see `counter`)
+# - adds additional `attribs` to the <svg> element
+def patch(formula, out, attribs):
+ global counter
+ counter += 1
+ return _unique_src.sub(_unique_dst.format(counter=counter), _patch_src.sub(_patch_dst.format(attribs=attribs, formula=formula.replace('\\', '\\\\')), out['svg']))
import pelican.signals
import latex2svg
-
-latex2svg_params = latex2svg.default_params.copy()
-latex2svg_params.update({
- # Don't use libertine fonts as they mess up things
- 'preamble': r"""
-\usepackage[utf8x]{inputenc}
-\usepackage{amsmath}
-\usepackage{amsfonts}
-\usepackage{amssymb}
-\usepackage{newtxtext}
-""",
- # Zoom the letters a bit to match page font size
- 'dvisvgm_cmd': 'dvisvgm --no-fonts -Z 1.25',
- })
-
-patch_src = re.compile(r"""<\?xml version='1.0' encoding='UTF-8'\?>
-<!-- This file was generated by dvisvgm \d+\.\d+\.\d+ -->
-<svg (?P<attribs>.+) xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
-""")
-
-patch_dst = r"""<svg{attribs} \g<attribs>>
-<title>LaTeX Math</title>
-<desc>
-{formula}
-</desc>
-"""
-
-unique_src = re.compile(r"""(?P<name> id|xlink:href)='(?P<ref>#?)(?P<id>g\d+-\d+|page\d+)'""")
-unique_dst = r"""\g<name>='\g<ref>eq{counter}-\g<id>'"""
-
-counter = 0
+import latex2svgextra
render_as_code = False
-def _patch(formula, out, attribs):
- global counter
- counter += 1
- return unique_src.sub(unique_dst.format(counter=counter), patch_src.sub(patch_dst.format(attribs=attribs, formula=formula.replace('\\', '\\\\')), out['svg']))
-
class Math(rst.Directive):
option_spec = {'class': directives.class_option,
'name': directives.unchanged}
if not block:
continue
- out = latex2svg.latex2svg("$$" + block + "$$", params=latex2svg_params)
+ out = latex2svg.latex2svg("$$" + block + "$$", params=latex2svgextra.params)
container = nodes.container(**self.options)
container['classes'] += ['m-math']
- node = nodes.raw(self.block_text, _patch(block, out, ''), format='html')
+ node = nodes.raw(self.block_text, latex2svgextra.patch(block, out, ''), format='html')
node.line = self.content_offset + 1
self.add_name(node)
container.append(node)
classes += ' ' + ' '.join(options['classes'])
del options['classes']
- out = latex2svg.latex2svg("$" + text + "$", params=latex2svg_params)
+ out = latex2svg.latex2svg("$" + text + "$", params=latex2svgextra.params)
# CSS classes and styling for proper vertical alignment. Depth is relative
# to font size, describes how below the line the text is. Scaling it back
# to 12pt font, scaled by 125% as set above in the config.
attribs = ' class="{}" style="vertical-align: -{:.1f}pt;"'.format(classes, out['depth']*12*1.25)
- node = nodes.raw(rawtext, _patch(text, out, attribs), format='html', **options)
+ node = nodes.raw(rawtext, latex2svgextra.patch(text, out, attribs), format='html', **options)
return [node], []
def configure_pelican(pelicanobj):