zoukankan      html  css  js  c++  java
  • 将文字转成网页

    import sys, re
    from handlers import HTMLRenderer
    from util import blocks
    from rules import rule_list
    
    #将文本转成网页
    class Parser:
        
        def __init__(self, handler):
            self.handler = handler
            self.rules = []     
            self.filters = []   
    
        def addRule(self, rule):
           
            self.rules.append(rule)
    
        def addFilter(self, pattern, name):
           
            def filter(block, handler):
              
                return re.sub(pattern, handler.sub(name), block)
            self.filters.append(filter)
    
        def parse(self, file):
           
            self.handler.start('document')
          
            for block in blocks(file):
             
                for filter in self.filters:
                    block = filter(block, self.handler)
               
                for rule in self.rules:
                  
                    if rule.condition(block):
                        last = rule.action(block, self.handler)
                      
                        if last: 
                            break
          
            self.handler.end('document')
    
    
    class BasicTextParser(Parser):
      
    
        def __init__(self, handler):
            super().__init__(handler)
            for rule in rule_list:
                self.addRule(rule)
            self.addFilter(r'*(.+?)*', 'emphasis')
            self.addFilter(r'(http://[.a-zA-Z/]+)', 'url')
            self.addFilter(r'([.a-zA-Z]+@[.a-zA-Z]+[a-zA-Z]+)', 'mail')
    
    
    def main():
        handler = HTMLRenderer()
        parser = BasicTextParser(handler)
        parser.parse(sys.stdin)
    
    
    if __name__ == '__main__':
        main()
    
    
        $ cd ~/Code
    $ wget http://labfile.oss.aliyuncs.com/courses/70/python_markup.zip
    $ unzip python_markup.zip
    $ cp unzip python_markup/test.txt .
  • 相关阅读:
    熔断降级(Polly)
    网站被黑
    Redis 缓存穿透
    搭建私有Nuget仓库
    什么是配置
    css3的@media媒体查询
    css设置background图片的位置实现居中
    UTF-8有签名和无签名的区别
    SQL Server表结构和数据导入到MySQL
    Jquery实现滚动到底部加载更多(最原始)
  • 原文地址:https://www.cnblogs.com/jestin/p/12911546.html
Copyright © 2011-2022 走看看