# -*- coding: iso-8859-1 -*-
"""Classes to generate HTML in Python
The HTMLTags module defines a class for all the valid HTML tags, written in
uppercase letters. To create a piece of HTML, the general syntax is :
t = TAG(innerHTML, key1=val1,key2=val2,...)
so that "print t" results in :
innerHTML
For instance :
print A('bar', href="foo") ==> bar
To generate HTML attributes without value, give them the value True :
print OPTION('foo',SELECTED=True,value=5) ==>
For non-closing tags such as or , the print statement does not
generate the closing tag
The innerHTML argument can be an instance of an HTML class, so that you can nest
tags, like this :
print B(I('foo')) ==> foo
Instances of the HTML classes support the addition :
print B('bar')+INPUT(name="bar") ==> bar
and also repetition :
print TH(' ')*3 ==>
If you have a list (or any iterable) of instances, you can't concatenate the items with
sum(instanceList) because sum takes only numbers as arguments. So there is a
function called Sum which will do the job :
Sum( TR(TD(i)+TD(i*i)) for i in range(100) )
generates the rows of a table showing the squares of integers from 0 to 99
The innerHTML argument can be a string, but you can't concatenate a string and
an instance of an HTML class, like in :
H1('To be or ' + B('not to be'))
For this, use a class called TEXT, which will not generate any tag :
H1(TEXT('To be or ') + B('not to be'))
A simple document can be produced by :
print HTML( HEAD(TITLE('Test document')) +
BODY(H1('This is a test document')+
TEXT('First line')+BR()+
TEXT('Second line')))
This will produce :
Test document
This is a test document
First line
Second line
If the document is more complex it is more readable to create the elements
first, then to print the whole result in one instruction. For example :
stylesheet = LINK(rel="Stylesheet",href="doc.css")
head= HEAD(TITLE('Record collection')+stylesheet)
title = H1('My record collection')
rows = Sum ([TR(TD(rec.title,Class="title")+TD(rec.artist,Class="Artist"))
for rec in records])
table = TABLE(TR(TH('Title')+TH('Artist')) + rows)
print HTML(head + BODY(title + table))
"""
## from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366000
## NOTE: I changed TAG.__str__ to emit lower-case tags
import cStringIO, string, copy, types
class TAG:
"""Generic class for tags"""
def __init__(self, innerHTML="", **attrs):
self.tag = [self.__class__.__name__]
self.innerHTML = [innerHTML]
self.attrs = [attrs]
def __str__(self):
res = cStringIO.StringIO()
w = res.write
for tag, innerHTML, attrs in zip(self.tag, self.innerHTML, self.attrs):
# force unicode
tag = unicode(tag)
innerHTML = unicode(innerHTML)
if not tag == 'TEXT':
if tag in _WS_INSENSITIVE:
w(u'\n')
#w("<%s" %tag)
w(u'<%s' % string.lower(tag))
# attributes which will produce arg = "val"
attr1 = [ k for k in attrs if not isinstance(attrs[k], bool) ]
#w("".join([' %s="%s"' % (k, attrs[k]) for k in attr1]))
# AJI: force attribute names to lowercase to handle the 'Class="lala"' workaround
#w(u''.join([u' %s="%s"' % (string.lower(k), self.__to_utf8(attrs[k])) for k in attr1]))
# be delicate when handling unicode
for k in attr1:
w(u' %s="' % k)
#w(self.__to_utf8(attrs[k]))
if type(attrs[k]) in types.StringTypes:
w(attrs[k].encode('ascii', 'xmlcharrefreplace'))
else:
w(str(attrs[k]))
w(u'"')
# attributes with no argument
# if value is False, don't generate anything
attr2 = [ k for k in attrs if attrs[k] is True ]
w(u''.join([u' %s' %k for k in attr2]))
if len(unicode(innerHTML)) > 0:
w(u'>')
else:
w(u'/>') # default to xhtml tags
# encode to HTML entities
w(innerHTML.encode('ascii', 'xmlcharrefreplace'))
if tag in ClosingTags:
#w("%s>" %tag)
w(u'%s>' % string.lower(tag))
return res.getvalue()
def __to_utf8(self, s):
"""courtesy http://maxischenko.in.ua/blog/entries/89/sqlobject-unicode-and-ascii-error/"""
if isinstance(s, str):
pass
elif hasattr(s, '__unicode__'):
s = unicode(s, 'utf-8')
if isinstance(s, unicode):
s = s.encode('utf-8')
if not isinstance(s, unicode):
# force conversion to unicode string
s = unicode(s)
return s
def __add__(self,other):
"""Concatenate another tag to self"""
self.tag += other.tag
self.innerHTML += other.innerHTML
self.attrs += other.attrs
return self
#AJI: original code returned multiplication incorrectly
#objself = copy.copy(self)
#objself.tag += other.tag
#objself.innerHTML += other.innerHTML
#objself.attrs += other.attrs
#return objself
def __mul__(self,n):
"""Replicate self n times"""
return Sum([self]*n)
# AJI: original code does NOT work properly when n > 2
#return Sum([objtag for copy.copy(self) in range(n)])
#return Sum([copy.copy(self) for intx in range(n)])
#lstmultiple = []
#for intx in range(n):
#lstmultiple.append(copy.copy(self))
#return Sum(lstmultiple)
# list of tags, from the HTML 4.01 specification
ClosingTags = ['A', 'ABBR', 'ACRONYM', 'ADDRESS', 'APPLET',
'B', 'BDO', 'BIG', 'BLOCKQUOTE', 'BUTTON',
'CAPTION', 'CENTER', 'CITE', 'CODE',
'DEL', 'DFN', 'DIR', 'DIV', 'DL',
'EM', 'FIELDSET', 'FONT', 'FORM', 'FRAMESET',
'H1', 'H2', 'H3', 'H4', 'H5', 'H6',
'I', 'IFRAME', 'INS', 'KBD', 'LABEL', 'LEGEND',
'MAP', 'MENU', 'NOFRAMES', 'NOSCRIPT', 'OBJECT',
'OL', 'OPTGROUP', 'PRE', 'Q', 'S', 'SAMP',
'SCRIPT', 'SELECT', 'SMALL', 'SPAN', 'STRIKE',
'STRONG', 'STYLE', 'SUB', 'SUP', 'TABLE',
'TEXTAREA', 'TITLE', 'TT', 'U', 'UL',
'VAR', 'BODY', 'COLGROUP', 'DD', 'DT', 'HEAD',
'HTML', 'LI', 'OPTION', 'P', 'TBODY',
'TD', 'TFOOT', 'TH', 'THEAD', 'TR']
NonClosingTags = ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME',
'HR', 'IMG', 'INPUT', 'ISINDEX', 'LINK',
'META', 'PARAM']
# create the classes
for tag in ClosingTags + NonClosingTags + ['TEXT']:
exec("class %s(TAG): pass" %tag)
def Sum(iterable):
"""Return the concatenation of the instances in the iterable
Can't use the built-in sum() on non-integers"""
return reduce(lambda x,y:x+y, iterable)
#AJI: original code incorrectly sums multipled tags
#objtagfinal = iterable[0]
#for objtag in iterable[1:]:
#objtagfinal = objtagfinal + objtag
#return objtagfinal
# whitespace-insensitive tags, determines pretty-print rendering
_WS_INSENSITIVE = NonClosingTags + ['HTML','HEAD','BODY',
'FRAMESET','FRAME',
'TITLE','SCRIPT',
'TABLE','TR','TD','TH','SELECT','OPTION',
'FORM']
if __name__ == "__main__":
class Record:
def __init__(self,title,artist):
self.title = title
self.artist = artist
records = [Record('Antics','Interpol'),
Record('Employment','Kaiser Chiefs')]
stylesheet = LINK(rel="Stylesheet",href="doc.css")
head= HEAD(TITLE('Record collection')+stylesheet)
title = H1('My record collection')
rows = Sum (TR(TD(rec.title,Class="title")+TD(rec.artist,Class="Artist")) for rec in records)
table = TABLE(TR(TH('Title')+TH(TEXT('Artist'))) + rows)
print HTML(head + BODY(title + table))