Example #1
0
def extract_docstring_highlighted(id):
    lines = open('testish/lib/forms.py').readlines()
    docstring = []
    in_function = False
    in_quotes = False
    for line in lines:
        if 'def %s('%id in line:
            in_function = True
        elif 'def ' in line:
            in_function = False

        if in_function and not in_quotes and '    """' in line:
            in_quotes = True
        elif in_function and in_quotes and '   """' in line:
            in_quotes = False

        if in_quotes and in_function and '   """' not in line and 'def %s('%id not in line:
            docstring.append( line )

    docstring = ''.join(docstring)
    try:
        from pygments import highlight
        from pygments.lexers import MakoHtmlLexer
        from pygments.formatters import HtmlFormatter
        return highlight(docstring, MakoHtmlLexer(), HtmlFormatter())
    except ImportError:
        return docstring
Example #2
0
def render_highlight_code(text, path, **kwargs):
    try:
        if path.endswith(('.html', '.mako')):
            lexer = MakoHtmlLexer(encoding='utf-8')
        elif path.endswith('.ptl'):
            lexer = PythonLexer(encoding='utf-8')
        elif path.endswith('.md'):
            lexer = RstLexer(encoding='utf-8')
        else:
            if path.endswith(IGNORE_FILE_EXTS):
                text = 'Hmm.., this is binary file.'
            lexer = guess_lexer_for_filename(path, text)
        lexer.encoding = 'utf-8'
        lexer.stripnl = False
    except ClassNotFound:
        # no code highlight
        lexer = TextLexer(encoding='utf-8')

    formatter = CodeHtmlFormatter

    return highlight(text, lexer, formatter(linenos='inline',
                                            lineanchors='L',
                                            anchorlinenos=True,
                                            encoding='utf-8',
                                            **kwargs))
Example #3
0
def highlight_code(path, src, div=False, **kwargs):
    src = decode_charset_to_unicode(src)
    try:
        if path.endswith(('.html', '.mako')):
            lexer = MakoHtmlLexer(encoding='utf-8')
        elif path.endswith('.ptl'):
            lexer = PythonLexer(encoding='utf-8')
        elif path.endswith('.md'):
            lexer = RstLexer(encoding='utf-8')
        else:
            if path.endswith(IGNORE_FILE_EXTS):
                src = 'Hmm.., this is binary file.'
            lexer = guess_lexer_for_filename(path, src)
        lexer.encoding = 'utf-8'
        lexer.stripnl = False
    except ClassNotFound:
        # no code highlight
        lexer = TextLexer(encoding='utf-8')
    if div:
        formatter = _CodeHtmlFormatter
    else:
        formatter = HtmlFormatter

    src = highlight(src, lexer, formatter(linenos=True,
                                          lineanchors='L',
                                          anchorlinenos=True,
                                          encoding='utf-8',
                                          **kwargs))
    return src