4 # Convert parsed content blocks to a structured document (library file).
6 # Copyright 2002, 2004, 2007, 2008, 2014 by
9 # This file is part of the FreeType project, and may only be used,
10 # modified, and distributed under the terms of the FreeType project
11 # license, LICENSE.TXT. By continuing to use, modify, or distribute
12 # this file you indicate that you have read the license and
13 # understand and accept it fully.
16 # This is the base Formatter class. Its purpose is to convert a content
17 # processor's data into specific documents (i.e., table of contents, global
18 # index, and individual API reference indices).
20 # You need to sub-class it to output anything sensible. For example, the
21 # file `tohtml.py' contains the definition of the `HtmlFormatter' sub-class
31 ################################################################
37 def __init__( self
, processor
):
38 self
.processor
= processor
40 self
.chapters
= processor
.chapters
41 self
.sections
= processor
.sections
.values()
44 # store all blocks in a dictionary
46 for section
in self
.sections
:
47 for block
in section
.blocks
.values():
48 self
.add_identifier( block
.name
, block
)
50 # add enumeration values to the index, since this is useful
51 for markup
in block
.markups
:
52 if markup
.tag
== 'values':
53 for field
in markup
.fields
:
54 self
.add_identifier( field
.name
, block
)
56 self
.block_index
= self
.identifiers
.keys()
57 self
.block_index
.sort( key
= index_key
)
59 def add_identifier( self
, name
, block
):
60 if name
in self
.identifiers
:
62 sys
.stderr
.write( "WARNING: duplicate definition for"
64 + "in " + block
.location() + ", "
65 + "previous definition in "
66 + self
.identifiers
[name
].location()
69 self
.identifiers
[name
] = block
72 # formatting the table of contents
74 def toc_enter( self
):
77 def toc_chapter_enter( self
, chapter
):
80 def toc_section_enter( self
, section
):
83 def toc_section_exit( self
, section
):
86 def toc_chapter_exit( self
, chapter
):
89 def toc_index( self
, index_filename
):
95 def toc_dump( self
, toc_filename
= None, index_filename
= None ):
98 output
= open_output( toc_filename
)
102 for chap
in self
.processor
.chapters
:
104 self
.toc_chapter_enter( chap
)
106 for section
in chap
.sections
:
107 self
.toc_section_enter( section
)
108 self
.toc_section_exit( section
)
110 self
.toc_chapter_exit( chap
)
112 self
.toc_index( index_filename
)
117 close_output( output
)
120 # formatting the index
122 def index_enter( self
):
125 def index_name_enter( self
, name
):
128 def index_name_exit( self
, name
):
131 def index_exit( self
):
134 def index_dump( self
, index_filename
= None ):
137 output
= open_output( index_filename
)
141 for name
in self
.block_index
:
142 self
.index_name_enter( name
)
143 self
.index_name_exit( name
)
148 close_output( output
)
151 # formatting a section
153 def section_enter( self
, section
):
156 def block_enter( self
, block
):
159 def markup_enter( self
, markup
, block
= None ):
162 def field_enter( self
, field
, markup
= None, block
= None ):
165 def field_exit( self
, field
, markup
= None, block
= None ):
168 def markup_exit( self
, markup
, block
= None ):
171 def block_exit( self
, block
):
174 def section_exit( self
, section
):
177 def section_dump( self
, section
, section_filename
= None ):
180 output
= open_output( section_filename
)
182 self
.section_enter( section
)
184 for name
in section
.block_names
:
187 block
= self
.identifiers
[name
]
188 # `block_names' can contain field names also,
189 # which we filter out
190 for markup
in block
.markups
:
191 if markup
.tag
== 'values':
192 for field
in markup
.fields
:
193 if field
.name
== name
:
196 skip_entry
= 1 # this happens e.g. for `/empty/' entries
201 self
.block_enter( block
)
203 for markup
in block
.markups
[1:]: # always ignore first markup!
204 self
.markup_enter( markup
, block
)
206 for field
in markup
.fields
:
207 self
.field_enter( field
, markup
, block
)
208 self
.field_exit( field
, markup
, block
)
210 self
.markup_exit( markup
, block
)
212 self
.block_exit( block
)
214 self
.section_exit( section
)
217 close_output( output
)
219 def section_dump_all( self
):
220 for section
in self
.sections
:
221 self
.section_dump( section
)