2010年6月26日 星期六

[Question 7] Html_converter

Description :
Use Python to converter the code become html format.

Code :

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# html_converter version 1.0
import codecs
import html_1
import html_2
import html_3

text=[]
filename1 = "html.py"
filename2 = 'filename2'
filename3 = 'filename3'

# open file read file into array text
f = open(filename1, "r")
text = f.read()
# because chinese, so change type(text) to unicode
text_unicode=unicode(text,'utf-8')

# when meet # put ooxx
text_unicode=html_1.html(text_unicode)

# when meet '' put #FF34B3> ooxx
text_unicode=html_2.html(text_unicode)

# when meet import put #FF34B3> ooxx
text_unicode=html_3.html(text_unicode)

# use ''.join(text_step1) the word can combine together
# If just print text_step1 between word and word will have a little gap.
text_unicode = ''.join(text_unicode)

# transfer word to code use repr
s = repr(text_unicode)
f = codecs.open(filename3,'w+')
f.write(s)
f.close()

# use codecs.open to open file
# write code into (filename2 = memory_test2)
f = codecs.open(filename2,'w+','utf-8')
f.write(text_unicode)
f.close()


html_1 :

#! /usr/bin/env python
# when meet # put <font color=blue> ooxx
def html(n) :
i=0
j=0
array=[]
while i if n[i] in '#' :
j=1
array.append('<font color=blue>'+n[i])
i=i+1
else:
if n[i] in '\n' and j==1 :
j=0
array.append('</font>'+n[i])
i=i+1
else :
array.append(n[i])
i=i+1

return array


html_2 :

#! /usr/bin/env python
# when meet '' put <font color=#FF34B3> ooxx
def html(n) :
i=0
j=0
array=[]
while i if n[i] in '\'"' and j==0 :
j=1
array.append('<font color=#FF34B3>'+n[i])
i=i+1
elif n[i] in '\'"' and j==1 :
j=0
array.append(n[i]+'</font>')
i=i+1
else :
array.append(n[i])
i=i+1

return array


html_3 :

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# when meet import put <font color=#9B30FF> ooxx
def html(n) :
pattern='i m p o r t'
i=0
m = pattern.split()
array=[]

j=0
while i if (n[i:i+6] == m ) :
j=6
array.append('<font color=#EE00EE>'+n[i])
i=i+1
else :
j-=1
if j == 0 :
array.append(n[i]+'</font>')
i=i+1
else :
array.append(n[i])
i=i+1


return array


# http://caterpillar.onlyfun.net/Gossip/AlgorithmGossip/MatchString.htm
# 字串比對



Result :

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# html_converter version 1.0
import codecs
import html_1
import html_2
import html_3

text=[]
filename1 = "html.py"
filename2 = 'filename2'
filename3 = 'filename3'

# open file read file into array text
f = open(filename1, "r")
text = f.read()
# because chinese, so change type(text) to unicode
text_unicode=unicode(text,'utf-8')

# when meet # put <font color=blue> ooxx
text_unicode=html_1.html(text_unicode)

# when meet '' put <font color=#FF34B3> ooxx
text_unicode=html_2.html(text_unicode)

# when meet import put <font color=#FF34B3> ooxx
text_unicode=html_3.html(text_unicode)

# use ''.join(text_step1) the word can combine together
# If just print text_step1 between word and word will have a little gap.
text_unicode = ''.join(text_unicode)

# transfer word to code use repr
s = repr(text_unicode)
f = codecs.open(filename3,'w+')
f.write(s)
f.close()

# use codecs.open to open file
# write code into (filename2 = memory_test2)
f = codecs.open(filename2,'w+','utf-8')
f.write(text_unicode)
f.close()


Improve :
Using input() substitute filename1 = "html.py"

Related Posts:

0 意見:

張貼留言