最后
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
三、项目截图
纯文本文件:
Welcome to ShiYanLou
解析后生成的 HTML 页面如下图
四、项目讲解
1. 文本块生成器
首先我们需要有一个文本块生成器把纯文本分成一个一个的文本块,以便接下来对每一个文本快进行解析,util.py 代码如下:
#!/usr/bin/python
encoding: utf-8
def lines(file):
“”"
生成器,在文本最后加一空行
“”"
for line in file: yield line
yield ‘\n’
def blocks(file):
“”"
生成器,生成单独的文本块
“”"
block = []
for line in lines(file):
if line.strip():
block.append(line)
elif block:
yield ‘’.join(block).strip()
block = []
2. 处理程序
通过文本生成器我们得到了一个一个的文本块,然后需要有处理程序对不同的文本块加相应的 HTML 标记,handlers.py 代码如下:
#!/usr/bin/python
encoding: utf-8
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 substitution(match):
result = self.callback(‘sub_’, name, match)
if result is None: result = match.group(0)
return result
return substitution
class HTMLRenderer(Handler):
“”"
HTML 处理程序,给文本块加相应的 HTML 标记
“”"
def start_document(self):
print ‘
def end_document(self):
print ‘’
def start_paragraph(self):
print ‘
’
def end_paragraph(self):
print ‘
’def start_heading(self):
print ‘
’
def end_heading(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_emphasis(self, match):
return ‘%s’ % match.group(1)
def sub_url(self, match):
return ‘%s’ % (match.group(1), match.group(1))
def sub_mail(self, match):
return ‘%s’ % (match.group(1), match.group(1))
def feed(self, data):
print data
3. 规则
有了处理程序和文本块生成器,接下来就需要一定的规则来判断每个文本块交给处理程序将要加什么标记,rules.py 代码如下:
#!/usr/bin/python
encoding: utf-8
class Rule:
“”"
规则父类
“”"
def action(self, block, handler):
“”"
加标记
“”"
handler.start(self.type)
handler.feed(block)
handler.end(self.type)
return True
class HeadingRule(Rule):
“”"
一号标题规则
“”"
type = ‘heading’
def condition(self, block):
“”"
判断文本块是否符合规则
“”"
return not ‘\n’ in block and len(block) <= 70 and not block[-1] == ‘:’
class TitleRule(HeadingRule):
“”"
二号标题规则
“”"
type = ‘title’
first = True
def condition(self, block):
if not self.first: return False
self.first = False
return HeadingRule.condition(self, block);
class 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 True
class 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 False
class ParagraphRule(Rule):
“”"
段落规则
“”"
type = ‘paragraph’
def condition(self, block):
return True
4. 解析
现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。
分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!