Exemplo n.º 1
0
def highlight(code, language, _preview=False, _linenos=True):
    """Highlight a given code to HTML."""
    if not _preview:
        if language == 'diff':
            return highlight_diff(code)
        elif language == 'creole':
            return format_creole(code)
        elif language == 'csv':
            return format_csv(code)
        elif language == 'gcc-messages':
            return format_compiler_messages(parse_gcc_messages(code), 'gcc')
        elif language == 'javac-messages':
            return format_compiler_messages(parse_javac_messages(code), 'javac')
    if language == 'multi':
        return highlight_multifile(code)
    elif language == 'php':
        lexer = PhpLexer(startinline=True)
    else:
        try:
            lexer = get_lexer_by_name(language)
        except ClassNotFound:
            lexer = TextLexer()
    style = get_style(name_only=True)
    formatter = HtmlFormatter(linenos=_linenos, cssclass='syntax', style=style)
    return u'<div class="code">%s</div>' % \
           pygments.highlight(code, lexer, formatter)
Exemplo n.º 2
0
def highlight(code, language, _preview=False, _linenos=True):
    """Highlight a given code to HTML."""
    if not _preview:
        if language == 'diff':
            return highlight_diff(code)
        elif language == 'creole':
            return format_creole(code)
        elif language == 'csv':
            return format_csv(code)
        elif language == 'gcc-messages':
            return format_compiler_messages(parse_gcc_messages(code), 'gcc')
        elif language == 'javac-messages':
            return format_compiler_messages(parse_javac_messages(code),
                                            'javac')
    if language == 'multi':
        return highlight_multifile(code)
    elif language == 'php':
        lexer = PhpLexer(startinline=True)
    else:
        try:
            lexer = get_lexer_by_name(language)
        except ClassNotFound:
            lexer = TextLexer()
    style = get_style(name_only=True)
    formatter = HtmlFormatter(linenos=_linenos, cssclass='syntax', style=style)
    return u'<div class="code">%s</div>' % \
           pygments.highlight(code, lexer, formatter)