Created
July 10, 2012 02:06
-
-
Save luztak/3080527 to your computer and use it in GitHub Desktop.
Markdown parsing library in pure Python(md2html).
纯Python编写的Markdown解析库.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from re import compile as rc | |
#Inline Link | |
#行内链接 | |
def inline_link(text): | |
il=rc('\[(.*?)\]\((.*?)\)') | |
ret=il.findall(text) | |
for x in ret: | |
text=text.replace('[' x[0]+']('+x[1]+')','<a href="'+x[1]+'">'+x[0]+'</a>') | |
return text | |
#Heading | |
#标题 | |
def head(text): | |
h_se=rc('\n(.*?)\n([=]{1,}|[-]{1,})\n') | |
h_atx=rc('\n([#]{1,6})(.*?)([#]{0,}|)\n') | |
for x in h_se.findall(text): | |
text=text.replace(text[h_se.search(text).start():h_se.search(text).end()],'<h1>'+x[0]+'</h1>\n') | |
for x in h_atx.findall(text): | |
text=text.replace(text[h_atx.search(text).start():h_atx.search(text).end()],'\n<h'+str(len(x[0]))+'>'+x[1]+'</h' str(len(x[0]))+'>\n') | |
return text | |
#Separate Line | |
#分割线(<hr />) | |
def sep_liner(text): | |
sl=rc('\n([*]{3,}|[-]{3,}|[_]{3,})\n') | |
for x in sl.findall(text): | |
text=text.replace(x,'\n<hr />\n') | |
return text | |
#<em> and <strong> -ed text | |
#着重(<em>,<strong>) | |
def stressed(text): | |
em=rc('(\*[^*] \*)') | |
strong==rc('(\*\*[^*] \*\*)') | |
for x in em.findall(text): | |
text=text.replace(x,'<em>'+x[1:-1]+'</em>') | |
for x in strong.findall(text): | |
text=text.replace(x,'<strong>'+x[1:-1]+'</strong>') | |
return text | |
#Inline Code | |
#行内代码 | |
def inline_code(text): | |
ic=rc('([`]{1,}.*?[`]{1,})') | |
for x in ic.findall(text): | |
text=text.replace(x,'<code>'+x[1:-1]+'</code>') | |
return text | |
#Image | |
#插入图像 | |
def img(text): | |
im=rc('!\[(.*?)\]\((.*?)\)') | |
ret=im.findall(text) | |
for x in ret: | |
text=text.replace('![' x[0]+']('+x[1]+')','<img src="'+x[1]+'" alt="' x[0] '" />') | |
return text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment