Skip to content

@stevommmm /gist:5770048
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
pygmentme.py
#!/usr/bin/env python
import logging
import os
from pygments.util import ClassNotFound
from pygments import highlight
from pygments.lexers import guess_lexer_for_filename
from pygments.formatters import HtmlFormatter
logging.basicConfig(level=logging.DEBUG)
PROJECT_NAME = '%s_source' % os.path.basename(os.getcwd())
ROOT = './%s/' % PROJECT_NAME
BLACKLIST_EXT = [".pyc"]
BLACKLIST_DIR = [".git"]
def pygmentize(fpath):
try:
with open(fpath, 'r') as inFile:
source = inFile.read()
except IOError, e:
logging.critical("Exception in file read: %s" % e)
return None
try:
return highlight(source,
guess_lexer_for_filename(fpath, source),
HtmlFormatter(cssclass="source",
full=True))
except ClassNotFound:
return "<pre>%s</pre>" % source
def appendHtml(p):
return "%s.html" % p
def writePygment(fpath, source, retry=False):
try:
with open(fpath, 'w+') as ouf:
ouf.write(source)
except UnicodeEncodeError:
logging.info("Unicode file %s ignored" % fpath)
except IOError:
if retry:
logging.critical("File %s failed retry pygment" % fpath)
return
logging.info("Path %s not found, creating" % os.path.dirname(fpath))
os.mkdir(os.path.dirname(fpath))
writePygment(fpath, source, True)
if __name__ == '__main__':
if not os.path.exists(ROOT):
os.mkdir(ROOT)
for r, d, f in os.walk("."):
for di in d:
filepath = os.path.relpath(os.path.join(r, di))
outpath = os.path.join(ROOT, filepath)
if not os.path.exists(outpath):
logging.debug("Created path: %s" % outpath)
os.mkdir(outpath)
for fn in f:
(_, ex) = os.path.splitext(fn)
if not ex in BLACKLIST_EXT:
filepath = os.path.relpath(os.path.join(r, fn))
if not any([True for x in BLACKLIST_DIR if filepath.startswith(x)]):
outpath = os.path.join(ROOT, appendHtml(filepath))
writePygment(outpath, pygmentize(filepath))
else:
logging.debug("Blacklisted directory %s ignored" % filepath)
else:
logging.debug("Blacklisted file %s ignored" % fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.