博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 小练习之山寨版markdown格式txt文件转html文件
阅读量:4221 次
发布时间:2019-05-26

本文共 4595 字,大约阅读时间需要 15 分钟。

本练习根据《python基础教程》课后练习一——《即时标记》改编

分为四个模块(不包括输出部分):文本解析、规则制定、过滤、处理程序,顺序如下:
这里写图片描述

规则制定

这里写图片描述

Rule模块由action和condition两部分实现,就是在遍历规则的时候通过调用condition这个东西来判断是否符合当前规则。

我们考虑了标题(H1Rule)、引用(GuideRule)、无序列表(ListItemRule&ListRule)、代码(CodeRule&CodeListRule)和普通段落(ParagraphRule),在处理程序(handlers)模块会有相应的实现。

过滤部分

采用正则表达式,对加粗、加斜和超链接做了相应的处理

self.addFilter(r'\*([^*].+?)\*','emphasis')        self.addFilter(r'\*{3}([^*].+?)\*{3}','bold')        self.addFilter(r'(http://[\.a-zA-Z1-9]+)','url')

中文处理

#coding=utf-8print data.decode('utf-8').encode('gbk')

这里写图片描述

请忽略我丑丑的布局T^T
这里写图片描述
具体代码如下:

handlers.py

class Handler:    def callback(self,prefix,name,*args):        method = getattr(self,prefix+name,None)        if callable(method):            return method(*args)    def start(self,name):        self.callback('start_',name)    def end(self,name):        self.callback('end_',name)    def sub(self,name):        def subsitution(match):            result = self.callback('sub_',name,match)            if result is None:                match.group(0)            return result        return subsitutionclass HTMLRenderer(Handler):    def start_document(self):        print '...'    def end_document(self):        print ''    def start_paragraph(self):        print '

' def end_paragraph(self): print '

' def start_guide(self): print '

' def end_guide(self): print '

' def start_code(self): print '
    ' def end_code(self): print '
' def start_h1(self): print '

' def end_h1(self): print '

' def start_h2(self): print '

' def end_h2(self): print '

' def start_h3(self): print '

' def end_h3(self): print '

' def start_list(self): print '
    ' def end_list(self): print '
' def start_listitem(self): print '
  • ' def end_listitem(self): print '
  • ' def start_title(self): print '

    ' def end_title(self): print '

    ' def sub_bold(self,match): return '%s' %match.group(1) def sub_emphasis(self,match): return '%s' % match.group(1) def sub_boldandemphasis(self,match): return '%s' % match.group(1) def sub_url(self,match): return '%s' %(match.group(1),match.group(1)) def feed(self,data): print data.decode('utf-8').encode('gbk')

    rules.py

    class Rule:    flag=True    def action(self,block,handler):        handler.start(self.type)        handler.feed(block)        handler.end(self.type)        return Trueclass H1Rule(Rule):    def condition(self,block):        return block[-1]=='#' and block[0]=='#'    def action(self,block,handler):        count=0;i=0;j=-1        while block[j]=='#' and block[i]=='#':            i=i+1;j=j-1;count=count+1        type='h'+str(count)        handler.start(type)        handler.feed(block[i:j+1].strip())        handler.end(type)        return Trueclass GuideRule(Rule):    type='guide'    def condition(self,block):        return block[0]=='>'    def action(self,block,handler):        handler.start(self.type)        handler.feed(block[1:].strip())        handler.end(self.type)        return Trueclass ListItemRule(Rule):    type='listitem'    def condition(self,block):        return block[0]=='-'    def action(self,block,handler):        handler.start(self.type)        handler.feed(block[1:].strip())        handler.end(self.type)        return Trueclass ListRule(ListItemRule):    type='list'    inside=False    def condition(self,block):        return True    def action(self,block,handler):        if not self.inside and ListItemRule.condition(self,block):            handler.start(self.type)            self.inside=True        elif self.inside and not ListItemRule.condition(self,block):            handler.end(self.type)            self.inside=False        return Falseclass CodeRule(Rule):    type='code'    def condition(self,block):        return block=='```'    def action(self,block,handler):        if self.flag==True:            handler.start(self.type)            self.flag=False            print self.flag        else:            handler.end(self.type)            self.flag=True        return Trueclass CodeListRule(CodeRule):    type='list'    def condition(self,block):        return True    def action(self,block,handler):        if self.flag==False and block!='```':            handler.start(self.type)            handler.feed(block)            handler.end(self.type)            return True        else:            return Falseclass ParagraphRule(Rule):    type='paragraph'    def condition(self,block):        return True

    执行

    $python makeup.py

    你可能感兴趣的文章
    test-definitions/blob/master/auto-test/boost/boost.sh
    查看>>
    Java多态性理解
    查看>>
    Intellij Idea 工具在java文件中怎么避免 import .*包,以及import包顺序的问题
    查看>>
    IDEA Properties中文unicode转码问题
    查看>>
    Oracle中Blob转换成Clob
    查看>>
    Linux如何查看so中函数名
    查看>>
    自动管理代码的android.mk
    查看>>
    cocos2dx 2.2.6编译记录(1)
    查看>>
    makefile学习网站
    查看>>
    C 编写lua模块(1)
    查看>>
    Lua教程:Lua调用C/C++函数(4)
    查看>>
    win下创建win32控制台工程,执行lua脚本
    查看>>
    cocos2dx android启动错误
    查看>>
    eclipse: android rename package name
    查看>>
    cocos2dx c++调用java思想
    查看>>
    cocos2dx lua Node节点 私有数据存取
    查看>>
    lua math.ceil math.ceil
    查看>>
    cocos2dx CCNode计算node的大小
    查看>>
    cocos2dx 布局记录(1)
    查看>>
    lua 多行注释和取消多行注释
    查看>>