3
from formatter import *
7
# The following defines the HTML header used by all generated pages.
10
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"
11
"http://www.w3.org/TR/html4/loose.dtd">
14
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
17
html_header_2= """ API Reference</title>
18
<style type="text/css">
19
body { font-family: Verdana, Geneva, Arial, Helvetica, serif;
21
background: #FFFFFF; }
23
p { text-align: justify; }
24
h1 { text-align: center; }
25
li { text-align: justify; }
27
a:link { color: #0000EF; }
28
a:visited { color: #51188E; }
29
a:hover { color: #FF0000; }
31
span.keyword { font-family: monospace;
36
pre.colored { color: blue; }
38
ul.empty { list-style-type: none; }
44
html_header_3=""" API Reference</h1></center>
49
# The HTML footer used by all generated pages.
55
# The header and footer used for each section.
57
section_title_header = "<center><h1>"
58
section_title_footer = "</h1></center>"
60
# The header and footer used for code segments.
62
code_header = '<pre class="colored">'
63
code_footer = '</pre>'
65
# Paragraph header and footer.
70
# Block header and footer.
72
block_header = '<table align=center width="75%"><tr><td>'
73
block_footer = '</td></tr></table><hr width="75%">'
75
# Description header/footer.
77
description_header = '<table align=center width="87%"><tr><td>'
78
description_footer = "</td></tr></table><br>"
80
# Marker header/inter/footer combination.
82
marker_header = '<table align=center width="87%" cellpadding=5><tr bgcolor="#EEEEFF"><td><em><b>'
83
marker_inter = "</b></em></td></tr><tr><td>"
84
marker_footer = "</td></tr></table>"
86
# Source code extracts header/footer.
88
source_header = '<table align=center width="87%"><tr bgcolor="#D6E8FF"><td><pre>\n'
89
source_footer = "\n</pre></table><br>"
91
# Chapter header/inter/footer.
93
chapter_header = '<br><table align=center width="75%"><tr><td><h2>'
94
chapter_inter = '</h2><ul class="empty"><li>'
95
chapter_footer = '</li></ul></td></tr></table>'
98
# source language keyword coloration/styling
100
keyword_prefix = '<span class="keyword">'
101
keyword_suffix = '</span>'
103
section_synopsis_header = '<h2>Synopsis</h2>'
104
section_synopsis_footer = ''
106
# Translate a single line of source to HTML. This will convert
107
# a "<" into "<.", ">" into ">.", etc.
109
def html_quote( line ):
110
result = string.replace( line, "&", "&" )
111
result = string.replace( result, "<", "<" )
112
result = string.replace( result, ">", ">" )
116
# same as 'html_quote', but ignores left and right brackets
118
def html_quote0( line ):
119
return string.replace( line, "&", "&" )
122
def dump_html_code( lines, prefix = "" ):
123
# clean the last empty lines
125
l = len( self.lines )
126
while l > 0 and string.strip( self.lines[l - 1] ) == "":
129
# The code footer should be directly appended to the last code
130
# line to avoid an additional blank line.
132
print prefix + code_header,
133
for line in self.lines[0 : l+1]:
134
print '\n' + prefix + html_quote(line),
135
print prefix + code_footer,
139
class HtmlFormatter(Formatter):
141
def __init__( self, processor, project_title, file_prefix ):
143
Formatter.__init__( self, processor )
145
global html_header_1, html_header_2, html_header_3, html_footer
148
file_prefix = file_prefix + "-"
152
self.project_title = project_title
153
self.file_prefix = file_prefix
154
self.html_header = html_header_1 + project_title + html_header_2 + \
155
project_title + html_header_3
157
self.html_footer = "<center><font size=""-2"">generated on " + \
158
time.asctime( time.localtime( time.time() ) ) + \
159
"</font></center>" + html_footer
163
def make_section_url( self, section ):
164
return self.file_prefix + section.name + ".html"
167
def make_block_url( self, block ):
168
return self.make_section_url( block.section ) + "#" + block.name
171
def make_html_words( self, words ):
172
""" convert a series of simple words into some HTML text """
175
line = html_quote( words[0] )
177
line = line + " " + html_quote( w )
182
def make_html_word( self, word ):
183
"""analyze a simple word to detect cross-references and styling"""
184
# look for cross-references
186
m = re_crossref.match( word )
191
block = self.identifiers[ name ]
192
url = self.make_block_url( block )
193
return '<a href="' + url + '">' + name + '</a>' + rest
195
return '?' + name + '?' + rest
197
# look for italics and bolds
198
m = re_italic.match( word )
201
return '<i>'+name+'</i>'
203
m = re_bold.match( word )
206
return '<b>'+name+'</b>'
208
return html_quote(word)
211
def make_html_para( self, words ):
212
""" convert a paragraph's words into tagged HTML text, handle xrefs """
215
line = self.make_html_word( words[0] )
216
for word in words[1:]:
217
line = line + " " + self.make_html_word( word )
219
return "<p>" + line + "</p>"
222
def make_html_code( self, lines ):
223
""" convert a code sequence to HTML """
224
line = code_header + '\n'
226
line = line + html_quote( l ) + '\n'
228
return line + code_footer
231
def make_html_items( self, items ):
232
""" convert a field's content into some valid HTML """
236
lines.append( self.make_html_code( item.lines ) )
238
lines.append( self.make_html_para( item.words ) )
240
return string.join( lines, '\n' )
243
def print_html_items( self, items ):
244
print self.make_html_items( items )
247
def print_html_field( self, field ):
249
print "<table><tr valign=top><td><b>"+field.name+"</b></td><td>"
251
print self.make_html_items( field.items )
254
print "</td></tr></table>"
257
def html_source_quote( self, line, block_name = None ):
260
m = re_source_crossref.match( line )
263
prefix = html_quote( m.group(1) )
264
length = len( m.group(0) )
266
if name == block_name:
267
# this is the current block name, if any
268
result = result + prefix + '<b>' + name + '</b>'
270
elif re_source_keywords.match(name):
271
# this is a C keyword
272
result = result + prefix + keyword_prefix + name + keyword_suffix
274
elif self.identifiers.has_key(name):
275
# this is a known identifier
276
block = self.identifiers[name]
277
result = result + prefix + '<a href="' + \
278
self.make_block_url(block) + '">' + name + '</a>'
280
result = result + html_quote(line[ : length ])
282
line = line[ length : ]
284
result = result + html_quote(line)
290
def print_html_field_list( self, fields ):
291
print "<table cellpadding=3>"
293
print "<tr valign=top><td><b>" + field.name + "</b></td><td>"
294
self.print_html_items( field.items )
299
def print_html_markup( self, markup ):
301
for field in markup.fields:
303
# we begin a new series of field or value definitions, we
304
# will record them in the 'table_fields' list before outputting
305
# all of them as a single table
307
table_fields.append( field )
311
self.print_html_field_list( table_fields )
314
self.print_html_items( field.items )
317
self.print_html_field_list( table_fields )
320
# Formatting the index
323
def index_enter( self ):
324
print self.html_header
325
self.index_items = {}
327
def index_name_enter( self, name ):
328
block = self.identifiers[ name ]
329
url = self.make_block_url( block )
330
self.index_items[ name ] = url
332
def index_exit( self ):
334
# block_index already contains the sorted list of index names
335
count = len( self.block_index )
336
rows = (count + self.columns - 1)/self.columns
338
print "<table align=center border=0 cellpadding=0 cellspacing=0>"
339
for r in range(rows):
341
for c in range(self.columns):
344
bname = self.block_index[ r + c*rows ]
345
url = self.index_items[ bname ]
346
line = line + '<td><a href="' + url + '">' + bname + '</a></td>'
348
line = line + '<td></td>'
349
line = line + "</tr>"
353
print self.html_footer
354
self.index_items = {}
356
def index_dump( self, index_filename = None ):
358
if index_filename == None:
359
index_filename = self.file_prefix + "index.html"
361
Formatter.index_dump( self, index_filename )
364
# Formatting the table of content
366
def toc_enter( self ):
367
print self.html_header
368
print "<center><h1>Table of Contents</h1></center>"
370
def toc_chapter_enter( self, chapter ):
371
print chapter_header + string.join(chapter.title) + chapter_inter
372
print "<table cellpadding=5>"
374
def toc_section_enter( self, section ):
375
print "<tr valign=top><td>"
376
print '<a href="' + self.make_section_url( section ) + '">' + \
377
section.title + '</a></td><td>'
379
print self.make_html_para( section.abstract )
381
def toc_section_exit( self, section ):
384
def toc_chapter_exit( self, chapter ):
388
def toc_index( self, index_filename ):
389
print chapter_header + '<a href="' + index_filename + '">Global Index</a>' + chapter_inter + chapter_footer
391
def toc_exit( self ):
392
print self.html_footer
394
def toc_dump( self, toc_filename = None, index_filename = None ):
395
if toc_filename == None:
396
toc_filename = self.file_prefix + "toc.html"
398
if index_filename == None:
399
index_filename = self.file_prefix + "index.html"
401
Formatter.toc_dump( self, toc_filename, index_filename )
404
# Formatting sections
406
def section_enter( self, section ):
407
print self.html_header
409
print section_title_header
411
print section_title_footer
413
# print section synopsys
414
print section_synopsis_header
415
print "<table align=center cellspacing=5 cellpadding=0 border=0>"
418
for b in section.blocks.values():
419
if len(b.name) > maxwidth:
420
maxwidth = len(b.name)
422
width = 70 # XXX magic number
423
columns = width / maxwidth
427
count = len(section.block_names)
428
rows = (count + columns-1)/columns
429
for r in range(rows):
431
for c in range(columns):
433
line = line + '<td></td><td>'
435
name = section.block_names[i]
436
line = line + '<a href="#' + name + '">' + name + '</a>'
438
line = line + '</td>'
439
line = line + "</tr>"
442
print "</table><br><br>"
443
print section_synopsis_footer
445
print description_header
446
print self.make_html_items( section.description )
447
print description_footer
449
def block_enter( self, block ):
452
# place html anchor if needed
454
print '<h4><a name="' + block.name + '">' + block.name + '</a></h4>'
456
# dump the block C source lines now
460
print self.html_source_quote( l, block.name )
464
def markup_enter( self, markup, block ):
465
if markup.tag == "description":
466
print description_header
468
print marker_header + markup.tag + marker_inter
470
self.print_html_markup( markup )
472
def markup_exit( self, markup, block ):
473
if markup.tag == "description":
474
print description_footer
478
def block_exit( self, block ):
482
def section_exit( self, section ):
486
def section_dump_all( self ):
487
for section in self.sections:
488
self.section_dump( section, self.file_prefix + section.name + '.html' )