import os, os.path, stat, sys import urllib, hashlib 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 '
  • ' print '' def dir_end(path,hidden,depth): print '' if depth!=0: print '
  • ' def dir_link(name,f,depth,hidden): script = sys.argv[0][sys.argv[0].rindex('/')+1:] print ' ' def file_link(name,f,depth,hidden): script = sys.argv[0][sys.argv[0].rindex('/')+1:] print '
  • ' print ' '+name+'' print '
  • ' def disp_txt(path): print '
    ' f = open(path) print markdown.markdown(f.read(),extensions=['math']) f.close() print '
    ' 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+='' 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=open(path,'r') 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=open(tmpfile,"r") 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 def disp_source(path): print '
    ' print get_source_html(path) #print source print '
    ' def disp(path): print '
    ' f = open(path) print f.read() f.close() print '
    ' 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 = open(path) txt=get_n_words(fi,40) print '
    '+txt[0] if txt[1]: print ' ...' print '
    ' fi.close() def prev_txt(path): fi = open(path) txt=get_n_words(fi,40) print '
    '+markdown.markdown(txt[0],extensions=['math']) if txt[1]: print ' ...' print '
    ' fi.close() def prev_source(path): return