Created
January 28, 2014 03:34
-
-
Save kylelk/8661873 to your computer and use it in GitHub Desktop.
Create highlighted listings of files
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
# -*- coding: utf-8 -*- | |
import pygments | |
from pygments import highlight | |
from pygments.lexers import get_lexer_by_name | |
from pygments.formatters import HtmlFormatter | |
from pygments.lexers import guess_lexer, get_lexer_for_filename | |
from os import listdir, mkdir | |
from os.path import isfile, join, getsize, getctime, isdir, dirname, abspath | |
from time import ctime | |
import glob | |
from math import ceil | |
mypath = "." | |
maxlines = 30 | |
items_per_page = 10.0 | |
base_path = dirname(abspath(__file__))+"/" | |
if isdir("pastebin/") == False: | |
mkdir("pastebin/") | |
onlyfiles = [ f for f in glob.glob(mypath+"/*.*") if isfile(join(mypath,f)) ] | |
#onlyfiles.remove("test.html") | |
template = """ | |
<!DOCTYPE html> | |
<style> | |
.main-page { | |
background-color:#C0C0C0; | |
} | |
.list-item { | |
width:90%; | |
margin-top:10px; | |
margin-bottom:10px; | |
margin-right:5%; | |
margin-left:5%; | |
background-color:white; | |
box-shadow: 5px 5px 5px #888888; | |
} | |
h3 { | |
margin-top:10px; | |
text-align:center; | |
} | |
.source { | |
margin-right:10px; | |
margin-left:10px; | |
margin-top:10px; | |
margin-bottom:10px; | |
overflow:hidden; | |
} | |
footer { | |
bottom: 0; | |
position: fixed; | |
text-align:center; | |
width: 100%; | |
margin-top:10px; | |
background-color:white; | |
} | |
</style> | |
<body class="main-page"> | |
""" | |
valid_files = [] | |
for name in onlyfiles: | |
try: | |
get_lexer_for_filename(name) | |
valid_files.append(name) | |
except pygments.util.ClassNotFound: | |
pass | |
page_links = "" | |
for n in range(1, int(ceil(len(valid_files)/items_per_page))): | |
page_links+="""<a href="page%d.html"> %d </a>"""%(n, n) | |
count=0 | |
page_count=1 | |
output=template | |
for name in valid_files: | |
count+=1.0 | |
name=name.replace("./", "") | |
code = "".join(open(name, "rb").readlines()[:maxlines]) | |
lexer = get_lexer_for_filename(name) | |
formatter = HtmlFormatter(linenos=False, cssclass="source", encoding="utf-8") | |
result = highlight(code, lexer, formatter) | |
temp="""<div class="list-item">""" | |
temp+="<style> ccs-color-codes </style>" | |
temp+="""<h3><a href="file-path">file-name </a></h3><div class="info">""" | |
temp+=""" Created on: <b>date-created </b>""" | |
temp+="""Size: <b>file-size </b>""" | |
temp+="""</div><hr>result</div>""" | |
temp=temp.replace("ccs-color-codes", HtmlFormatter().get_style_defs('.source')) | |
temp=temp.replace("file-name", name) | |
temp=temp.replace("file-path", base_path+name) | |
temp=temp.replace("file-size", str(getsize(name))) | |
temp=temp.replace("date-created", ctime(getctime(name))) | |
temp=temp.replace("result", result) | |
output+=temp+"\n" | |
if int(count) == int(items_per_page): | |
count=0 | |
output+="""<footer><span id="footer">%s</span></footer>"""%(page_links) | |
output+="</body>" | |
open("pastebin/page%d.html"%(page_count), "w").write(output) | |
page_count+=1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment