def template(template_id):
    """
    The view where we display the result
    in syntax-highlighted HTML and CSS
    """

    template = Template.query.filter(Template.id == long(template_id)).first()
    if not template:
        return "The requested template doesn't exist", 404
    hashtml = len(template.html.strip()) > 0

    cssperma = template.css_url
    pygmented_css_link = None
    if cssperma:
        pygmented_css_link = highlight(
            '<link rel="stylesheet" type="text/css" href="%s">' % cssperma,
            CssLexer(), HtmlFormatter(style='bw', linenos='table'))
    return render_template(
        'saved_template.html',
        template=template,
        pygmented_css_link_code=pygmented_css_link,
        pygmented_html_code=highlight(
            template.html, HtmlLexer(),
            HtmlFormatter(style='bw', linenos='table')),
        pygmented_css_code=highlight(
            template.css, CssLexer(), HtmlFormatter(style='bw',
                                                    linenos='table')),
        pygments_style=HtmlFormatter(style='bw').get_style_defs('.highlight'),
        hashtml=hashtml,
    )
Beispiel #2
0
    def html_before_write(self, book, chapter):
        from lxml import etree, html

        from pygments import highlight
        from pygments.formatters import HtmlFormatter

        from ebooklib import epub

        try:
            tree = parse_html_string(chapter.content)
        except:
            return

        root = tree.getroottree()

        had_source = False

        if len(root.find('body')) != 0:
            body = tree.find('body')
            # check for embeded source
            for source in body.xpath('//pre[contains(@class,"source-")]'):
                css_class = source.get('class')

                source_text = (source.text or '') + ''.join(
                    [html.tostring(child) for child in source.iterchildren()])

                if 'source-python' in css_class:
                    from pygments.lexers import PythonLexer

                    #                    _text =  highlight(source_text, PythonLexer(), HtmlFormatter(linenos="inline"))
                    _text = highlight(source_text, PythonLexer(),
                                      HtmlFormatter())

                if 'source-css' in css_class:
                    from pygments.lexers import CssLexer

                    _text = highlight(source_text, CssLexer(), HtmlFormatter())

                _parent = source.getparent()
                _parent.replace(source, etree.XML(_text))

                had_source = True

        if had_source:
            chapter.add_link(href="style/code.css",
                             rel="stylesheet",
                             type="text/css")
            chapter.content = etree.tostring(tree,
                                             pretty_print=True,
                                             encoding='utf-8')
def _get_lexer(codesyntax):
    if codesyntax in ('cpp', 'javascript'):
        return JavascriptLexer()
    elif codesyntax == 'python':
        return PythonLexer()
    elif codesyntax == 'xml' or codesyntax == 'html':
        return HtmlLexer()
    elif codesyntax == 'css':
        return CssLexer()
    elif codesyntax == 'sql':
        return SqlLexer()
    elif codesyntax:
        raise NotImplementedError(codesyntax)
    else:
        return TextLexer()
Beispiel #4
0
def source_view(request, domain, id):
    result = get_object_or_404(Result, id=id)
    assert result.domain == domain, result.domain
    context = {}

    html_formatter = HtmlFormatter(linenos=True,
                                   lineanchors='L',
                                   linespans='L',
                                   anchorlinenos=True)
    context['code'] = highlight(result.before, CssLexer(), html_formatter)
    context['domain'] = domain
    context['result'] = {
        'id': result.id,
        'url': result.url,
    }
    return context
Beispiel #5
0
def pigmentize(text):
    """searches for <span></span> and replace with HTML pigmented code
    supported languages: python ; html ; css ; emacs ; bash ; hexdump ;
     DjangoLexer"""
    start_code = text.find('<pre>') + 5
    end_code = text.find('</pre')
    code = text[start_code:end_code]
    if code[0:5] == 'python':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, PythonLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:4] == 'html':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, HtmlLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:3] == 'css':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, CssLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:5] == 'emac':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, EmacsLispLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:4] == 'bash':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, BashLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:7] == 'hexdump':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, HexdumpLexer(), HtmlFormatter()) +
                     '</pre></div>')
    if code[0:6] == 'django':
        text.replace('<div class="highlight"><pre>' +
                     highlight(code, DjangoLexer(), HtmlFormatter()) +
                     '</pre></div>')
    return (text)
Beispiel #6
0
 def load_css_syntax(self):
     self.master.lexer = CssLexer()
     self.master.initial_highlight()
Beispiel #7
0
options_menu.add_separator()
options_menu.add_command(label=lang['options'][5], command=textbox.scrap_page)
options_menu.add_command(label=lang['options'][6], command=textbox.highlight_all)

style_menu.add_command(label=lang['style'][0], command=lambda: textbox.tagger('bold'))
style_menu.add_command(label=lang['style'][1], command=lambda: textbox.tagger('italic'))
style_menu.add_command(label=lang['style'][2], command=lambda: textbox.tagger('underline'))

syntax_menu.add_command(label='Python 3', command=lambda: textbox.set_lexer(Python3Lexer()))
syntax_menu.add_command(label='C/C++', command=lambda: textbox.set_lexer(CppLexer()))
syntax_menu.add_command(label='C#', command=lambda: textbox.set_lexer(CSharpLexer()))
syntax_menu.add_command(label='Java', command=lambda: textbox.set_lexer(JavaLexer()))
syntax_menu.add_command(label='Rust', command=lambda: textbox.set_lexer(RustLexer()))
syntax_menu.add_command(label='Go', command=lambda: textbox.set_lexer(GoLexer()))
syntax_menu.add_command(label='HTML', command=lambda: textbox.set_lexer(HtmlLexer()))
syntax_menu.add_command(label='CSS', command=lambda: textbox.set_lexer(CssLexer()))
syntax_menu.add_command(label='Javascript', command=lambda: textbox.set_lexer(JavascriptLexer()))
syntax_menu.add_command(label='PHP', command=lambda: textbox.set_lexer(PhpLexer()))
syntax_menu.add_command(label='SQL', command=lambda: textbox.set_lexer(SqlLexer()))
syntax_menu.add_command(label='Batch', command=lambda: textbox.set_lexer(BatchLexer()))
syntax_menu.add_command(label='Bash', command=lambda: textbox.set_lexer(BashLexer()))
syntax_menu.add_command(label='Markdown', command=lambda: textbox.set_lexer(MarkdownLexer()))

for font_name in settings["fonts"]:
	font_menu.add_command(label=font_name, command=lambda font_name=font_name: textbox.change_font(font_name, 0))

for size in range(settings["min_font_size"], 
	settings["max_font_size"] + settings["font_size_interval"], 
	settings["font_size_interval"]):
	font_size_menu.add_command(label=size, command=lambda size=size: textbox.change_font(size, 1))