美文网首页每天写1000字
将Markdown转换为微信排版

将Markdown转换为微信排版

作者: 刘思宁 | 来源:发表于2016-06-30 18:01 被阅读627次

在简书上用Markdown写东西,会慢慢形成一定的写作格式,比如我。另外,我还会把一些简书上写的东西更新到个人公众号上。久而久之公众号排版也形成了一定的格式。这两种格式可以对应上,所以每次排版的操作几乎是重复的。所以我自学了一点Python,方便我把Markdown很快的转换为微信文章的排版。

代码如下,希望也能帮你加快排版速度。另外,我只是个菜鸟,还求大神指点。

#open a file
fname = raw_input('Which article:')
print fname
try:
    fhand = open(fname)
except:
print "Can't find:",fname
fname = raw_input("Input the right name:")
#already opened, I got a handle

#tell where to write or build a new file
#actually, I can build a new one in the same document folder
dotposition = fname.find('.')
outname = fname[:dotposition] + 'out.txt'
print outname
fout = open(outname,'w')
linkname = fname[:dotposition] + 'link.txt'
fout_link = open(linkname,'w')

#define how to exchange markdown label, I shall save them in a txt docment someday
p_style = '<p style="margin: 20px 30px; max-width: 100%; min-height: 1em; white-space: pre-wrap; color: rgb(137, 137, 137); text-align: justify; line-height: 1.5; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 255, 255);"><span style="max-width: 100%; line-height: 30px; box-sizing: border-box !important; word-wrap: break-word !important;">'
p_style_end = '</span></p>'

img_caption = '<p style="margin: 20px 30px; max-width: 100%; min-height: 1em; white-space: pre-wrap; color: rgb(182,228,253); text-align: center; font-size : 12px; line-height: 1.5; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 255, 255);"><span style="max-width: 100%; line-height: 30px; box-sizing: border-box !important; word-wrap: break-word !important;">'
img_caption_end = '</span></p>'

h2_style = '<p style="max-width: 100%; min-height: 1em; white-space: pre-wrap; color: rgb(62, 62, 62); line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 255, 255);"><span style="max-width: 100%; color: #898989; line-height: 24px; text-align: justify; box-sizing: border-box !important; word-wrap: break-word !important;"> 1</span></p><h2 style="margin-top: 0px; margin-bottom: 15px; max-width: 100%; box-sizing: border-box; word-wrap: break-word; white-space: normal; outline: none; font-family: sans-serif; line-height: 5px; color: white; border-left-width: 20px; border-left-style: solid; border-left-color: rgb(85, 160, 249); background: rgb(182, 228, 253);">   </h2><p style="margin: 20px; max-width: 100%; min-height: 1em; white-space: pre-wrap; color: rgb(137, 137, 137); text-align: justify; line-height: 1.5; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 255, 255);"><strong style="max-width: 100%; color: rgb(62, 62, 62); line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="max-width: 100%; font-size: 20px; box-sizing: border-box !important; word-wrap: break-word !important;">'
h2_style_end = '</span></strong><br style="max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important;"/></p>'

end_mark = '<br><p style="white-space: normal; text-align: center;"><span style="color: #B6E4FD; font-size: 14px; line-height: 22px;"><strong>< In the end ></strong></span></p><p style="white-space: normal;"><br></p>'

strong_label = '<strong>'
strong_label_end = '</strong>'

quote_label = '<blockquote style="line-height: 25.6px; white-space: normal;">'
quote_label_end = '</blockquote>'

list_label = '<ol list-paddingleft-2" style="width: 515.844px; max-width: 100%; color: rgb(62, 62, 62); line-height: 25.6px; box-sizing: border-box !important; word-wrap: break-word !important; background-color: rgb(255, 255, 255);">'
list_label_end = '</ol>'

list_content = '<li><p style="margin: 5px; max-width: 100%; min-height: 1em; color: rgb(137, 137, 137); text-align: justify; line-height: 1.5; box-sizing: border-box !important; word-wrap: break-word !important;"><span style="max-width: 100%; line-height: 30px; color: rgb(61, 167, 66); box-sizing: border-box !important; word-wrap: break-word !important;">'
list_content_end = '</span></p></li>'

test_style = ' test style %s'

#judge the type
linknum = 0 # it will be used in counting link
for line in fhand:
if line.startswith('##'): #find h2
    if line.startswith('###'): #find h3(the end)
        h3content = line[3:]
        endcontent = end_mark + p_style + h3content + p_style_end + '\n'
        fout.write(endcontent)
    else:
        h2content = line[2:]
        myh2 = h2_style + h2content + h2_style_end + '\n'
        fout.write(myh2)
elif line.startswith('!['): #find image
    #change the line's content
    imgaddress_postion = line.find('(')
    imgaddress = line[imgaddress_postion + 1:len(line) - 1]
    imgword_position = line.find('[')
    imgword = line[imgword_position + 1 : imgaddress_postion - 1]
    imgexpress = img_caption + '<img src=' + imgaddress + '><br>' + imgword + img_caption_end +'\n'
    fout.write(imgexpress)
else: #normal sentence may include link, list, strong, and italic (timely ignored)
    #4 if sentence, if certain label is found, handle it and assign the new string to a new verialbe which will be used in next sentence
    
    line_strong = ''
    line_strong = line
    if line_strong.find('**') != -1: #handle ** at first  #I'd better make it a circle, so that more strong marks can be handled.
        while line_strong.find('**') != -1:
            strong_position1 = line.find('**')
            strong_position2 = line.find('**',strong_position1 + 1)
            line_strong = line[:strong_position1] + strong_label + line[strong_position1 + 2 : strong_position2] + strong_label_end + line[strong_position2 + 2 :]
            print ' in the first if'
            
    line_link = line_strong 
    if line_link.find('](') != -1: #handle link secondly
        while line_link.find('](') != -1:
            linknum = linknum + 1
            linknumstr = str(linknum)
            link_position2 = line_link.find('](')
            link_position1 = line_link.find('[')
            link_position3 = line_link.find(')', link_position1)
            line_linkword = line_link[link_position1 + 1:link_position2]
            line_link = line_link[:link_position1] + line_linkword + '<sup>'+ linknumstr + '</sup>' + line_link[link_position3 + 1:] #warning: position3 + 1 could be 0, and then the whole line will be printed
            line_linklink = line_strong[link_position2 + 1 : link_position3]
            fout_link.write(line_linklink)
            print ' in the second if'   
    
    if line_link.find('1. ') != -1 or line_link.find('2. ') != -1 or line_link.find('3. ') != -1 or line_link.find('4. ') != -1 or line_link.find('5. ') != -1: #usually, i won't make more than 5 items
        line_list = quote_label + list_label + '<li>' + list_content + line_link + list_content_end + '</li>' + list_label_end + quote_label_end
        line_final = line_list
    else:
        line_final = p_style + line_link + p_style_end
    
    fout.write(line_final)

相关文章

网友评论

    本文标题:将Markdown转换为微信排版

    本文链接:https://www.haomeiwen.com/subject/llhfjttx.html