5172
|
1 |
# Formatter (c) 2002, 2004, 2007, 2008 David Turner <david@freetype.org>
|
|
2 |
#
|
|
3 |
|
|
4 |
from sources import *
|
|
5 |
from content import *
|
|
6 |
from utils import *
|
|
7 |
|
|
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).
|
|
11 |
#
|
|
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.
|
|
15 |
#
|
|
16 |
|
|
17 |
class Formatter:
|
|
18 |
|
|
19 |
def __init__( self, processor ):
|
|
20 |
self.processor = processor
|
|
21 |
self.identifiers = {}
|
|
22 |
self.chapters = processor.chapters
|
|
23 |
self.sections = processor.sections.values()
|
|
24 |
self.block_index = []
|
|
25 |
|
|
26 |
# store all blocks in a dictionary
|
|
27 |
self.blocks = []
|
|
28 |
for section in self.sections:
|
|
29 |
for block in section.blocks.values():
|
|
30 |
self.add_identifier( block.name, block )
|
|
31 |
|
|
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 )
|
|
37 |
|
|
38 |
self.block_index = self.identifiers.keys()
|
|
39 |
self.block_index.sort( index_sort )
|
|
40 |
|
|
41 |
def add_identifier( self, name, block ):
|
|
42 |
if self.identifiers.has_key( name ):
|
|
43 |
# duplicate name!
|
|
44 |
sys.stderr.write( \
|
|
45 |
"WARNING: duplicate definition for '" + name + "' in " + \
|
|
46 |
block.location() + ", previous definition in " + \
|
|
47 |
self.identifiers[name].location() + "\n" )
|
|
48 |
else:
|
|
49 |
self.identifiers[name] = block
|
|
50 |
|
|
51 |
#
|
|
52 |
# Formatting the table of contents
|
|
53 |
#
|
|
54 |
def toc_enter( self ):
|
|
55 |
pass
|
|
56 |
|
|
57 |
def toc_chapter_enter( self, chapter ):
|
|
58 |
pass
|
|
59 |
|
|
60 |
def toc_section_enter( self, section ):
|
|
61 |
pass
|
|
62 |
|
|
63 |
def toc_section_exit( self, section ):
|
|
64 |
pass
|
|
65 |
|
|
66 |
def toc_chapter_exit( self, chapter ):
|
|
67 |
pass
|
|
68 |
|
|
69 |
def toc_index( self, index_filename ):
|
|
70 |
pass
|
|
71 |
|
|
72 |
def toc_exit( self ):
|
|
73 |
pass
|
|
74 |
|
|
75 |
def toc_dump( self, toc_filename = None, index_filename = None ):
|
|
76 |
output = None
|
|
77 |
if toc_filename:
|
|
78 |
output = open_output( toc_filename )
|
|
79 |
|
|
80 |
self.toc_enter()
|
|
81 |
|
|
82 |
for chap in self.processor.chapters:
|
|
83 |
|
|
84 |
self.toc_chapter_enter( chap )
|
|
85 |
|
|
86 |
for section in chap.sections:
|
|
87 |
self.toc_section_enter( section )
|
|
88 |
self.toc_section_exit( section )
|
|
89 |
|
|
90 |
self.toc_chapter_exit( chap )
|
|
91 |
|
|
92 |
self.toc_index( index_filename )
|
|
93 |
|
|
94 |
self.toc_exit()
|
|
95 |
|
|
96 |
if output:
|
|
97 |
close_output( output )
|
|
98 |
|
|
99 |
#
|
|
100 |
# Formatting the index
|
|
101 |
#
|
|
102 |
def index_enter( self ):
|
|
103 |
pass
|
|
104 |
|
|
105 |
def index_name_enter( self, name ):
|
|
106 |
pass
|
|
107 |
|
|
108 |
def index_name_exit( self, name ):
|
|
109 |
pass
|
|
110 |
|
|
111 |
def index_exit( self ):
|
|
112 |
pass
|
|
113 |
|
|
114 |
def index_dump( self, index_filename = None ):
|
|
115 |
output = None
|
|
116 |
if index_filename:
|
|
117 |
output = open_output( index_filename )
|
|
118 |
|
|
119 |
self.index_enter()
|
|
120 |
|
|
121 |
for name in self.block_index:
|
|
122 |
self.index_name_enter( name )
|
|
123 |
self.index_name_exit( name )
|
|
124 |
|
|
125 |
self.index_exit()
|
|
126 |
|
|
127 |
if output:
|
|
128 |
close_output( output )
|
|
129 |
|
|
130 |
#
|
|
131 |
# Formatting a section
|
|
132 |
#
|
|
133 |
def section_enter( self, section ):
|
|
134 |
pass
|
|
135 |
|
|
136 |
def block_enter( self, block ):
|
|
137 |
pass
|
|
138 |
|
|
139 |
def markup_enter( self, markup, block = None ):
|
|
140 |
pass
|
|
141 |
|
|
142 |
def field_enter( self, field, markup = None, block = None ):
|
|
143 |
pass
|
|
144 |
|
|
145 |
def field_exit( self, field, markup = None, block = None ):
|
|
146 |
pass
|
|
147 |
|
|
148 |
def markup_exit( self, markup, block = None ):
|
|
149 |
pass
|
|
150 |
|
|
151 |
def block_exit( self, block ):
|
|
152 |
pass
|
|
153 |
|
|
154 |
def section_exit( self, section ):
|
|
155 |
pass
|
|
156 |
|
|
157 |
def section_dump( self, section, section_filename = None ):
|
|
158 |
output = None
|
|
159 |
if section_filename:
|
|
160 |
output = open_output( section_filename )
|
|
161 |
|
|
162 |
self.section_enter( section )
|
|
163 |
|
|
164 |
for name in section.block_names:
|
|
165 |
block = self.identifiers[name]
|
|
166 |
self.block_enter( block )
|
|
167 |
|
|
168 |
for markup in block.markups[1:]: # always ignore first markup!
|
|
169 |
self.markup_enter( markup, block )
|
|
170 |
|
|
171 |
for field in markup.fields:
|
|
172 |
self.field_enter( field, markup, block )
|
|
173 |
self.field_exit( field, markup, block )
|
|
174 |
|
|
175 |
self.markup_exit( markup, block )
|
|
176 |
|
|
177 |
self.block_exit( block )
|
|
178 |
|
|
179 |
self.section_exit( section )
|
|
180 |
|
|
181 |
if output:
|
|
182 |
close_output( output )
|
|
183 |
|
|
184 |
def section_dump_all( self ):
|
|
185 |
for section in self.sections:
|
|
186 |
self.section_dump( section )
|
|
187 |
|
|
188 |
# eof
|