Source

hack2 / codeviewer / xdemo.py

Full commit
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from jinja2 import Template
import json

class CustomHtmlFormatter(HtmlFormatter):
    def __init__(self, codelines):
        self.codelines = codelines
        HtmlFormatter.__init__(self)
    
    def wrap(self, source, outfile):
        total = 1
        for i, t in HtmlFormatter.wrap(self, source, outfile):
            if i:
                if not t:
                    t = " "
                yield i, '<div id="w-%d" class="wrapper"><span class="line" id="%d">' % (total, total) + t + '</span></div>'
                total += 1
            else:
                yield i, t

FNAME = '/home/fijal/src/pypy/pypy/translator/goal/richards.py'
code = open(FNAME, 'r').read()
with open('pygments.css', 'w') as f:
    f.write(HtmlFormatter().get_style_defs('.highlight'))
with open('main.html', 'w') as f:
    f.write(Template(open("file.jinja").read()).render())
with open("x.html", "w") as f:
    source = highlight(code, PythonLexer(), CustomHtmlFormatter(code.splitlines()))
    f.write(source)
with open("x.json", "w") as f:
    json.dump(code.splitlines(), f)