Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1637877
  • 博文数量: 126
  • 博客积分: 1541
  • 博客等级: 上尉
  • 技术积分: 1914
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-20 11:14
文章分类

全部博文(126)

文章存档

2014年(21)

2013年(42)

2012年(63)

分类: Python/Ruby

2014-07-23 09:12:55

写了一个例子,可以使用python手动生成html页面。
注释里面有相关的必要信息。如下:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. '''an example for generate html page.
  3. the module used is:PyH
  4. Its home page is:
  5. we can download it at:downloads/detail?name=PyH-0.1.1.tar.gz&can=2&q=
  6. More things about html at:
  7. '''
  8. from pyh import *
  9. page = PyH("MyPage")
  10. #page.addCSS('myStylesheet1.css', 'myStylesheet2.css')
  11. #page.addJS('myJavascript1.js', 'myJavascript2.js')

  12. #<h1 align="center">MyTitle1</h1>
  13. page <<h1('MyTitle1',align='center')

  14. #<div align="center" id="myDiv1"><p id="myp1">my paragraph1</p>
  15. #</div>
  16. page << div(align='center',id='myDiv1') << p('my paragraph1',id='myp1')

  17. #<div id="myDiv2"><h2>title2 in div2<p>paragraph under title2</p>
  18. #</h2>
  19. #</div>
  20. mydiv2 = page << div(id='myDiv2')
  21. mydiv2 <<h2('title2 in div2') << p('paragraph under title2')

  22. #<div align="right" id="myDiv3"><p>paragraph in mydiv3</p>
  23. #</div>
  24. mydiv3 = page << div(id='myDiv3')
  25. mydiv3.attributes['align'] = 'right'
  26. mydiv3 << p('paragraph in mydiv3')

  27. #<table border="1" id="mytable1"><tr id="headline"><td>Head1<td>Head2</td>
  28. #</td>
  29. #</tr>
  30. #<tr id="line1"><td>r1,c1<td>r1,c2</td>
  31. #</td>
  32. #</tr>
  33. #<tr id="line2"><td>r2,c1<td>r2,c2</td>
  34. #</td>
  35. #</tr>
  36. #</table>
  37. table1 = page << table(border='1',id='mytable1')
  38. headtr = table1 << tr(id='headline')
  39. headtr << td('Head1') << td('Head2')

  40. tr1 = table1 << tr(id='line1')
  41. tr1 << td('r1,c1') <<td('r1,c2')

  42. tr2 = table1 << tr(id='line2')
  43. tr2 << td('r2,c1') <<td('r2,c2')



  44. #The result is:
  45. #<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
  46. #<html lang="en" xmlns=""><head><title>MyPage</title>
  47. #</head>
  48. #<body><h1 align="center">MyTitle1</h1>
  49. #<div align="center" id="myDiv1"><p id="myp1">my paragraph1</p>
  50. #</div>
  51. #<div id="myDiv2"><h2>title2 in div2<p>paragraph under title2</p>
  52. #</h2>
  53. #</div>
  54. #<div align="right" id="myDiv3"><p>paragraph in mydiv3</p>
  55. #</div>
  56. #<table border="1" id="mytable1"><tr id="headline"><td>Head1<td>Head2</td>
  57. #</td>
  58. #</tr>
  59. #<tr id="line1"><td>r1,c1<td>r1,c2</td>
  60. #</td>
  61. #</tr>
  62. #<tr id="line2"><td>r2,c1<td>r2,c2</td>
  63. #</td>
  64. #</tr>
  65. #</table>
  66. #</body>
  67. #</html>
  68. page.printOut()


这里,pyh是一个生成html的模块库,已经给出其网址,也将代码列出如下:

点击(此处)折叠或打开

  1. # @file: pyh.py
  2. # @purpose: a HTML tag generator
  3. # @author: Emmanuel Turlay <turlay@cern.ch>

  4. __doc__ = """The pyh.py module is the core of the PyH package. PyH lets you
  5. generate HTML tags from within your python code.
  6. See for documentation.
  7. """
  8. __author__ = "Emmanuel Turlay "
  9. __version__ = '$Revision: 63 $'
  10. __date__ = '$Date: 2010-05-21 03:09:03 +0200 (Fri, 21 May 2010) $'

  11. from sys import _getframe, stdout, modules, version
  12. nOpen={}

  13. nl = '\n'
  14. doctype = '\n'
  15. charset = '\n'

  16. tags = ['html', 'body', 'head', 'link', 'meta', 'div', 'p', 'form', 'legend',
  17.         'input', 'select', 'span', 'b', 'i', 'option', 'img', 'script',
  18.         'table', 'tr', 'td', 'th', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
  19.         'fieldset', 'a', 'title', 'body', 'head', 'title', 'script', 'br', 'table',
  20.         'ul', 'li', 'ol']

  21. selfClose = ['input', 'img', 'link', 'br']

  22. class Tag(list):
  23.     tagname = ''
  24.     
  25.     def __init__(self, *arg, **kw):
  26.         self.attributes = kw
  27.         if self.tagname :
  28.             name = self.tagname
  29.             self.isSeq = False
  30.         else:
  31.             name = 'sequence'
  32.             self.isSeq = True
  33.         self.id = kw.get('id', name)
  34.         #self.extend(arg)
  35.         for a in arg: self.addObj(a)

  36.     def __iadd__(self, obj):
  37.         if isinstance(obj, Tag) and obj.isSeq:
  38.             for o in obj: self.addObj(o)
  39.         else: self.addObj(obj)
  40.         return self
  41.     
  42.     def addObj(self, obj):
  43.         if not isinstance(obj, Tag): obj = str(obj)
  44.         id=self.setID(obj)
  45.         setattr(self, id, obj)
  46.         self.append(obj)

  47.     def setID(self, obj):
  48.         if isinstance(obj, Tag):
  49.             id = obj.id
  50.             n = len([t for t in self if isinstance(t, Tag) and t.id.startswith(id)])
  51.         else:
  52.             id = 'content'
  53.             n = len([t for t in self if not isinstance(t, Tag)])
  54.         if n: id = '%s_%03i' % (id, n)
  55.         if isinstance(obj, Tag): obj.id = id
  56.         return id

  57.     def __add__(self, obj):
  58.         if self.tagname: return Tag(self, obj)
  59.         self.addObj(obj)
  60.         return self

  61.     def __lshift__(self, obj):
  62.         self += obj
  63.         if isinstance(obj, Tag): return obj

  64.     def render(self):
  65.         result = ''
  66.         if self.tagname:
  67.             result = '<%s%s%s>' % (self.tagname, self.renderAtt(), self.selfClose()*' /')
  68.         if not self.selfClose():
  69.             for c in self:
  70.                 if isinstance(c, Tag):
  71.                     result += c.render()
  72.                 else: result += c
  73.             if self.tagname:
  74.                 result += '' % self.tagname
  75.         result += '\n'
  76.         return result

  77.     def renderAtt(self):
  78.         result = ''
  79.         for n, v in self.attributes.iteritems():
  80.             if n != 'txt' and n != 'open':
  81.                 if n == 'cl': n = 'class'
  82.                 result += ' %s="%s"' % (n, v)
  83.         return result

  84.     def selfClose(self):
  85.         return self.tagname in selfClose
  86.     
  87. def TagFactory(name):
  88.     class f(Tag):
  89.         tagname = name
  90.     f.__name__ = name
  91.     return f

  92. thisModule = modules[__name__]

  93. for t in tags: setattr(thisModule, t, TagFactory(t))

  94. def ValidW3C():
  95.     out = a(img(src='', alt='Valid XHTML 1.0 Strict'), href='')
  96.     return out

  97. class PyH(Tag):
  98.     tagname = 'html'
  99.     
  100.     def __init__(self, name='MyPyHPage'):
  101.         self += head()
  102.         self += body()
  103.         self.attributes = dict(xmlns='', lang='en')
  104.         self.head += title(name)

  105.     def __iadd__(self, obj):
  106.         if isinstance(obj, head) or isinstance(obj, body): self.addObj(obj)
  107.         elif isinstance(obj, meta) or isinstance(obj, link): self.head += obj
  108.         else:
  109.             self.body += obj
  110.             id=self.setID(obj)
  111.             setattr(self, id, obj)
  112.         return self

  113.     def addJS(self, *arg):
  114.         for f in arg: self.head += script(type='text/javascript', src=f)

  115.     def addCSS(self, *arg):
  116.         for f in arg: self.head += link(rel='stylesheet', type='text/css', href=f)
  117.     
  118.     def printOut(self,file=''):
  119.         if file: f = open(file, 'w')
  120.         else: f = stdout
  121.         f.write(doctype)
  122.         f.write(self.render())
  123.         f.flush()
  124.         if file: f.close()


阅读(9593) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~