This is a simple python program that reads a file (code), and replaces line breaks and spaces with appropriate tag and entity.
1
2 import sys
3
4 line_break='<br>'
5 nb_space=' '
6
7 def toHtml(f,out):
8 try:
9 fo=open(out,'w')
10 except IOError:
11 print 'output file error!'
12 else:
13 for line in f:
14 line=line.replace('\t',nb_space*8)
15 line=line.replace(' ',nb_space)
16 line=line.replace('\n',line_break)
17 fo.write(line)
18 fo.close()
19
20
21 if len(sys.argv) == 1:
22 print("at least one argument / input filename / required")
23 sys.exit()
24 if len(sys.argv) > 3:
25 print"too many arguments"
26 sys.exit()
27 try:
28 f=open(sys.argv[1],'r')
29 except IOError:
30 print 'cannot open file ', sys.argv[1]
31 else:
32 if len(sys.argv) == 2:
33 out=f.name + '.html'
34 else:
35 out=sys.argv[2]
36 toHtml(f, out)
37 f.close()