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 '
  • ' 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 = 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 '
    ' 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 '
    ' 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 '
    ' def disp(path): print '
    ' f = codecs.open(path, mode="r", encoding=cfg.encoding)(path) print f.read().encode(cfg.encoding) 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 = codecs.open(path, mode="r", encoding=cfg.encoding) txt=get_n_words(fi,40) print '
    '+txt[0].encode(cfg.encoding) if txt[1]: print ' ...' print '
    ' fi.close() def prev_txt(path): fi = codecs.open(path, mode="r", encoding=cfg.encoding) txt=get_n_words(fi,40) print '
    '+markdown.markdown(txt[0],extensions=cfg.markdown_extensions, content_format=cfg.content_format).encode(cfg.encoding) if txt[1]: print ' ...' print '
    ' 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 '
    ' # print get_source_html(path).encode(cfg.encoding) # #print source # 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=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