Esempio n. 1
0
    def run(self):
        set_classes(self.options)
        self.assert_has_content()

        parent = self.state.parent

        # Fallback rendering as code requested
        if settings['M_MATH_RENDER_AS_CODE']:
            # If this is a math figure, replace the figure CSS class to have a
            # matching border
            if _is_math_figure(parent):
                parent['classes'][parent['classes'].index(
                    'm-figure')] = 'm-code-figure'

            content = nodes.raw('',
                                html.escape('\n'.join(self.content)),
                                format='html')
            pre = nodes.literal_block('')
            pre.append(content)
            return [pre]

        content = '\n'.join(self.content)

        _, svg = latex2svgextra.fetch_cached_or_render("$$" + content + "$$")

        # If this is the first real node inside a math figure, put the SVG
        # directly inside
        if _is_math_figure(parent):
            node = nodes.raw(
                self.block_text,
                latex2svgextra.patch(
                    content, svg, None, ' class="{}"'.format(
                        ' '.join(['m-math'] +
                                 self.options.get('classes', [])))),
                format='html')
            node.line = self.content_offset + 1
            self.add_name(node)
            return [node]

        # Otherwise wrap it in a <div class="m-math">
        node = nodes.raw(self.block_text,
                         latex2svgextra.patch(content, svg, None, ''),
                         format='html')
        node.line = self.content_offset + 1
        self.add_name(node)
        container = nodes.container(**self.options)
        container['classes'] += ['m-math']
        container.append(node)
        return [container]
Esempio n. 2
0
def math(role, rawtext, text, lineno, inliner, options={}, content=[]):
    # In order to properly preserve backslashes (well, and backticks)
    text = rawtext[rawtext.find('`') + 1:rawtext.rfind('`')]

    # Fallback rendering as code requested
    if settings['M_MATH_RENDER_AS_CODE']:
        set_classes(options)
        classes = []
        if 'classes' in options:
            classes += options['classes']
            del options['classes']

        content = nodes.raw('',
                            html.escape(utils.unescape(text)),
                            format='html')
        node = nodes.literal(rawtext, '', **options)
        node.append(content)
        return [node], []

    # Apply classes to the <svg> element instead of some outer <span>
    set_classes(options)
    classes = 'm-math'
    if 'classes' in options:
        classes += ' ' + ' '.join(options['classes'])
        del options['classes']

    depth, svg = latex2svgextra.fetch_cached_or_render("$" + text + "$")

    attribs = ' class="{}"'.format(classes)
    node = nodes.raw(rawtext,
                     latex2svgextra.patch(text, svg, depth, attribs),
                     format='html',
                     **options)
    return [node], []
Esempio n. 3
0
def math(role, rawtext, text, lineno, inliner, options={}, content=[]):
    # Otherwise the backslashes do quite a mess there
    i = rawtext.find('`')
    text = rawtext.split('`')[1]

    # Fallback rendering as code requested
    if render_as_code:
        set_classes(options)
        classes = []
        if 'classes' in options:
            classes += options['classes']
            del options['classes']

        content = nodes.raw('',
                            html.escape(utils.unescape(text)),
                            format='html')
        node = nodes.literal(rawtext, '', **options)
        node.append(content)
        return [node], []

    # Apply classes to the <svg> element instead of some outer <span>
    set_classes(options)
    classes = 'm-math'
    if 'classes' in options:
        classes += ' ' + ' '.join(options['classes'])
        del options['classes']

    depth, svg = latex2svgextra.fetch_cached_or_render("$" + text + "$")

    attribs = ' class="{}"'.format(classes)
    node = nodes.raw(rawtext,
                     latex2svgextra.patch(text, svg, depth, attribs),
                     format='html',
                     **options)
    return [node], []
Esempio n. 4
0
    def run(self):
        set_classes(self.options)
        self.assert_has_content()

        # Fallback rendering as code requested
        if render_as_code:
            content = nodes.raw('', '\n'.join(self.content), format='html')
            pre = nodes.literal_block('')
            pre.append(content)
            return [pre]

        # join lines, separate blocks
        content = '\n'.join(self.content).split('\n\n')
        _nodes = []
        for block in content:
            if not block:
                continue

            out = latex2svg.latex2svg("$$" + block + "$$",
                                      params=latex2svgextra.params)

            container = nodes.container(**self.options)
            container['classes'] += ['m-math']
            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)
            _nodes.append(container)
        return _nodes
Esempio n. 5
0
File: math.py Progetto: mosra/m.css
def math(role, rawtext, text, lineno, inliner, options={}, content=[]):
    # Otherwise the backslashes do quite a mess there
    i = rawtext.find('`')
    text = rawtext.split('`')[1]

    # Fallback rendering as code requested
    if settings['M_MATH_RENDER_AS_CODE']:
        set_classes(options)
        classes = []
        if 'classes' in options:
            classes += options['classes']
            del options['classes']

        content = nodes.raw('', html.escape(utils.unescape(text)), format='html')
        node = nodes.literal(rawtext, '', **options)
        node.append(content)
        return [node], []

    # Apply classes to the <svg> element instead of some outer <span>
    set_classes(options)
    classes = 'm-math'
    if 'classes' in options:
        classes += ' ' + ' '.join(options['classes'])
        del options['classes']

    depth, svg = latex2svgextra.fetch_cached_or_render("$" + text + "$")

    attribs = ' class="{}"'.format(classes)
    node = nodes.raw(rawtext, latex2svgextra.patch(text, svg, depth, attribs), format='html', **options)
    return [node], []
Esempio n. 6
0
File: math.py Progetto: mosra/m.css
    def run(self):
        set_classes(self.options)
        self.assert_has_content()

        parent = self.state.parent

        # Fallback rendering as code requested
        if settings['M_MATH_RENDER_AS_CODE']:
            # If this is a math figure, replace the figure CSS class to have a
            # matching border
            if _is_math_figure(parent):
                parent['classes'][parent['classes'].index('m-figure')] = 'm-code-figure'

            content = nodes.raw('', html.escape('\n'.join(self.content)), format='html')
            pre = nodes.literal_block('')
            pre.append(content)
            return [pre]

        content = '\n'.join(self.content)

        _, svg = latex2svgextra.fetch_cached_or_render("$$" + content + "$$")

        # If this is the first real node inside a math figure, put the SVG
        # directly inside
        if _is_math_figure(parent):
            node = nodes.raw(self.block_text, latex2svgextra.patch(content, svg, None, ' class="{}"'.format(' '.join(['m-math'] + self.options.get('classes', [])))), format='html')
            node.line = self.content_offset + 1
            self.add_name(node)
            return [node]

        # Otherwise wrap it in a <div class="m-math">
        node = nodes.raw(self.block_text, latex2svgextra.patch(content, svg, None, ''), format='html')
        node.line = self.content_offset + 1
        self.add_name(node)
        container = nodes.container(**self.options)
        container['classes'] += ['m-math']
        container.append(node)
        return [container]
Esempio n. 7
0
def math(role, rawtext, text, lineno, inliner, options={}, content=[]):
    # Otherwise the backslashes do quite a mess there
    i = rawtext.find('`')
    text = rawtext.split('`')[1]

    # Fallback rendering as code requested
    if render_as_code:
        set_classes(options)
        classes = []
        if 'classes' in options:
            classes += options['classes']
            del options['classes']

        content = nodes.raw('', utils.unescape(text), format='html')
        node = nodes.literal(rawtext, '', **options)
        node.append(content)
        return [node], []

    # Apply classes to the <svg> element instead of some outer <span>
    set_classes(options)
    classes = 'm-math'
    if 'classes' in options:
        classes += ' ' + ' '.join(options['classes'])
        del options['classes']

    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,
                     latex2svgextra.patch(text, out, attribs),
                     format='html',
                     **options)
    return [node], []