-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.py
executable file
·215 lines (157 loc) · 4.04 KB
/
html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python
'''
Space Telescope Science Institute
Synopsis:
This is a small set of routines that creates html files
Command line usage (if any):
usage: html.py will run a basic test of the routines
Description:
Each routine returns a string. As one builds up an html page
one normally, adds the string that is returnted to a pre-existing
string. When finished one writes the accumulated string to a file
Primary routines:
Notes:
History:
110505 ksl Coding begun
'''
import sys
def begin(title='Title'):
'''
Start an html page
'''
string='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="content-type"
content="text/html; charset=ISO-8859-1">
<title>%s</title>
</head>
<body>
<h1>%s</h1>
''' % (title,title)
return string
def end():
'''
Finish and html page
'''
string='''\n</body> \n</html>'''
return string
def paragraph(content='This is a paragraph. Who knows whether to continue'):
'''
Add a paragraph to the page
'''
string = '\n<p> \n %s \n</p>\n' % content
return string
def link(text='here',href='foo.txt'):
'''
Make a link to the page
Note: generally speaking one needs to put this in a string for a paragraph
'''
string='''
<a
href="%s">%s
</a>''' % (href,text)
return string
def image(image='test.png',alt='Thumbnails',width=400,height=400):
'''
Add a centered image to a page
'''
string='''\n<div style="text-align: center;">'''
string=string+'''\n <img alt="%s" src="%s" width="%d" height="%d"> \n''' % (alt,image,width,height)
string=string+'''</div>\n'''
return string
def table(lines):
'''
Format a table in html. Lines should be a set of records, with each record containing the
values for one row.
'''
string='''\n<table border="1" cellpadding="2" cellspacing="2" width="100%">\n'''
for line in lines:
row='<tr>\n'
for word in line:
row=row+' <td> %s </td>\n' % word
row=row+'<tr>\n'
string=string+row
trailer='''
</tbody>
</table>
'''
string=string+trailer
return string
def hline(size=3,width=100):
'''
Draw a horizontal line with thickness given by size and horizontal width given
by width in percent
'''
string='''\n<hr size="%d" width="%d''' % (int(size),int(width))
string=string+'%">\n' # Note that this funny construction is needed to handle the % symbol
return string
def h1(line):
'''
Add a header line
'''
string='''<h1>%s</h1>''' % line
return string
def h2(line):
'''
Add a header line
'''
string='''<h2>%s</h2>''' % line
return string
def h3(line):
'''
Add a header line
'''
string='''<h3>%s</h3>''' % line
return string
def add_list(lines):
'''
Add a simple list
Note - This routine is named add_list rather than list
to avoid confusion with python lists
'''
string='\n<ul>'
for line in lines:
line=line.strip()
string=string+'\n <li>%s</li>' % line
string=string+'\n</ul>'
return string
def preformat(lines):
'''
Add a set of preformated lines, that is text.
'''
string='<pre>'
for line in lines:
line=line.replace('\n','')
string=string+line+'<br>'
string=string+'</pre>'
return string
def test(filename='test.html'):
'''
Test the various html subroutines
'''
string=begin('Hi Knox')
string=string+paragraph('Tell it all, Knox')
link_string=link('here','ds9.py')
mypar='This file can be found %s' % link_string
string=string+paragraph(mypar)
line=[['test1',4],['test2',4]]
string=string+table(line)
string=string+hline()
string=string+image('file:///data/Projects/Persistence/Test/D/Visit19/Persist/Figs/ib6v19cpq_subtract.png')
string=string+hline()
line=['Hi Knox','Goodbye Knox']
string=string+add_list(line)
lines=['this is test of preformating\n','hello','world','tomorrow is another day']
string=string+preformat(lines)
string=string+end()
g=open(filename,'w')
g.write(string)
# Next lines permit one to run the routine from the command line
if __name__ == "__main__":
import sys
if len(sys.argv)>1:
# doit(int(sys.argv[1]))
doit(sys.argv[1])
else:
print 'usage: html.py test.html'