1
# Formatter (c) 2002, 2004, 2007, 2008 David Turner <david@freetype.org>
8
# This is the base Formatter class. Its purpose is to convert
9
# a content processor's data into specific documents (i.e., table of
10
# contents, global index, and individual API reference indices).
12
# You need to sub-class it to output anything sensible. For example,
13
# the file tohtml.py contains the definition of the HtmlFormatter sub-class
14
# used to output -- you guessed it -- HTML.
19
def __init__( self, processor ):
20
self.processor = processor
22
self.chapters = processor.chapters
23
self.sections = processor.sections.values()
26
# store all blocks in a dictionary
28
for section in self.sections:
29
for block in section.blocks.values():
30
self.add_identifier( block.name, block )
32
# add enumeration values to the index, since this is useful
33
for markup in block.markups:
34
if markup.tag == 'values':
35
for field in markup.fields:
36
self.add_identifier( field.name, block )
38
self.block_index = self.identifiers.keys()
39
self.block_index.sort( index_sort )
41
def add_identifier( self, name, block ):
42
if self.identifiers.has_key( name ):
45
"WARNING: duplicate definition for '" + name + "' in " + \
46
block.location() + ", previous definition in " + \
47
self.identifiers[name].location() + "\n" )
49
self.identifiers[name] = block
52
# Formatting the table of contents
54
def toc_enter( self ):
57
def toc_chapter_enter( self, chapter ):
60
def toc_section_enter( self, section ):
63
def toc_section_exit( self, section ):
66
def toc_chapter_exit( self, chapter ):
69
def toc_index( self, index_filename ):
75
def toc_dump( self, toc_filename = None, index_filename = None ):
78
output = open_output( toc_filename )
82
for chap in self.processor.chapters:
84
self.toc_chapter_enter( chap )
86
for section in chap.sections:
87
self.toc_section_enter( section )
88
self.toc_section_exit( section )
90
self.toc_chapter_exit( chap )
92
self.toc_index( index_filename )
97
close_output( output )
100
# Formatting the index
102
def index_enter( self ):
105
def index_name_enter( self, name ):
108
def index_name_exit( self, name ):
111
def index_exit( self ):
114
def index_dump( self, index_filename = None ):
117
output = open_output( index_filename )
121
for name in self.block_index:
122
self.index_name_enter( name )
123
self.index_name_exit( name )
128
close_output( output )
131
# Formatting a section
133
def section_enter( self, section ):
136
def block_enter( self, block ):
139
def markup_enter( self, markup, block = None ):
142
def field_enter( self, field, markup = None, block = None ):
145
def field_exit( self, field, markup = None, block = None ):
148
def markup_exit( self, markup, block = None ):
151
def block_exit( self, block ):
154
def section_exit( self, section ):
157
def section_dump( self, section, section_filename = None ):
160
output = open_output( section_filename )
162
self.section_enter( section )
164
for name in section.block_names:
165
block = self.identifiers[name]
166
self.block_enter( block )
168
for markup in block.markups[1:]: # always ignore first markup!
169
self.markup_enter( markup, block )
171
for field in markup.fields:
172
self.field_enter( field, markup, block )
173
self.field_exit( field, markup, block )
175
self.markup_exit( markup, block )
177
self.block_exit( block )
179
self.section_exit( section )
182
close_output( output )
184
def section_dump_all( self ):
185
for section in self.sections:
186
self.section_dump( section )