加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mdX.py 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
朱程炀 提交于 2021-05-21 22:28 . upd
import re
def loadFile(fnm):
with open(fnm) as f:
return f.read()
def htmlTransfer(a):
a = a.replace(r"&", r"&")
a = a.replace(r" ", r" ")
a = a.replace(r"<", r"&lt;")
a = a.replace(r">", r"&gt;")
# a = a.replace(r"\"", r"&quot;")
a = a.replace(r"'", r"&apos;")
return a
def inline(s): # 行内样式转换
s = htmlTransfer(s)
s = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>",s)
s = re.sub(r"--(.+?)--", r"<del>\1</del>", s)
s = re.sub(r"_(.+?)_", r"<i>\1</i>", s)
s = re.sub(r"`(.+?)`", r"<code>\1</code>", s)
s = re.sub(r"\\(.)", r"\1", s)
return s.replace(r"\"", r"&quot;")
def markdown(s):
s = s.split("\n")
ret = []
for i in s:
tag = 'p'; iret = i
tmp = iret; iret = re.sub(r"^###### (.+)", r"\1", iret)
if iret != tmp: tag = 'h6'
tmp = iret; iret = re.sub(r"^##### (.+)", r"\1", iret)
if iret != tmp: tag = 'h5'
tmp = iret; iret = re.sub(r"^#### (.+)", r"\1", iret)
if iret != tmp: tag = 'h4'
tmp = iret; iret = re.sub(r"^### (.+)", r"\1", iret)
if iret != tmp: tag = 'h3'
tmp = iret; iret = re.sub(r"^## (.+)", r"\1", iret)
if iret != tmp: tag = 'h2'
tmp = iret; iret = re.sub(r"^# (.+)", r"\1", iret)
if iret != tmp: tag = 'h1'
ret.append("<%s>%s</%s>" % (tag, inline(iret), tag))
return "\r".join(ret)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化