最近在ubuntu linux下混,可惜CppBlog下的FreeTextBox用firefox一打開就假死,而TextBox又不支持文本轉(zhuǎn)html(主要是沒有加換行),于是就寫了一個(gè)腳本.
在/usr/bin中新建一個(gè)快捷方式,名為txt2htm,然后在屬性中設(shè)為可執(zhí)行,就可以用了
用法如
txt2htm xxx.txt
為了方便起見,大家可以新建一個(gè)后綴為txt的文件,點(diǎn)右鍵,選打開方式,輸入命令txt2htm,然后確定,以后只要點(diǎn)點(diǎn)鼠標(biāo)選"以...打開"就可以完成工作了.
windows下當(dāng)然也可以使用這個(gè)腳本,添加到右鍵的方法是按著shift點(diǎn)右鍵,選打開方式.
--------------------------------------------------------------
#!/usr/local/bin/python
# -*-coding:UTF-8-*-
#txt2htm.py
#Author: 張沈鵬 zsp007@gmail.com
#Update: 2006-11-10 Beta0.2
import sys
import re
def htmlWrapper(content,tag,attr):
return "<"+tag+" "+attr+">"+content+"</"+tag+">"
def fontColorWrapper(content,color):
return htmlWrapper(content,'font','color="#'+color+'"')
def htmHighLight(line):
keywords=["
if","
then","
else","
def","
for","
in","
return","
import","
print","
unsigned","
long","
int",\
"
short","
include","
class","
void","
while","
const","
template"
]
for i
in keywords:
keywordMatcher=re.compile(r'\b'+i+r'\b')
line = keywordMatcher.sub(fontColorWrapper(i,'cf0000'), line)
return line
def txt2htm(txtName):
txt=open(txtName)
htmlName=filename+".html"
htm=open(htmlName,"w")
for line
in txt:
line=line\
.replace('&','&')\
.replace('<','<')\
.replace('® ','® ')\
.replace('"','"')\
.replace('©','©')\
.replace('™','™')\
.replace('<','<')\
.replace('\t'," ").\
replace(' ',' ')
line="<br/>"+htmHighLight(line)
print line
htm.write( line)
txt.close()
htm.close()
print "\n轉(zhuǎn)換成功,保存在"+htmlName+'\n'
if len(sys.argv) < 2:
print "\n請指定要轉(zhuǎn)換為htm的文件\n"
else:
filename=sys.argv[1]
txt2htm(filename)