You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
5.7 KiB
197 lines
5.7 KiB
import os, os.path, stat, sys
|
|
import urllib, hashlib, codecs
|
|
from datetime import datetime
|
|
import pylog_config as cfg
|
|
import subprocess,tempfile
|
|
import markdown
|
|
|
|
from HTMLParser import HTMLParser
|
|
|
|
def dir_begin(path,hidden,depth):
|
|
if depth!=0:
|
|
print '<li>'
|
|
print '<ul',
|
|
if hidden:
|
|
print 'class="hidden"',
|
|
print'>'
|
|
def dir_end(path,hidden,depth):
|
|
print '</ul>'
|
|
if depth!=0:
|
|
print '</li>'
|
|
def dir_link(name,f,depth,hidden):
|
|
script = sys.argv[0][sys.argv[0].rindex('/')+1:]
|
|
print ' <li class="link level'+str(depth),
|
|
if hidden:
|
|
print ' hidden',
|
|
print '">'
|
|
print ' <a class="menulink" href="'+script+'?'+'path='+os.path.normpath(f).replace(' ','+')+'">'+name+'</a>'
|
|
print ' </li>'
|
|
def file_link(name,f,depth,hidden):
|
|
script = sys.argv[0][sys.argv[0].rindex('/')+1:]
|
|
print ' <li class="entry level'+str(depth),
|
|
if hidden:
|
|
print ' hidden',
|
|
print '">'
|
|
print ' <a class="menulink" href="'+script+'?'+'disp='+os.path.normpath(f).replace(' ','+')+'">'+name+'</a>'
|
|
print ' </li>'
|
|
|
|
def disp_txt(path):
|
|
print '<div class="entrytext">'
|
|
f = codecs.open(path, mode="r", encoding=cfg.encoding)
|
|
txt=f.read()
|
|
print markdown.markdown(txt,extensions=cfg.markdown_extensions,content_format=cfg.content_format).encode(cfg.encoding)
|
|
f.close()
|
|
print '</div>'
|
|
|
|
def disp_python(path):
|
|
disp_source(path,"python",True)
|
|
|
|
def disp_c(path):
|
|
disp_source(path,"c",True)
|
|
|
|
def disp_source(path,lang,line_numbers):
|
|
print '<div class="entrytext">'
|
|
f = codecs.open(path, mode="r", encoding=cfg.encoding)
|
|
if line_numbers:
|
|
txt="\t#!"+lang+'\n'
|
|
else:
|
|
txt="\t:::"+lang+'\n'
|
|
for line in f.read().split('\n'):
|
|
txt += "\t"+line+'\n'
|
|
print markdown.markdown(txt,extensions=cfg.markdown_extensions,content_format=cfg.content_format).encode(cfg.encoding)
|
|
f.close()
|
|
print '</div>'
|
|
|
|
def disp(path):
|
|
print '<div class="entrytext">'
|
|
f = codecs.open(path, mode="r", encoding=cfg.encoding)(path)
|
|
print f.read().encode(cfg.encoding)
|
|
f.close()
|
|
print '</div>'
|
|
|
|
def get_n_words(fi,n):
|
|
i=0
|
|
txt=""
|
|
c=" "
|
|
space=False
|
|
while i<40 and len(c)==1:
|
|
c = fi.read(1)
|
|
txt+=c
|
|
if c==" " and not space:
|
|
i+=1
|
|
space=True
|
|
else:
|
|
space=False
|
|
|
|
if i==40:
|
|
return (txt,True)
|
|
return (txt,False)
|
|
|
|
def prev(path):
|
|
fi = codecs.open(path, mode="r", encoding=cfg.encoding)
|
|
txt=get_n_words(fi,40)
|
|
print '<div class="preview">'+txt[0].encode(cfg.encoding)
|
|
if txt[1]:
|
|
print '<i> ...</i>'
|
|
print '</div>'
|
|
fi.close()
|
|
|
|
def prev_txt(path):
|
|
fi = codecs.open(path, mode="r", encoding=cfg.encoding)
|
|
txt=get_n_words(fi,40)
|
|
print '<div class="preview">'+markdown.markdown(txt[0],extensions=cfg.markdown_extensions, content_format=cfg.content_format).encode(cfg.encoding)
|
|
if txt[1]:
|
|
print '<i> ...</i>'
|
|
print '</div>'
|
|
fi.close()
|
|
|
|
def prev_source(path):
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# Legacy code ------------------------
|
|
# using vim for syntax highlighting,could be usefull,
|
|
# old disp_source using vim
|
|
#def disp_source(path):
|
|
# print '<div class="sourcecode">'
|
|
# print get_source_html(path).encode(cfg.encoding)
|
|
# #print source
|
|
# print '</div>'
|
|
class BodyParser(HTMLParser):
|
|
body=False
|
|
src=''
|
|
blacklist = ['script']
|
|
def handle_starttag(self, tag, attrs):
|
|
if self.body and not(tag in self.blacklist):
|
|
self.src+='<'+tag
|
|
for attr in attrs:
|
|
self.src+=' '+attr[0]+'="'+attr[1]+'"'
|
|
self.src+='>'
|
|
if tag=='body':
|
|
self.body=True
|
|
def handle_endtag(self, tag):
|
|
if tag=='body':
|
|
self.body=False
|
|
if self.body and not(tag in self.blacklist):
|
|
self.src+='</'+tag+'>'
|
|
def handle_data(self, data):
|
|
if self.body:
|
|
self.src+=data
|
|
def handle_entityref(self,ref):
|
|
self.handle_charref(ref)
|
|
def handle_charref(self,ref):
|
|
if self.body:
|
|
self.src += '&'+ref+';'
|
|
|
|
def get_source_html(path):
|
|
tmpdir = os.path.join(tempfile.gettempdir(),"pylog")
|
|
cache=False
|
|
if not os.path.exists(tmpdir):
|
|
os.mkdir(tmpdir,0700)
|
|
|
|
if os.path.exists(tmpdir):
|
|
s=os.stat(tmpdir)
|
|
if s.st_mode & stat.S_IRWXU == stat.S_IRWXU and \
|
|
s.st_mode & stat.S_IRWXG == 0 and \
|
|
s.st_mode & stat.S_IRWXO == 0 and \
|
|
os.path.isdir(tmpdir):
|
|
f=codecs.open(path, mode='r', encoding=cfg.encoding)
|
|
tmpfile=os.path.join(tmpdir,hashlib.sha256(f.read()).hexdigest())
|
|
f.close()
|
|
cache=True
|
|
|
|
if not cache:
|
|
tmpfile=tempfile.mkstemp(prefix="pylog",dir=tmpdir)[1]
|
|
|
|
if not cache or (cache and not os.path.exists(tmpfile)):
|
|
#we use a fork of vim to generate a nice syntax highlighted html file
|
|
#TODO this requires further testing
|
|
p=subprocess.Popen(["/usr/bin/vim",path,\
|
|
"-nEs",\
|
|
"+syn on",\
|
|
"+let html_no_progress=1",\
|
|
"+let html_use_css=1",\
|
|
"+let html_number_lines=1",\
|
|
"+run! syntax/2html.vim",\
|
|
"+w! "+tmpfile,\
|
|
"+q!",\
|
|
"+q!"],\
|
|
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
|
|
|
|
p.stdout.close()
|
|
p.stderr.close()
|
|
p.wait() #wait for child to terminate
|
|
|
|
f=codecs.open(tmpfile, mode="r", encoding=cfg.encoding)
|
|
bp=BodyParser()
|
|
bp.feed(f.read())
|
|
src = bp.src
|
|
f.close()
|
|
#delete tmp file but only if it's not a secure cached file
|
|
if not cache:
|
|
os.remove(tmpfile)
|
|
return src
|
|
|
|
|