python代码:
import re
class translate:
def __init__(self,mdFile,html):
self.block=[]
self.html=html+'.html'
self.mdFile=mdFile+'.md'
def readmd(self):
with open(self.mdFile,'r') as f:
for line in f.readlines():
line=line.strip('\n')
yield line
yield 'end'
def blocks(self):
self.temp=self.readmd()
while True:
self.oneLine=next(self.temp)
if self.oneLine is 'end':
break
self.block.append(self.oneLine)
print("blocks are finished")
def examine(self):
self.blocks()
self.InitialHtml()
while len(self.block):
self.scan_xss()
print(str(len(self.block))+" lines to finish")
if self.examineCutLine():
continue
elif self.examineHead():
continue
elif self.examineParagraph():
continue
elif self.examineCode_block():
continue
elif self.examineCode_line():
continue
elif self.examineBlock_quote():
continue
elif self.examineList_ul():
continue
elif self.examineImg():
continue
elif self.examineSuperlink():
continue
else:
self.write(self.block[0])
self.EndHtml()
print("translate finish")
def scan_xss(self):
self.block[0]=self.block[0].replace("","")
def InitialHtml(self):
self.head= '''
'''+self.html[:-5]+'''
'''
with open(self.html,'a') as f:
f.write(self.head)
def EndHtml(self):
self.final= '''
'''
with open(self.html,'a') as f:
f.write(self.final)
def examineHead(self):
if len(self.block[0])<1:
return False
self.i=0
while self.block[0][self.i] is '#':
if self.block[0][self.i+1] is '#':
self.i+=1
else:
self.h(self.i)
return True
return False
def examineParagraph(self):
if len(self.block[0])<2:
return False
if self.block[0][0] == '~' and self.block[0][1] == '~' and self.block[0][-1] == '~'and self.block[0][-2] == '~' and len(self.block[0])>4:
self.p(3)
return True
if self.block[0][0] == '*' and self.block[0][-1] == '*' and len(self.block[0])>2:
if self.block[0][1] == '*' and self.block[0][-2] == '*' and len(self.block[0])>4:
if self.block[0][2] == '*' and self.block[0][-3] == '*' and len(self.block[0])>6:
self.p(2)
else:
self.p(1)
else:
self.p(0)
return True
return False
def examineCutLine(self):
if len(self.block[0])<3:
return False
self.style1=0
self.style2=0
for i in range(3):
if self.block[0][i]=='-':
self.style1+=1
else:
break
if self.style1 == 3 and len(self.block[0])==3:
self.cutLine(0)
return True
elif self.style1 == 3 and len(self.block[0])==4:
self.cutLine(1)
return True
for i in range(3):
if self.block[0][i]=='*':
self.style2+=1
else:
break
if self.style2 == 3 and len(self.block[0])==3:
self.cutLine(2)
return True
elif self.style2 == 3 and len(self.block[0])==4:
self.cutLine(3)
return True
return False
def examineCode_line(self):
if len(self.block[0])<2:
return False
if self.block[0][0] is '`' and self.block[0][-1] is '`' and len(self.block[0])>2:
self.Code_line()
return True
return False
def examineCode_block(self):
if len(self.block[0])<3:
return False
if len(self.block[0])==3 and self.block[0]=="```" :
self.Code_block()
return True
return False
def examineBlock_quote(self):
if len(self.block[0])<1:
return False
self.num=0
if self.block[0][0] is '>':
while self.block[self.num][0]=='>':
self.num+=1
if len(self.block)==self.num:
break
self.Block_quote(self.num)
return True
return False
def examineList_ul(self):
if len(self.block[0])<2:
return False
self.i=0
while (self.block[self.i][0] is '*' or self.block[self.i][0] is '+' or self.block[self.i][0] is '-') and self.block[self.i][1] is " ":
self.i+=1
if len(self.block)==self.i:
break
if self.i>0:
self.List_ul(self.i)
return True
return False
def examineImg(self):
if re.match(r'!\[.+\]([\w\d\W]+)',self.block[0]) is None:
return False
self.Pic=re.sub(r'!\[.+\]([\w\d\W]+)',self.subFunc,self.block[0])
if self.Pic is not None:
self.Img(self.Pic.split(',,'))
return True
return False
def examineSuperlink(self):
if re.match(r'\[.+\]([\w\d\W]+)',self.block[0]) is None:
return False
self.Link=re.sub(r'!{0}\[.+\]([\w\d\W]+)',self.subFunc,self.block[0])
if self.Link is not None:
self.Super_link(self.Link.split(',,'))
return True
return False
def subFunc(self,match):
self.cut=re.split(r'\[|\]|\(|\)|[\s+]|!',match.group(0))
while '' in self.cut:
self.cut.remove('')
self.allstr=''
for i in self.cut:
self.allstr+=i
self.allstr+=',,'
return self.allstr[:-2]
def h(self,num):
self.words=''+self.block[0][num+1:]+' '
self.write(self.words)
def p(self,num):
if num is 1:
self.words=""+self.block[0][2:-2]+"
"
self.write(self.words)
elif num is 0:
self.words=""+self.block[0][1:-1]+"
"
self.write(self.words)
elif num is 2:
self.words=""+self.block[0][3:-3]+"
"
self.write(self.words)
elif num is 3:
self.words=""+self.block[0][1:-1]+"
"
self.write(self.words)
def cutLine(self,num):
self.words="
"
self.write(self.words)
def Code_line(self):
self.words=""+self.block[0][1:-1]+"
"
self.write(self.words)
def Code_block(self):
self.write("")
while len(self.block)>0 and self.block[0] != "```":
self.scan_xss()
self.words=self.block[0]+"
"
self.write(self.words)
if self.block[0]=="```":
self.write("
")
def Block_quote(self,num):
with open(self.html,'a') as f:
f.write("")
for i in range(num):
self.write(self.block[0][1:]+"
")
with open(self.html,'a') as f:
f.write("
")
def List_ul(self,num):
with open(self.html,'a') as f:
f.write("")
for i in range(num):
self.write("- "+self.block[0][2:]+"
")
with open(self.html,'a') as f:
f.write("
")
def Img(self,Pic):
if len(Pic)==2:
self.str=""
self.write(self.str)
elif len(Pic)==3:
self.str=""
self.write(self.str)
def Super_link(self,url):
if len(url)==2:
self.str=""+url[0]+""
self.write(self.str)
elif len(url)==3:
self.str=""+url[0]+""
self.write(self.str)
def write(self,words):
with open(self.html,'a') as f:
f.write(words)
self.block.pop(0)
def main():
txt=input("输入需要转译的md文件名:")
html=input("输入新建的html文件名:")
test=translate(txt,html)
test.examine()
if __name__=='__main__':
main()
css代码:
@charset "utf-8";
html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
body{
color:#444;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
font-size:13px;
line-height:1.5em;
padding:1em;
margin:auto;
max-width:42em;
background:#fefefe;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
}
h1 {
color: #000000;
font-size: 28px;
}
h2 {
color: #000000;
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777777;
background-color: inherit;
font-size: 14px;
}
hr {
height: 0.2em;
border: 0;
color: #CCCCCC;
background-color: #CCCCCC;
}
p, blockquote, ul, ol, dl, li, table, pre {
margin: 15px 0;
}
p{
margin:1em 0;
}
pre {
background-color: #F8F8F8;
border: 1px solid #CCCCCC;
border-radius: 3px;
overflow: auto;
padding: 5px;
}
pre code {
background-color: #F8F8F8;
border: none;
padding: 0;
}
code {
font-family: Consolas, Monaco, Andale Mono, monospace;
background-color:#F8F8F8;
border: 1px solid #CCCCCC;
border-radius: 3px;
padding: 0 0.2em;
line-height: 1;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
}
a{ color: #0645ad; text-decoration:none;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
::-moz-selection{background:rgba(255,255,0,0.3);color:#000}
::selection{background:rgba(255,255,0,0.3);color:#000}
a::-moz-selection{background:rgba(255,255,0,0.3);color:#0645ad}
a::selection{background:rgba(255,255,0,0.3);color:#0645ad}
blockquote{
color:#666666;
margin:0;
padding-left: 3em;
border-left: 0.5em #EEE solid;
}
ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }
img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; max-width:100%;}
table { border-collapse: collapse; border-spacing: 0; }
td { vertical-align: top; }
@media only screen and (min-width: 480px) {
body{font-size:14px;}
}
@media only screen and (min-width: 768px) {
body{font-size:16px;}
}