Exemple #1
0
def html_visit_displaymath(self, node):
    # type: (HTMLTranslator, nodes.math_block) -> None
    if node['nowrap']:
        latex = node.astext()
    else:
        latex = wrap_displaymath(node.astext(), None, False)
    try:
        fname, depth = render_math(self, latex)
    except MathExtError as exc:
        msg = str(exc)
        sm = nodes.system_message(msg,
                                  type='WARNING',
                                  level=2,
                                  backrefs=[],
                                  source=node.astext())
        sm.walkabout(self)
        logger.warning(__('inline latex %r: %s'), node.astext(), msg)
        raise nodes.SkipNode
    self.body.append(self.starttag(node, 'div', CLASS='math'))
    self.body.append('<p>')
    if node['number']:
        number = get_node_equation_number(self, node)
        self.body.append('<span class="eqno">(%s)' % number)
        self.add_permalink_ref(node, _('Permalink to this equation'))
        self.body.append('</span>')
    if fname is None:
        # something failed -- use text-only as a bad substitute
        self.body.append('<span class="math">%s</span></p>\n</div>' %
                         self.encode(node.astext()).strip())
    else:
        self.body.append(('<img src="%s"' % fname) + get_tooltip(self, node) +
                         '/></p>\n</div>')
    raise nodes.SkipNode
Exemple #2
0
def wrap_displaymath(text, label, numbering):
    # type: (unicode, unicode, bool) -> unicode
    warnings.warn('sphinx.ext.mathbase.wrap_displaymath() is moved to '
                  'sphinx.util.math package.',
                  RemovedInSphinx30Warning, stacklevel=2)
    from sphinx.util.math import wrap_displaymath
    return wrap_displaymath(text, label, numbering)
Exemple #3
0
def wrap_displaymath(text, label, numbering):
    # type: (unicode, unicode, bool) -> unicode
    warnings.warn('sphinx.ext.mathbase.wrap_displaymath() is moved to '
                  'sphinx.util.math package.',
                  RemovedInSphinx30Warning, stacklevel=2)
    from sphinx.util.math import wrap_displaymath
    return wrap_displaymath(text, label, numbering)
Exemple #4
0
def html_visit_displaymath(self, node):
    # type: (HTMLTranslator, nodes.math_block) -> None
    if node['nowrap']:
        latex = node.astext()
    else:
        latex = wrap_displaymath(node.astext(), None, False)
    try:
        fname, depth = render_math(self, latex)
    except MathExtError as exc:
        msg = str(exc)
        sm = nodes.system_message(msg, type='WARNING', level=2,
                                  backrefs=[], source=node.astext())
        sm.walkabout(self)
        logger.warning(__('inline latex %r: %s'), node.astext(), msg)
        raise nodes.SkipNode
    self.body.append(self.starttag(node, 'div', CLASS='math'))
    self.body.append('<p>')
    if node['number']:
        number = get_node_equation_number(self, node)
        self.body.append('<span class="eqno">(%s)' % number)
        self.add_permalink_ref(node, _('Permalink to this equation'))
        self.body.append('</span>')
    if fname is None:
        # something failed -- use text-only as a bad substitute
        self.body.append('<span class="math">%s</span></p>\n</div>' %
                         self.encode(node.astext()).strip())
    else:
        self.body.append(('<img src="%s"' % fname) + get_tooltip(self, node) +
                         '/></p>\n</div>')
    raise nodes.SkipNode
Exemple #5
0
def wrap_displaymath(text: str, label: str, numbering: bool) -> str:
    warnings.warn(
        'sphinx.ext.mathbase.wrap_displaymath() is moved to '
        'sphinx.util.math package.',
        RemovedInSphinx30Warning,
        stacklevel=2)
    from sphinx.util.math import wrap_displaymath
    return wrap_displaymath(text, label, numbering)
Exemple #6
0
    def _replace_math_blocks(self, doctree):
        """
        replace math blocks with images

        Math blocks are pre-processed and replaced with respective images in the
        list of documents to process. This is to help prepare additional images
        into the asset management for this extension. Math support will work on
        systems which have latex/dvipng installed.

        Args:
            doctree: the doctree to replace blocks on
        """
        if imgmath is None:
            return

        # imgmath's render_math call expects a translator to be passed
        # in; mock a translator tied to our self-builder
        class MockTranslator:
            def __init__(self, builder):
                self.builder = builder

        mock_translator = MockTranslator(self)

        for node in itertools.chain(doctree.traverse(nodes.math),
                                    doctree.traverse(nodes.math_block)):
            try:
                if not isinstance(node, nodes.math):
                    if node['nowrap']:
                        latex = node.astext()
                    else:
                        latex = wrap_displaymath(node.astext(), None, False)
                else:
                    latex = '$' + node.astext() + '$'

                mf, depth = imgmath.render_math(mock_translator, latex)
                if not mf:
                    continue

                new_node = nodes.image(candidates={'?'},
                                       uri=path.join(self.outdir, mf),
                                       **node.attributes)
                new_node['from_math'] = True
                if not isinstance(node, nodes.math):
                    new_node['align'] = 'center'
                if depth is not None:
                    new_node['math_depth'] = depth
                node.replace_self(new_node)
            except imgmath.MathExtError as exc:
                ConfluenceLogger.warn('inline latex {}: {}'.format(
                    node.astext(), exc))
Exemple #7
0
def latex_visit_displaymath(self, node):
    if node.get('label'):
        label = "equation:%s:%s" % (node['docname'], node['label'])
    else:
        label = None

    if node.get('nowrap'):
        if label:
            self.body.append(r'\label{%s}' % label)
        self.body.append(node.astext())
    else:
        from sphinx.util.math import wrap_displaymath
        self.body.append(
            wrap_displaymath(node.astext(), label,
                             self.builder.config.math_number_all))
    raise nodes.SkipNode
Exemple #8
0
def replace_math_blocks(builder, doctree):
    """
    replace math blocks with images

    Math blocks are pre-processed and replaced with respective images in the
    list of documents to process. This is to help prepare additional images into
    the asset management for this extension. Math support will work on systems
    which have latex/dvipng installed.

    Args:
        builder: the builder
        doctree: the doctree to replace blocks on
    """

    # allow users to disabled implemented extension changes
    restricted = builder.config.confluence_adv_restricted
    if 'ext-imgmath' in restricted:
        return

    # phase 1 -- convert math blocks into Confluence LaTeX blocks
    for node in itertools.chain(doctree.traverse(nodes.math),
                                doctree.traverse(nodes.math_block)):
        if not isinstance(node, nodes.math):
            if node['nowrap']:
                latex = node.astext()
            else:
                latex = wrap_displaymath(node.astext(), None, False)
            new_node_type = confluence_latex_block
        else:
            latex = '$' + node.astext() + '$'
            new_node_type = confluence_latex_inline

        new_node = new_node_type(latex, latex, **node.attributes)
        new_node['from_math'] = True

        if not isinstance(node, nodes.math):
            new_node['align'] = 'center'

        node.replace_self(new_node)

    # disable automatic conversion of latex blocks to images if a latex
    # macro is configured
    if builder.config.confluence_latex_macro:
        return

    if imgmath is None:
        return

    # phase 2 -- convert Confluence LaTeX blocks into image blocks
    #
    # imgmath's render_math call expects a translator to be passed
    # in; mock a translator tied to our builder
    class MockTranslator:
        def __init__(self, builder):
            self.builder = builder

    mock_translator = MockTranslator(builder)

    for node in itertools.chain(doctree.traverse(confluence_latex_inline),
                                doctree.traverse(confluence_latex_block)):
        try:
            mf, depth = imgmath.render_math(mock_translator, node.astext())
            if not mf:
                continue

            new_node = nodes.image(candidates={'?'},
                                   uri=path.join(builder.outdir, mf),
                                   **node.attributes)

            if depth is not None:
                new_node['math_depth'] = depth

            node.replace_self(new_node)
        except imgmath.MathExtError as exc:
            ConfluenceLogger.warn('inline latex {}: {}'.format(
                node.astext(), exc))