Example #1
0
def latex_math(text):
    if not TEXOID_ENABLED:
        return text

    tree = lxml_tree.fromstring(text)
    texoid = TexoidRenderer()

    for latex in tree.xpath('.//latex'):
        result = texoid.get_result(latex.text)
        if not result:
            tag = html.Element('pre')
            tag.text = 'LaTeX rendering error\n' + latex.text
        elif 'error' not in result:
            img = html.Element('img')
            img.set('src', result['svg'])
            img.set('onerror', "this.src='%s';this.onerror=null" % result['png'])

            ident = result['meta']
            img.set('width', ident['width'])
            img.set('height', ident['height'])
            style = []
            if 'inline' not in latex.attrib:
                tag = html.Element('div')
                style += ['text-align: center']
            else:
                tag = html.Element('span')
            style += ['max-width: 100%', 'height: %s' % ident['height'],
                      'max-height: %s' % ident['height'], 'width: %s' % ident['height']]
            tag.set('style', ';'.join(style))
            tag.append(img)
        else:
            tag = html.Element('pre')
            tag.text = result['error']
        latex.getparent().replace(latex, tag)
    return tree
Example #2
0
def latex_math(text):
    if not TEXOID_ENABLED:
        return text

    tree = lxml_tree.fromstring(text)
    texoid = TexoidRenderer()

    for latex in tree.xpath('.//latex'):
        result = texoid.get_result(latex.text)
        if not result:
            tag = html.Element('pre')
            tag.text = 'LaTeX rendering error\n' + latex.text
        elif 'error' not in result:
            img = html.Element('img')
            img.set('src', result['svg'])
            img.set('onerror',
                    "this.src='%s';this.onerror=null" % result['png'])

            ident = result['meta']
            img.set('width', ident['width'])
            img.set('height', ident['height'])
            style = []
            if 'inline' not in latex.attrib:
                tag = html.Element('div')
                style += ['text-align: center']
            else:
                tag = html.Element('span')
            style += [
                'max-width: 100%',
                'height: %s' % ident['height'],
                'max-height: %s' % ident['height'],
                'width: %s' % ident['height']
            ]
            tag.set('style', ';'.join(style))
            tag.append(img)
        else:
            tag = html.Element('pre')
            tag.text = result['error']
        latex.getparent().replace(latex, tag)
    return tree
Example #3
0
class AwesomeRenderer(MathRenderer, mistune.Renderer):
    def __init__(self, *args, **kwargs):
        self.nofollow = kwargs.pop('nofollow', True)
        self.texoid = TexoidRenderer() if kwargs.pop('texoid', False) else None
        self.parser = HTMLParser()
        super(AwesomeRenderer, self).__init__(*args, **kwargs)

    def _link_rel(self, href):
        if href:
            try:
                url = urlparse(href)
            except ValueError:
                return ' rel="nofollow"'
            else:
                if url.netloc and url.netloc not in NOFOLLOW_WHITELIST:
                    return ' rel="nofollow"'
        return ''

    def autolink(self, link, is_email=False):
        text = link = mistune.escape(link)
        if is_email:
            link = 'mailto:%s' % link
        return '<a href="%s"%s>%s</a>' % (link, self._link_rel(link), text)

    def link(self, link, title, text):
        link = mistune.escape_link(link)
        if not title:
            return '<a href="%s"%s>%s</a>' % (link, self._link_rel(link), text)
        title = mistune.escape(title, quote=True)
        return '<a href="%s" title="%s"%s>%s</a>' % (link, title, self._link_rel(link), text)

    def block_code(self, code, lang=None):
        if not lang:
            return '\n<pre><code>%s</code></pre>\n' % mistune.escape(code).rstrip()
        return highlight_code(code, lang)

    def block_html(self, html):
        if self.texoid and html.startswith('<latex'):
            attr = html[6:html.index('>')]
            latex = html[html.index('>') + 1:html.rindex('<')]
            latex = self.parser.unescape(latex)
            result = self.texoid.get_result(latex)
            if not result:
                return '<pre>%s</pre>' % mistune.escape(latex, smart_amp=False)
            elif 'error' not in result:
                img = ('''<img src="%(svg)s" onerror="this.src='%(png)s';this.onerror=null"'''
                       'width="%(width)s" height="%(height)s"%(tail)s>') % {
                          'svg': result['svg'], 'png': result['png'],
                          'width': result['meta']['width'], 'height': result['meta']['height'],
                          'tail': ' /' if self.options.get('use_xhtml') else ''
                      }
                style = ['max-width: 100%',
                         'height: %s' % result['meta']['height'],
                         'max-height: %s' % result['meta']['height'],
                         'width: %s' % result['meta']['height']]
                if 'inline' in attr:
                    tag = 'span'
                else:
                    tag = 'div'
                    style += ['text-align: center']
                return '<%s style="%s">%s</%s>' % (tag, ';'.join(style), img, tag)
            else:
                return '<pre>%s</pre>' % mistune.escape(result['error'], smart_amp=False)
        return super(AwesomeRenderer, self).block_html(html)

    def header(self, text, level, *args, **kwargs):
        return super(AwesomeRenderer, self).header(text, level + 2, *args, **kwargs)