1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from sys import stdout, modules
13 doc_type = '<!DOCTYPE HTML>\n'
14 default_title = "Html Page"
15 charset = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n'
101 disused_tags = {'isindex', 'font', 'dir', 's', 'strike',
102 'u', 'center', 'basefont', 'applet', 'xmp'}
137 tags = html4_tags | disused_tags | html5_tags
139 __all__ = [x.title() for x in tags] + ['PyHtml', 'space']
141 self_close = {'input', 'img', 'link', 'br'}
151 def __init__(self, *args, **kwargs):
152 self.attributes = kwargs
159 self._id = kwargs.get('id', name)
163 def __iadd__(self, obj):
164 if isinstance(obj, Tag) and obj.is_seq:
171 def add_obj(self, obj):
172 if not isinstance(obj, Tag):
174 _id = self.set_id(obj)
175 setattr(self, _id, obj)
178 def set_id(self, obj):
179 if isinstance(obj, Tag):
181 obj_lst = filter(lambda t: isinstance(
182 t, Tag) and t._id.startswith(_id), self)
185 obj_lst = filter(lambda t: not isinstance(t, Tag), self)
186 length = len(obj_lst)
188 _id = '%s_%03i' % (_id, length)
189 if isinstance(obj, Tag):
193 def __add__(self, obj):
195 return Tag(self, obj)
199 def __lshift__(self, obj):
200 if isinstance(obj, Tag):
203 print "unknown obj: %s " % obj
209 result += '<%s%s%s>' % (self.tag_name,
211 self._self_close() * ' /')
212 if not self._self_close():
215 if isinstance(c, Tag):
222 result += '</%s>' % self.tag_name
226 def _render_attr(self):
228 for key, value in self.attributes.iteritems():
229 if key != 'txt' and key != 'open':
232 result += ' %s="%s"' % (key, value)
235 def _self_close(self):
236 return self.tag_name in self_close
239 def tag_factory(tag):
243 F.__name__ = tag.title()
247 THIS = modules[__name__]
250 setattr(THIS, t.title(), tag_factory(t))
252 THIS = modules[__name__]
254 obj = type(t.title(), (Tag, ), {'tag_name': t})
255 setattr(THIS, t.title(), obj)
258 def _render_style(style):
264 for key, value in values.iteritems():
265 result += " %s: %s;\n" % (key, value)
268 result = '\n' + result
275 def __init__(self, title=default_title):
279 self.attributes = dict(xmlns='http://www.w3.org/1999/xhtml', lang='en')
280 self.head += Title(title)
282 def __iadd__(self, obj):
283 if isinstance(obj, Head) or isinstance(obj, Body):
285 elif isinstance(obj, Meta) or isinstance(obj, Link):
289 _id = self.set_id(obj)
290 setattr(self, _id, obj)
293 def add_js(self, *arg):
295 self.head += Script(type='text/javascript', src=f)
297 def add_css(self, *arg):
299 self.head += Link(rel='stylesheet', type='text/css', href=f)
301 def output(self, name=''):
303 fil = open(name, 'w')
306 fil.write(self.as_string())
312 return doc_type + self.render()
314 def add_style(self, style):
315 self.head += Style(_render_style(style))
317 def add_table(self, data):
318 table = self << Table()
320 cols = len(zip(*data))
322 for i in range(rows):
325 for j in range(1, cols):