Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1817884
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-03-21 15:46:43

初始版本:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. python基础教程第2版 20 项目1:即时标记
  5. 主要使用正则
  6. 将一段文本转换成有格式的

  7. '''
  8. import sys,re
  9. #找出块
  10. #收集所有行,直到空行,然后返回已经收集的行.
  11. def lines(file):
  12.     for line in file: yield line
  13.     yield '\n'
  14. def blocks(file):
  15.     block=[]
  16.     for line in lines(file):
  17.         #判断是否是空行
  18.         if line.strip():
  19.             block.append(line)
  20.         elif block:
  21.             yield ''.join(block).strip()
  22.             block = []


  23. print(' 测试 ')
  24. title=True
  25. for block in blocks(sys.stdin):
  26.     block = re.sub(r'\*(.+?)\*',r'\1',block)
  27.     if title:
  28.         print('

    ')

  29.         print(block)
  30.         print('')
  31.         title=False
  32.     else:
  33.         print('

    ')

  34.         print(block)
  35.         print('


    '
    )
  36. print('')
进一步处理:
html.py(主要执行程序)

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. import sys,re
  6. from util import *
  7. from handler import *
  8. from rules import *

  9. class Parser:
  10.     def __init__(self,handler):
  11.         self.handler=handler
  12.         self.rules=[]
  13.     def addRule(self,rule):
  14.         self.rules.append(rule)
  15.     def parse(self,file):
  16.         self.handler.start('document')
  17.         for block in blocks(file): #应用规则
  18. # print(self.rules)
  19.             for rule in self.rules:
  20.                 if rule.condition(block):
  21.                     last=rule.action(block,self.handler)
  22.                     if last:
  23.                         break
  24.         self.handler.end('document')
  25. class BasicTextParser(Parser):
  26.     #处理文本需要渲染器
  27.     def __init__(self,handler):
  28.         #调用父类的初始化
  29.         Parser.__init__(self,handler)
  30.         #调用父类的方法
  31.         self.addRule(ListRule()) #添加文章格式规则
  32.         self.addRule(ListItemRule())
  33.         self.addRule(TitleRule())
  34.         self.addRule(HeadingRule())
  35.         self.addRule(ParagraphRule())

  36. handler=HTMLRenderer()
  37. parser=BasicTextParser(handler)
  38. #parser.parse(sys.stdin)
  39. parser.parse(sys.stdin)
分割模块:util.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. 这个模块主要实现把文本分成区块.
  5. '''
  6. def lines(file):
  7.     for line in file: #读取文件,生成每一行的迭代器
  8.         yield line
  9.         yield '\n'
  10. def blocks(file):
  11.     block=[]
  12.     for line in lines(file):#从迭代器中取出每一行
  13.         if line.strip(): #如果为空,退出;有内容,添加block列表中
  14.             block.append(line)
  15.         elif block: #如果上面有空行,再次判断,如果block列表中有内容,生成block列表块;没有内容说明是多行空行,忽略.
  16.             yield ''.join(block).strip()
  17.             block=[]
规则模块:rules.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. from util import *
  6. class Rule:
  7.     """
  8.     Base class for all rules.
  9.     """
  10.     def action(self, block, handler):
  11.         handler.start(self.type)
  12.         handler.feed(block)
  13.         handler.end(self.type)
  14.         return True

  15. class HeadingRule(Rule):
  16.     """
  17.     A heading is a single line that is at most 70 characters and
  18.     that doesn't end with a colon.
  19.     """
  20.     type = 'heading'
  21.     def condition(self, block):
  22.         return not '\n' in block and len(block) <= 70 and not block[-1] == ':'

  23. class TitleRule(HeadingRule):
  24.     """
  25.     The title is the first block in the document, provided that it is
  26.     a heading.
  27.     """
  28.     type = 'title'
  29.     first = True

  30.     def condition(self, block):
  31.         if not self.first: return False
  32.         self.first = False
  33.         return HeadingRule.condition(self, block)

  34. class ListItemRule(Rule):
  35.     """
  36.     A list item is a paragraph that begins with a hyphen. As part of
  37.     the formatting, the hyphen is removed.
  38.     """
  39.     type = 'listitem'
  40.     def condition(self, block):
  41.         return block[0] == '-'
  42.     def action(self, block, handler):
  43.         handler.start(self.type)
  44.         handler.feed(block[1:].strip())
  45.         handler.end(self.type)
  46.         return True

  47. class ListRule(ListItemRule):
  48.     """
  49.     A list begins between a block that is not a list item and a
  50.     subsequent list item. It ends after the last consecutive list
  51.     item.
  52.     """
  53.     type = 'list'
  54.     inside = False
  55.     def condition(self, block):
  56.         return True
  57.     def action(self, block, handler):
  58.         if not self.inside and ListItemRule.condition(self, block):
  59.             handler.start(self.type)
  60.             self.inside = True
  61.         elif self.inside and not ListItemRule.condition(self, block):
  62.             handler.end(self.type)
  63.             self.inside = False
  64.         return False

  65. class ParagraphRule(Rule):
  66.     """
  67.     A paragraph is simply a block that isn't covered by any of the
  68.     other rules.
  69.     """
  70.     type = 'paragraph'
  71.     def condition(self, block):
  72.         return True
渲染模块:handler.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''

  4. '''
  5. class Handler():
  6.     def callback(self,prefix,name,*args):
  7.         mothed=getattr(self,prefix+name,None)
  8.         if callable(mothed): #加一层判断,如果对象是可调用的才返回
  9.             return mothed(*args)
  10.     def start(self,name):
  11.         self.callback('start_',name)
  12.     def end(self,name):
  13.         self.callback('end_',name)

  14. class HTMLRenderer(Handler):
  15.     '''
  16.     用于生成HTML的具体处理程序
  17.     HTMLRender内的方法都可以通过超类处理程序的start(),end(),sub()方法来访问,它们实现了用于HTML文档的基本标签.

  18.     '''

  19.     def start_document(self):
  20.         print(' html ')
  21.     def end_document(self):
  22.         print('')

  23.     def start_paragraph(self):
  24.         print('

    ')

  25.     def end_paragraph(self):
  26.         print('


    '
    )

  27.     def start_heading(self):
  28.         print('

    ')

  29.     def end_heading(self):
  30.         print('')

  31.     def start_list(self):
  32.         print('
      ')
    •     def end_list(self):
    •         print('
    '
    )

  33.     def start_listitem(self):
  34.         print('
  35. ')
  36.     def end_listitem(self):
  37.         print('
  38. ')

  39.     def start_title(self):
  40.         print('

    ')

  41.     def end_title(self):
  42.         print('')

  43.     def feed(self,data):
  44.         print(data)

  45. '''
  46. getattr(object, name[, default])
  47. Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
  48. callable(object)
  49. Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a __call__() method.
  50. '''

  51. #下面是给XML渲染








test_input.txt

点击(此处)折叠或打开

  1. Required Entitlements and Repositories

  2. The packages provided in the following repositories are required to install and configure a functioning Red Hat Enterprise Virtualization environment. When one of these repositories is required to install a package, the steps required to enable the repository are provided in the appropriate location in the Installation Guide or *Self-Hosted Engine Guide*.

  3. Red Hat Enterprise Virtualization Manager

  4. Red Hat Enterprise Virtualization Hypervisor 6 hosts are supported in Red Hat Enterprise Virtualization 3.6 only in clusters with 3.5 compatibility. Clusters with 3.6 compatibility support Red Hat Enterprise Virtualization Hypervisor 7 hosts and Red Hat Enterprise Linux 7 hosts only.

  5. The following Red Hat Enterprise Virtualization features are not supported:
  6. - Hot-plug CPU
  7. - SPICE display
  8. - SmartCard
  9. - Sound device
  10. - Watchdog

  11. ntegration with OpenStack Networking (Neutron), *OpenStack* Image (Glance), and OpenStack Volume (Cinder)
执行测试:

点击(此处)折叠或打开

  1. [t@bjb0541 untitled]$ ./B20util.py < test_input.txt |tee > 2.html
  2. <html><head><title>测试</title><body>
  3. <h1>
  4. Required Entitlements and Repositories
  5. </h1>
  6. <p>
  7. The packages provided in the following repositories are required to install and configure a functioning Red Hat Enterprise Virtualization environment. When one of these repositories is required to install a package, the steps required to enable the repository are provided in the appropriate location in the Installation Guide or <em>Self-Hosted Engine Guide</em>.
  8. </p>
  9. <p>
  10. Red Hat Enterprise Virtualization Manager
  11. </p>
  12. <p>
  13. Red Hat Enterprise Virtualization Hypervisor 6 hosts are supported in Red Hat Enterprise Virtualization 3.6 only in clusters with 3.5 compatibility. Clusters with 3.6 compatibility support Red Hat Enterprise Virtualization Hypervisor 7 hosts and Red Hat Enterprise Linux 7 hosts only.
  14. </p>
  15. <p>
  16. The following Red Hat Enterprise Virtualization features are not supported:
  17.   - Hot-plug CPU
  18.   - SPICE display
  19.   - SmartCard
  20.   - Sound device
  21.   - Watchdog
  22. </p>
  23. <p>
  24. ntegration with OpenStack Networking (Neutron), <em>OpenStack</em> Image (Glance), and OpenStack Volume (Cinder)
  25. </p>
  26. </body></html>



进一步版本执行测试:
[t@bjb0541 makeuphtml]$ python html.py < test_input.txt  > 1.html





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