Created
June 12, 2013 23:32
-
-
Save stevommmm/5770048 to your computer and use it in GitHub Desktop.
pygmentme.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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