欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python : 书签 转换为 HTML

程序员文章站 2022-07-13 23:30:26
...
bm2htm.py mybatis.txt
# -*- coding: cp936 -*-
# 先将 bookmark 转码: unicode -> gbk
import os, sys
import re

if len(sys.argv) ==2:
    f1 = sys.argv[1]
else:
    print 'usage: bm2htm.py file1.txt '
    sys.exit(4)

if not os.path.exists(f1):
    print 'ERROR: %s not found\n' % f1
    sys.exit(4)

fn,ext = os.path.splitext(f1)
if ext !='.txt':
    print 'ERROR: %s ext is not .txt\n' % f1
    sys.exit(4)

fp = open(f1,'r')
f2 = fn +'.htm'
fp2 = open(f2,'w')
fp2.write('<html>\n')
fp2.write('  <head>\n')
fp2.write('   <base target="top"/>\n')
fp2.write('    <meta http-equiv="Content-Type" content="text/html; charset=GBK">\n')
fp2.write('    <title>深入浅出 MyBatis技术原理与实战 </title>\n')
fp2.write('  </head>\n')
fp2.write('<body>\n')
# set 参数
path ='mybatis'
pre ='m'  # prefix
pat = re.compile("^[1-9A-Z]")
end = 272 # page max value

for line in fp:
    t = line.strip().replace(' ',' ').split()  # A1A1
    if line.strip() =='':
        fp2.write('\n')
        continue
    if re.match(pat,line):        
        if len(t)>=3:
            d = int(t[-1])
            aline = '  <li>%s  %s  <a href="get.php?dir=%s&pre=%s&end=%d&i=%d">%s</a></li>\n' % (t[0],t[1],path,pre,end,d,t[-1])
        else:
            aline = '  <li>%s  %s </li>\n' % (t[0],t[1])            
        fp2.write(aline)
    elif len(t)==2:
        d = int(t[-1])
        aline = '  <li>%s <a href="get.php?dir=%s&pre=%s&end=%d&i=%d">%s</a></li>\n' % (t[0],path,pre,end,d,t[1])
        fp2.write(aline)
    elif len(t)>=3:
        d = int(t[-1])
        aline = '</ul>\n<ul>%s  %s  <a href="get.php?dir=%s&pre=%s&end=%d&i=%d">%s</a>\n' % (t[0],t[1],path,pre,end,d,t[-1])
        fp2.write(aline)    
    else:
        aline = '</ul>\n<ul>'+line
        fp2.write(aline)
#
fp.close()
fp2.write('</ul>\n')
fp2.write('</body>\n')
fp2.write('</html>\n')
fp2.close()

相关标签: python