Created
April 6, 2012 18:45
-
-
Save danielrichman/2322013 to your computer and use it in GitHub Desktop.
dump & syntax everything possible in a git repo
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/python | |
import sys | |
import copy | |
import errno | |
import os | |
import git | |
import pygments | |
import pygments.lexers | |
import pygments.formatters | |
def skip(blob): | |
# don't even consider gitignore and .pdf .odt | |
if blob.name.endswith(".pdf") or blob.name.endswith(".odt"): | |
return True | |
if blob.name == ".gitignore": | |
return True | |
return False | |
def target(path, rel=False, raw=False): | |
assert ".." not in path | |
assert not path.startswith("/") and not path.endswith("/") | |
assert path | |
if raw: | |
path = "_raw/" + path | |
if not rel: | |
path = "html/" + path | |
if not raw: | |
path += ".html" | |
return path | |
def make_dirs(tgt): | |
tgt_dir = os.path.dirname(tgt) | |
try: | |
os.makedirs(tgt_dir) | |
pass | |
except OSError as e: | |
if e.errno == errno.EEXIST: | |
pass | |
else: | |
raise | |
else: | |
print "Created directory", tgt_dir | |
def process(blob): | |
tgt = target(blob.path) | |
data = blob.data_stream.read() | |
try: | |
lex = pygments.lexers.guess_lexer_for_filename(blob.name, data) | |
except pygments.util.ClassNotFound: | |
return False | |
fmtr = pygments.formatters.HtmlFormatter(full=True) | |
make_dirs(tgt) | |
with open(tgt, "w") as f: | |
pygments.highlight(data, lex, fmtr, f) | |
print "Wrote", tgt, "using", lex.name | |
tgt_raw = target(blob.path, raw=True) | |
make_dirs(tgt_raw) | |
with open(tgt_raw, "w") as f: | |
f.write(data) | |
return True | |
def traverse(tree): | |
filelist = [] | |
for blob in tree.traverse(): | |
if not isinstance(blob, git.Blob): | |
continue | |
if skip(blob): | |
print "Skipping (rule)", blob.path | |
continue | |
success = process(blob) | |
if success: | |
filelist.append(blob.path) | |
else: | |
print "Skipping (can't parse)", blob.path | |
return filelist | |
def write_index(filelist): | |
f = target("index") | |
with open(f, "w") as f: | |
f.write("<!DOCTYPE html><html>") | |
f.write("<style type='text/css'>" | |
"li a { font-family: monospace; }" | |
"</style>") | |
f.write("<body>\n") | |
for n in sorted(filelist): | |
# Who cares about escaping? | |
t = target(n, rel=True) | |
r = target(n, raw=True, rel=True) | |
f.write("<li><a href='{0}'>{1}</a> " | |
"<a href='{2}'>(raw)</a></li>\n".format(t, n, r)) | |
f.write("</body></html>") | |
def main(): | |
repo = git.Repo("temp.git") | |
filelist = traverse(repo.tree()) | |
write_index(filelist) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment