commit
2ac5bce8a9
|
After Width: | Height: | Size: 519 B |
@ -0,0 +1,159 @@ |
||||
#!/usr/bin/python |
||||
|
||||
import os,os.path, sys |
||||
import urllib, cgi |
||||
|
||||
from datetime import datetime |
||||
|
||||
from pylog_action import * |
||||
import pylog_config as cfg |
||||
|
||||
form = {} |
||||
def init(): |
||||
global form |
||||
import cgitb |
||||
cgitb.enable() |
||||
form = cgi.FieldStorage() |
||||
|
||||
print 'Content-type: text/html\n\n' |
||||
|
||||
def header(): |
||||
print '<head>' |
||||
print '<title>'+cfg.title+'</title>' |
||||
print '<h1>'+cfg.blogtitle+'</h1>' |
||||
print '<h2>'+cfg.subtitle+'</h2>' |
||||
print cfg.header |
||||
print '</head>' |
||||
|
||||
def dir_entry(f,path,depth,hidden): |
||||
if '_' in os.path.basename(f): |
||||
typ = f[f.rindex('_')+1:] |
||||
name = os.path.basename(f)[:-(len(typ)+1)] |
||||
else: |
||||
typ = '' |
||||
name = os.path.basename(f) |
||||
|
||||
if typ in dir_actions: |
||||
dir_actions[typ](name,f,depth,hidden) |
||||
|
||||
def file_entry(f,path,depth,hidden): |
||||
if '.' in os.path.basename(f): |
||||
typ = f[f.rindex('.')+1:] |
||||
name = os.path.basename(f)[:-(len(typ)+1)] |
||||
else: |
||||
typ = '' |
||||
name = os.path.basename(f) |
||||
if typ in disp_actions: |
||||
file_link(name,f,depth,hidden) |
||||
|
||||
def process_dirs(path='.',disppath='.',depth=0,hidden=False): |
||||
dirlist = os.listdir(path) |
||||
if len(dirlist)!=0: |
||||
dir_begin(disppath,hidden) |
||||
dirs = [] |
||||
files = [] |
||||
for f in dirlist: |
||||
f = os.path.normpath(os.path.join(path,f)) |
||||
if os.path.isdir(f): |
||||
dirs.append(f) |
||||
else: |
||||
files.append(f) |
||||
|
||||
dirs.sort() |
||||
files.sort() |
||||
for f in dirs+files: |
||||
if os.path.isdir(f): |
||||
dir_entry(f,disppath,depth,hidden) |
||||
if disppath[:len(f)]==f: |
||||
process_dirs(f,disppath,depth+1,False) |
||||
else: |
||||
process_dirs(f,disppath,depth+1,True) |
||||
else: |
||||
file_entry(f,disppath,depth,hidden) |
||||
|
||||
dir_end(disppath,hidden) |
||||
|
||||
def disp_list(path): |
||||
script = sys.argv[0][sys.argv[0].rindex('/')+1:] |
||||
|
||||
files = {} |
||||
for f in os.listdir(path): |
||||
f = os.path.normpath(os.path.join(path,f)) |
||||
if not os.path.isdir(f): |
||||
files[os.path.getctime(f)]=f |
||||
keys = files.keys() |
||||
keys.sort() |
||||
keys.reverse() |
||||
for k in keys: |
||||
f=files[k] |
||||
if '.' in os.path.basename(f): |
||||
typ = f[f.rindex('.')+1:] |
||||
name = os.path.basename(f)[:-(len(typ)+1)] |
||||
else: |
||||
typ = '' |
||||
name = os.path.basename(f) |
||||
if typ in prev_actions and not os.path.isdir(f): |
||||
sys.stdout.write('<h3><a class="entryheader" href="'+script+'?') |
||||
sys.stdout.write(urllib.urlencode({'path': os.path.normpath(os.path.dirname(f)), \ |
||||
'disp':os.path.normpath(f)})+'">') |
||||
print name+'</a> - <span class="entrydate">', |
||||
print datetime.fromtimestamp(int(k)).strftime(cfg.datefmt), |
||||
print '</span></h3>' |
||||
prev_actions[typ](f) |
||||
|
||||
|
||||
def get_valid_path(path): |
||||
if '..' in path or os.path.normpath(path)=='.': |
||||
return cfg.contentdir |
||||
valid_chars='.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/_0123456789' |
||||
for c in path: |
||||
if not c in valid_chars: |
||||
return cfg.contentdir |
||||
return os.path.normpath(path) |
||||
|
||||
init() |
||||
path = cfg.contentdir |
||||
if 'path' in form: |
||||
path=form['path'].value |
||||
|
||||
path=get_valid_path(path) |
||||
|
||||
|
||||
print '<html>' |
||||
header() |
||||
print '<body>' |
||||
|
||||
print '<div id="menu">' |
||||
process_dirs(cfg.contentdir,path) |
||||
print '</div>' |
||||
print '<div id="content">' |
||||
try: |
||||
if 'disp' in form: |
||||
validdisp = get_valid_path(form['disp'].value) |
||||
disp=os.path.basename(validdisp) |
||||
|
||||
if not os.path.isdir(disp): |
||||
|
||||
if '.' in disp: |
||||
typ = disp[disp.rindex('.')+1:] |
||||
name = disp[:-(len(typ)+1)] |
||||
else: |
||||
typ = '' |
||||
name = disp |
||||
if typ in disp_actions: |
||||
sys.stdout.write('<h3><span class="entryheader">') |
||||
print name+'</span> - <span class="entrydate">', |
||||
print datetime.fromtimestamp( \ |
||||
int(os.path.getctime(validdisp)) \ |
||||
).strftime(cfg.datefmt), |
||||
print '</span></h3>' |
||||
|
||||
disp_actions[typ](validdisp) |
||||
else: |
||||
disp_list(path) |
||||
except OSError: |
||||
pass |
||||
|
||||
print '</div>' |
||||
print '</body>' |
||||
print '</html>' |
||||
@ -0,0 +1,25 @@ |
||||
import os, os.path, sys |
||||
import urllib |
||||
from datetime import datetime |
||||
|
||||
import pylog_config as cfg |
||||
from pylog_action_impl import * |
||||
|
||||
dir_actions = { |
||||
'': dir_link |
||||
} |
||||
|
||||
disp_actions = { |
||||
'txt': disp_txt, |
||||
'html': disp, |
||||
'c':disp_source, |
||||
'py':disp_source |
||||
} |
||||
|
||||
prev_actions = { |
||||
'txt': prev_txt, |
||||
'html': prev, |
||||
'c':prev_source, |
||||
'py':prev_source |
||||
} |
||||
|
||||
@ -0,0 +1,142 @@ |
||||
import os, os.path, sys |
||||
import urllib |
||||
from datetime import datetime |
||||
import pylog_config as cfg |
||||
import subprocess,tempfile |
||||
|
||||
from HTMLParser import HTMLParser |
||||
|
||||
def dir_begin(path,hidden): |
||||
print '<ul', |
||||
if hidden: |
||||
print 'class="hidden"', |
||||
print'>' |
||||
def dir_end(path,hidden): |
||||
print '</ul>' |
||||
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+'?'+ urllib.urlencode({'path': os.path.normpath(f)}) +'">'+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+'?'+ urllib.urlencode({'path': os.path.normpath(os.path.dirname(f)), 'disp':os.path.normpath(f)}) +'">'+name+'</a>' |
||||
print ' </li>' |
||||
|
||||
def disp_txt(path): |
||||
print '<div class="entrytext"><span class="pre">' |
||||
f = open(path) |
||||
print f.read() |
||||
f.close() |
||||
print '</span></div>' |
||||
|
||||
class BodyParser(HTMLParser): |
||||
body=False |
||||
src='' |
||||
def handle_starttag(self, tag, attrs): |
||||
if self.body: |
||||
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: |
||||
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): |
||||
tmpfile=tempfile.mkstemp(prefix="pylog")[1] |
||||
#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,\ |
||||
"-n",\ |
||||
"-cTOhtml",\ |
||||
"-cw! "+tmpfile,\ |
||||
"-cq!",\ |
||||
"-cq!"],\ |
||||
bufsize=2**16,stdout=subprocess.PIPE,stderr=subprocess.PIPE,env={"HOME":os.getcwd()}) |
||||
p.stdout.close() |
||||
p.stderr.close() |
||||
# p.stdin.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 |
||||
os.remove(tmpfile) |
||||
return src |
||||
|
||||
def disp_source(path): |
||||
print '<div class="sourcecode">' |
||||
print get_source_html(path) |
||||
#print source |
||||
print '</div>' |
||||
|
||||
def disp(path): |
||||
print '<div class="entrytext">' |
||||
f = open(path) |
||||
print f.read() |
||||
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 = open(path) |
||||
txt=get_n_words(fi,40) |
||||
print '<div class="preview">'+txt[0] |
||||
if txt[1]: |
||||
print '<i> ...</i>' |
||||
print '</div>' |
||||
fi.close() |
||||
|
||||
def prev_txt(path): |
||||
fi = open(path) |
||||
txt=get_n_words(fi,40) |
||||
print '<div class="preview"><span class="pre">'+txt[0] |
||||
if txt[1]: |
||||
print '<i> ...</i>' |
||||
print '</span></div>' |
||||
fi.close() |
||||
|
||||
def prev_source(path): |
||||
return |
||||
|
||||
@ -0,0 +1,10 @@ |
||||
title = 'Title of destruction' |
||||
blogtitle= 'Le Blog' |
||||
subtitle='Some subtitle... and cats!' |
||||
|
||||
header=""" |
||||
<link rel="stylesheet" type="text/css" href="style.css" /> |
||||
<link rel="stylesheet" type="text/css" href="source.css" /> |
||||
""" |
||||
datefmt='%Y-%m-%d' |
||||
contentdir="cnt" |
||||
@ -0,0 +1,20 @@ |
||||
.Type { color: #00ff00; } |
||||
.Special { color: #ff40ff; } |
||||
.Constant { color: #ff6060; } |
||||
.Statement { color: #ffff00; } |
||||
.Identifier { color: #00ffff; } |
||||
.Comment { color: #8080ff; } |
||||
|
||||
.sourcecode { |
||||
white-space:wrap; |
||||
font-family:monospace; |
||||
color:#cccccc; |
||||
background-color:#222222; |
||||
padding:8px; |
||||
} |
||||
|
||||
.lnr { |
||||
display:inline; |
||||
color:#666666; |
||||
} |
||||
|
||||
@ -0,0 +1,126 @@ |
||||
/* generall */ |
||||
.hidden { |
||||
display: none |
||||
} |
||||
a { |
||||
color:#aa6735; |
||||
text-decoration:underline; |
||||
font-weight:bold; |
||||
a:hover{ |
||||
} |
||||
text-decoration:none; |
||||
} |
||||
|
||||
/* header */ |
||||
h1 { |
||||
font-size:18pt; |
||||
font-weight:bold; |
||||
padding:20px 0 0 15px; |
||||
margin:0; |
||||
color:#5598ca; |
||||
background-color:#eeeeee; |
||||
height:35px; |
||||
background-image:url(_img/tbg.png); |
||||
} |
||||
h2 { |
||||
font-size:10pt; |
||||
font-weight:bold; |
||||
padding:0 0 10px 40px; |
||||
margin:0; |
||||
color:#aa6735; |
||||
background-color:#eeeeee; |
||||
background-position:0 -55px; |
||||
background-image:url(_img/tbg.png); |
||||
height:15px; |
||||
} |
||||
|
||||
/* menu */ |
||||
#menu { |
||||
float:left; |
||||
width:15%; |
||||
} |
||||
#menu ul,li{ |
||||
margin:0; |
||||
padding:0; |
||||
list-style-type:none; |
||||
} |
||||
#menu ul{ |
||||
padding-left:10px; |
||||
border-collapse:collapse; |
||||
border-left:2px solid #ffffff; |
||||
} |
||||
|
||||
#menu ul, #menu ul ul ul{ |
||||
background-color:#ffffff; |
||||
} |
||||
|
||||
#menu ul ul, #menu ul ul ul ul{ |
||||
background-color:#eeeeee; |
||||
} |
||||
|
||||
|
||||
#menu li{ |
||||
|
||||
} |
||||
a.menulink { |
||||
padding:5px 0 5px 0; |
||||
display:block; |
||||
color:#5598ca; |
||||
font-weight:bold; |
||||
text-decoration:none; |
||||
font-size:11pt; |
||||
width:100%; |
||||
} |
||||
a.menulink:hover{ |
||||
color:white; |
||||
background-color:#777777; |
||||
text-decoration:underline; |
||||
} |
||||
|
||||
/* content */ |
||||
body { |
||||
margin:0; |
||||
background-color:#ffffff; |
||||
padding:0; |
||||
font-family:sans-serif; |
||||
color:black; |
||||
} |
||||
h3 { |
||||
font-size:14pt; |
||||
font-weight:bold; |
||||
margin:0 0 30px 20px; |
||||
color:#5598ca; |
||||
} |
||||
.entryheader { |
||||
color:#5598ca; |
||||
} |
||||
|
||||
.entrydate { |
||||
font-size:9pt; |
||||
color:#cccccc; |
||||
} |
||||
|
||||
#content { |
||||
padding:5px; |
||||
background-color:#f6f6f6; |
||||
margin-left:22%; |
||||
width:70%; |
||||
box-shadow: 6px 6px 5px #444444; |
||||
border: #f6f6f6 solid 15px; |
||||
border-radius: 15px; |
||||
margin-bottom:40px; |
||||
color:black; |
||||
} |
||||
|
||||
span.pre { |
||||
white-space:pre-wrap; |
||||
font-family:monospace; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue