Last active
August 29, 2015 14:09
-
-
Save selfsame/44a8378f364d94519918 to your computer and use it in GitHub Desktop.
minimap
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 | |
from PIL import Image | |
import sys, os, errno | |
import json, re | |
mimes = {"py", "hs", "js","clj","cljs"} | |
args = sys.argv[1:] | |
target = "" | |
output = "output" | |
if len(args) > 0: | |
target = args[0] | |
if len(args) > 1: | |
output = args[1] | |
def mkdir_p(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: # Python >2.5 | |
if exc.errno == errno.EEXIST: | |
pass | |
else: | |
raise | |
mkdir_p(output) | |
def draw(subdir, file): | |
url = subdir +"/"+ file | |
width = 0 | |
height = 0 | |
with open(url, 'r') as f: | |
lines = [] | |
for line in f: | |
height += 1 | |
width = max(width, len(line)) | |
lines.append(line) | |
im = Image.new("RGB", (width+1, height+1)) | |
pix = im.load() | |
for y, line in enumerate(lines): | |
comment = False | |
word = False | |
color = (0,0,0) | |
for x, char in enumerate(line): | |
if not word and char != " ": | |
if char == ":": | |
word = (150, 20, 100) | |
else: | |
word = (100,100,100) | |
if char == ";": | |
comment = True | |
if char == " ": | |
color = (0,0,0) | |
word = False | |
elif char in ["(", ")"]: | |
color = (0,250,0) | |
elif char in ["{", "}"]: | |
color = (250,0,0) | |
elif char in ["[", "]"]: | |
color = (250,250,0) | |
elif char in ["0","1","2","3","4","5","6","7","8","9"]: | |
color = (0,250,0) | |
elif comment and word: | |
color = (50,50,50) | |
elif word: | |
color = word | |
pix[x,y] = color | |
sub = subdir[len(target)+1:] | |
safe = re.sub("/", "-", sub + "/") | |
safe = re.sub("\\\\", "-", safe) | |
im.save(output + "/" + safe + file +".png", "PNG") | |
return [height, "<li><p>"+sub+"/"+file+"</p><img src='"+safe+file+".png"+"'></li>\n"] | |
def height_sort(a,b): | |
if a[0] >= b[0]: | |
return 1 | |
return 0 | |
def Main(): | |
result = [] | |
for subdir, dirs, files in os.walk(target): | |
for file in files: | |
if file.split(".")[-1] in mimes: | |
result.append(draw(subdir, file)) | |
result = sorted(result, key=lambda frag: frag[0]) | |
result.reverse() | |
res = "" | |
for e in result: | |
print e | |
res += e[1] | |
htmlpath = output+"/"+"result.html" | |
open(htmlpath, 'a').close() | |
with open(htmlpath, "r+") as html: | |
html.seek(0) | |
html.write("<html><head><style>"+ | |
"body{background-color:black;color:yellow;font-family:courier;font-size:12px;}"+ | |
"li{float:left;}"+ | |
"</style></head><body>\n\n"+res+"</body></head>") | |
html.truncate() | |
if __name__== '__main__' :Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment