Пример #1
0
 def __init__(self, filter=None):
     self._formatter = NullFormatter()
     self._lexer = Python3Lexer(ensurenl=False, stripnl=False)
     self._lexer.add_filter("tokenmerge")
     if filter is None:
         filter = ConverterFilter()
     self._lexer.add_filter(filter)
Пример #2
0
def get_api_from_file(filename):
    """
        @filename - source file to parse and extract API symbols
        @return - string - API definition

        Extract API symbols from a source file.
    """
    lex = None

# start language definitions - sorted by lang name

    if filename.lower().endswith('.java'):
        lex = lexers.JavaLexer()
        lex.add_filter(filters.JavaAPIFilter())
    elif filename.lower().endswith('.php'):
        lex = lexers.PhpLexer(startinline=True)
        lex.add_filter(filters.PHPAPIFilter())
    elif filename.lower().endswith('.py'):
        lex = lexers.PythonLexer()
        lex.add_filter(filters.PythonAPIFilter())


# end language definitions

    if lex:
        code = open(filename, 'r').read()
        return highlight(code, lex, NullFormatter())+"\n"
    else:
        return None
Пример #3
0
def redent(s):
    """
    Shamelessly stolen from infobob(#python bot) code
    https://code.launchpad.net/~pound-python/infobat/infobob
    """
    lexer = PythonLexer()
    lexer.add_filter(_RedentFilter())
    return highlight(s, lexer, NullFormatter())
Пример #4
0
def paste_raw_all(id):
    data = query_db("select * from pastes where id = ?", [id], one=True)
    try:
        Lexer = get_lexer_by_name(data["syntaxhighlight"].lower())
    except:
        Lexer = get_lexer_by_name("python")
    return Response(highlight(data["text"], Lexer, NullFormatter()),
                    mimetype="text/plain")
Пример #5
0
def test_correct_output():
    hfmt = HtmlFormatter(nowrap=True)
    houtfile = StringIO()
    hfmt.format(tokensource, houtfile)

    nfmt = NullFormatter()
    noutfile = StringIO()
    nfmt.format(tokensource, noutfile)

    stripped_html = re.sub('<.*?>', '', houtfile.getvalue())
    escaped_text = escape_html(noutfile.getvalue())
    assert stripped_html == escaped_text
Пример #6
0
    def test_correct_output(self):
        hfmt = CodeHtmlFormatter(instance_class=type, nowrap=True)
        houtfile = StringIO()
        hfmt.format(tokensource, houtfile)

        nfmt = NullFormatter()
        noutfile = StringIO()
        nfmt.format(tokensource, noutfile)

        stripped_html = re.sub('<.*?>', '', houtfile.getvalue())
        escaped_text = escape_html(noutfile.getvalue())
        self.assertEqual(stripped_html, escaped_text)
Пример #7
0
                if (closing_char) and def_started:
                    def_started = False
                    # for cases such as array(); - value is ();
                    if len(svalue) > 1:
                        yield token.Text, svalue[:-1]

                    yield token.Text, "\n"

            if def_started:
                yield ttype, value


if __name__ == "__main__":
    import os
    from pygments import highlight
    from pygments.lexers import PhpLexer
    from pygments.formatters import NullFormatter

    lex = PhpLexer(startinline=True)
    lex.add_filter(PHPAPIFilter())

    for (path, dirs, files) in os.walk('~/repos/git/symfony/src'):
        for fname in files:
            f = os.path.join(path, fname)
            if f.endswith(".php"):
                code = open(f, 'r').read()
                print "---------- start %s ----------" % f
                print highlight(code, lex, NullFormatter())
                print "---------- end %s ----------" % f
Пример #8
0
for filename in os.listdir(INPUT_DIR):
    if filename.startswith('.'):
        continue

    with open(os.path.join(INPUT_DIR, filename), encoding='utf-8') as f:
        code = f.read()

    if filename.endswith('.ansi'):
        lexer = ANSILexer()
    else:
        lexer = get_lexer_for_filename(filename)
    lexer.add_filter(Sublette.filter())
    highlighted = highlight(code, lexer, formatter)

    # Detect size by output instead of input.
    text = highlight(code, lexer, NullFormatter())
    lines = text.split('\n')
    columns = max(len(line.rstrip()) for line in lines)
    rows = len(lines)

    html = weasyprint.HTML(string=TEMPLATE % {
        'css': formatter.get_style_defs('body'),
        'html': highlighted,
        'width': (columns+4) * 15,
        'height': (rows+2) * 30,
    })

    output_path = OUTPUT_PATTERN % filename
    with open(output_path, 'wb') as f:
        html.write_png(f, font_config=font_config)
    print(os.path.relpath(output_path, os.path.curdir))
Пример #9
0
def redent(s):
    lexer = PythonLexer()
    lexer.add_filter(_RedentFilter())
    return highlight(s, lexer, NullFormatter())