#!/usr/bin/python
"""
classe di ausilio per la creazione di una pagina web
"""
import types
import js_library
def import_js(nome):
"""
sulla base del nome, carica dalla libreria
"""
return js_library.js_source(nome)
#return "<script src='%s'></script>" % nome
def tostr(cosa):
if type(cosa) in (types.StringType,types.UnicodeType):
return cosa
else:
return "\n".join(cosa)
class webpage:
def __init__(self,nome="webpage"):
self.diz ={"nome":nome,
"js_dict":{},
"testa":[],
"corpo":[],
"piede":[],
"css":[],
"rss":[],
"icona":"",
}
def js_function(self,nome):
if nome in self.diz["js_dict"].keys():
return
else:
self.diz["js_dict"][nome]=import_js(nome)
def set_icon(self,url_icona,tipo="image_url/png"):
self.diz["icona"]=url_icona
self.diz["icona_tipo"]=tipo
def add_rss(self,url_rss,titolo):
self.diz["rss"].append([url_rss,titolo])
def set_div_top(self,html):
"""
html = list or string
"""
self.diz["testa"]=html
def set_div_main(self,html):
"""
html = list or string
"""
self.diz["corpo"]=html
def set_div_bottom(self,html):
"""
html = list or string
"""
self.diz["piede"]=html
def html_tag_icon(self):
x=(self.diz.get("icona"),self.diz.get("icona_tipo"))
if x[0] > "":
return "<link rel='icon' href='%s' type='%s'>" % x
else:
return ""
def html_tag_rss(self):
modello = '<link rel="alternate" type="application/rss+xml" title="%s" href="%s">'
return "\n".join([modello % (x[0],x[1]) for x in self.diz["rss"]])
def html_link_rss(self):
if len(self.diz["rss"])>0:
x="RSS:"
modello = "<a href='%s' title='%s'>%s<img src='/gantt/rss.png'></a>"
return x+"\n".join([modello % (x[0],x[1],x[1]) for x in self.diz["rss"]])
else:
return ""
def html(self):
design = {
"nome":self.diz["nome"],
"icona":self.html_tag_icon(),
"rss":self.html_tag_rss(),
"link_rss":self.html_link_rss(),
"html_css":tostr(self.html_css),
"js_dict":tostr(self.diz["js_dict"].values()),
"testa":tostr(self.diz["testa"]),
"corpo":tostr(self.diz["corpo"]),
"piede":tostr(self.diz["piede"])}
html="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html charset-encoding='UTF-8'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
%(nome)s
</title>
<!-- inizializzazione ambiente client -->
%(icona)s
%(rss)s
%(html_css)s
%(js_dict)s
<!-- inizio contenuti -->
</head>
<body>
<div id='testa'>%(testa)s</div>
<div id='corpo'>%(corpo)s</div>
<div style='position:fixed; top:100%%; width:100%%; '>
<div id='piede' style='position:relative;'>
<hr>
%(nome)s %(link_rss)s %(piede)s
<div>
</div>
</body>
</html>""" % design
return html
|