4 listToHTML: convert The List from its normal format to HTML for easier (maybe) reading.
6 usage: listToHTML theList.txt theList.html
9 submitted by Mac-arena the Bored Zo.
10 part of Adium by Adam Iser.
11 /*-------------------------------------------------------------------------------------------------------*\
12 | Adium, Copyright (C) 2001-2004, Adam Iser (adamiser@mac.com | http://www.adiumx.com) |
13 \---------------------------------------------------------------------------------------------------------/
14 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU
15 | General Public License as published by the Free Software Foundation; either version 2 of the License,
16 | or (at your option) any later version.
18 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
19 | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 | Public License for more details.
22 | You should have received a copy of the GNU General Public License along with this program; if not,
23 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \------------------------------------------------------------------------------------------------------ */
28 '+': "change or addition",
35 '+': "#008800", #change or addition
36 '-': "#8800FF", #removal
37 '*': "#0000FF", #bug-fix
38 '#': "#FF0000", #debatable
43 def genfallbacks(fallbacks
):
45 prepare to generate colours that aren't in the default dictionary, in case the legend changes.
46 'fallbacks' must have an append method.
50 red
, green
, blue
= (0,0,0)
52 defaults
= task_colours
.values()
54 while red
<= 0xFF and green
<= 0xFF and blue
<= 0xFF:
55 color
= format
% (red
, green
, blue
)
56 if color
not in defaults
:
57 fallbacks
.append(color
)
67 del increment
, red
, green
, blue
, format
, defaults
71 print >>sys
.stderr
, warning
74 "given a string, turn it into something usable as a CSS class name."
76 index
= instr
.find('(')
83 instr
= re
.sub("\s+", '_', instr
)
87 def HTMLescape(instr
):
88 "given a string, turn it into something usable inside an HTML file (by escaping '<>&' with entities)."
90 def HTMLentity(match
):
91 matchstr
= match
.group(0)
102 return re
.sub('[&<>]', HTMLentity
, instr
)
104 def listToHTML(infile
, outfile
):
105 "the converter. pass an iterable (infile) and a flob that exports the 'write' method (outfile)."
109 legendRE
= re
.compile("Legend", re
.IGNORECASE
)
111 catsepRE
= re
.compile('^\s*(?P<BULLET>.) (?P<NAME>.+)\s*$')
112 separatorRE
= re
.compile('^-+$')
113 subheadingRE
= re
.compile("^(?P<WHITESPACE>\s*)(?P<NAME>.+):\s*$")
114 taskRE
= re
.compile("^\s*(?P<BULLET>.) (?P<TASK>.+)$")
117 legendMode
= object()
118 headingMode
= object()
124 genfallbacks(fallbacks
)
128 legendFmt
= '<dt>%(bullet)s</dt>\n\t<dd class="%(CSS name)s">%(name)s</dd>\n'
129 taskStartFmt
= '\t<li class="%(bullet name)s">%(task)s'
130 ruleFmt
= '\t.%(bullet name)s { color: %(color)s }\n'
133 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'
135 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n'
136 "<title>The List™</title>\n"
137 '<style type="text/css">\n'
138 'body { color: black; background-color: white }\n')
144 if legendRE
.search(buf
):
146 elif separatorRE
.match(buf
):
154 outfile
.write("\t</li>\n")
161 if mode
is legendMode
:
163 #write out the legend.
167 outfile
.write(''.join(reservoir
))
168 outfile
.write('</dl>\n<hr>\n')
171 if mode
is legendMode
or mode
is taskMode
:
173 outfile
.write("\n<h1>")
175 elif mode
is headingMode
:
177 outfile
.write(''.join(reservoir
))
179 outfile
.write("</h1>\n")
182 if mode
is legendMode
:
183 match
= catsepRE
.match(buf
)
185 bullet
, name
= match
.groups()
186 if bullet
not in task_colours
:
187 task_colours
[bullet
] = fallbacks
.pop()
188 task_names
[bullet
] = CSSname
= CSSclass(name
)
189 #add the list pair for the legend section of the page.
190 reservoir
.append(legendFmt
% {
193 'name': HTMLescape(name
),
194 'color': task_colours
[bullet
],
197 outfile
.write(ruleFmt
% {
198 'bullet name': CSSname
,
199 'color': task_colours
[bullet
],
202 elif mode
is headingMode
:
203 reservoir
.append(buf
)
204 elif mode
is taskMode
:
206 match
= taskRE
.match(sbuf
)
209 outfile
.write("<ul>\n")
211 bullet
, taskStart
= match
.groups()
212 if task_names
.has_key(bullet
):
213 bullet_name
= task_names
[bullet
]
215 outfile
.write("\t</li>\n")
218 outfile
.write(taskStartFmt
% {
220 'task': HTMLescape(taskStart
)+'\n',
221 'bullet name': bullet_name
226 match
= subheadingRE
.match(buf
)
229 outfile
.write("\t</li>\n")
232 outfile
.write("</ul>\n")
234 ws
= match
.group("WHITESPACE").replace(' ', '\t')
236 outfile
.write("<h%u>" % h_number
)
237 outfile
.write(HTMLescape(match
.group("NAME")))
238 outfile
.write("</h%u>\n" % h_number
)
240 outfile
.write("<ul>\n")
245 outfile
.write("<ul>\n")
248 outfile
.write("\t<li>\n")
250 outfile
.write(HTMLescape(buf
))
252 print >> sys
.stderr
, 'Error while processing line %u: %r' % (lineno
, buf
)
255 outfile
.write("\t</li>\n")
257 outfile
.write("</ul>\n")
259 outfile
.write("</body>\n")
260 outfile
.write("</html>\n")
262 if __name__
== "__main__":
264 infile
= file(sys
.argv
[1], 'rU', 1)
265 outfile
= file(sys
.argv
[2], 'w', 1)
266 listToHTML(infile
, outfile
)