Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / lib / libformatter.tex
blob6a97f809d96eff13cea5c21aeed3e5e9ede82a4e
1 \section{Standard Module \module{formatter}}
2 \label{module-formatter}
3 \stmodindex{formatter}
6 This module supports two interface definitions, each with mulitple
7 implementations. The \emph{formatter} interface is used by the
8 \class{HTMLParser} class of the \module{htmllib} module, and the
9 \emph{writer} interface is required by the formatter interface.
10 \withsubitem{(class in htmllib)}{\ttindex{HTMLParser}}
12 Formatter objects transform an abstract flow of formatting events into
13 specific output events on writer objects. Formatters manage several
14 stack structures to allow various properties of a writer object to be
15 changed and restored; writers need not be able to handle relative
16 changes nor any sort of ``change back'' operation. Specific writer
17 properties which may be controlled via formatter objects are
18 horizontal alignment, font, and left margin indentations. A mechanism
19 is provided which supports providing arbitrary, non-exclusive style
20 settings to a writer as well. Additional interfaces facilitate
21 formatting events which are not reversible, such as paragraph
22 separation.
24 Writer objects encapsulate device interfaces. Abstract devices, such
25 as file formats, are supported as well as physical devices. The
26 provided implementations all work with abstract devices. The
27 interface makes available mechanisms for setting the properties which
28 formatter objects manage and inserting data into the output.
31 \subsection{The Formatter Interface}
33 Interfaces to create formatters are dependent on the specific
34 formatter class being instantiated. The interfaces described below
35 are the required interfaces which all formatters must support once
36 initialized.
38 One data element is defined at the module level:
41 \begin{datadesc}{AS_IS}
42 Value which can be used in the font specification passed to the
43 \code{push_font()} method described below, or as the new value to any
44 other \code{push_\var{property}()} method. Pushing the \code{AS_IS}
45 value allows the corresponding \code{pop_\var{property}()} method to
46 be called without having to track whether the property was changed.
47 \end{datadesc}
49 The following attributes are defined for formatter instance objects:
52 \begin{memberdesc}[formatter]{writer}
53 The writer instance with which the formatter interacts.
54 \end{memberdesc}
57 \begin{methoddesc}[formatter]{end_paragraph}{blanklines}
58 Close any open paragraphs and insert at least \var{blanklines}
59 before the next paragraph.
60 \end{methoddesc}
62 \begin{methoddesc}[formatter]{add_line_break}{}
63 Add a hard line break if one does not already exist. This does not
64 break the logical paragraph.
65 \end{methoddesc}
67 \begin{methoddesc}[formatter]{add_hor_rule}{*args, **kw}
68 Insert a horizontal rule in the output. A hard break is inserted if
69 there is data in the current paragraph, but the logical paragraph is
70 not broken. The arguments and keywords are passed on to the writer's
71 \method{send_line_break()} method.
72 \end{methoddesc}
74 \begin{methoddesc}[formatter]{add_flowing_data}{data}
75 Provide data which should be formatted with collapsed whitespaces.
76 Whitespace from preceeding and successive calls to
77 \method{add_flowing_data()} is considered as well when the whitespace
78 collapse is performed. The data which is passed to this method is
79 expected to be word-wrapped by the output device. Note that any
80 word-wrapping still must be performed by the writer object due to the
81 need to rely on device and font information.
82 \end{methoddesc}
84 \begin{methoddesc}[formatter]{add_literal_data}{data}
85 Provide data which should be passed to the writer unchanged.
86 Whitespace, including newline and tab characters, are considered legal
87 in the value of \var{data}.
88 \end{methoddesc}
90 \begin{methoddesc}[formatter]{add_label_data}{format, counter}
91 Insert a label which should be placed to the left of the current left
92 margin. This should be used for constructing bulleted or numbered
93 lists. If the \var{format} value is a string, it is interpreted as a
94 format specification for \var{counter}, which should be an integer.
95 The result of this formatting becomes the value of the label; if
96 \var{format} is not a string it is used as the label value directly.
97 The label value is passed as the only argument to the writer's
98 \method{send_label_data()} method. Interpretation of non-string label
99 values is dependent on the associated writer.
101 Format specifications are strings which, in combination with a counter
102 value, are used to compute label values. Each character in the format
103 string is copied to the label value, with some characters recognized
104 to indicate a transform on the counter value. Specifically, the
105 character \character{1} represents the counter value formatter as an
106 arabic number, the characters \character{A} and \character{a}
107 represent alphabetic representations of the counter value in upper and
108 lower case, respectively, and \character{I} and \character{i}
109 represent the counter value in Roman numerals, in upper and lower
110 case. Note that the alphabetic and roman transforms require that the
111 counter value be greater than zero.
112 \end{methoddesc}
114 \begin{methoddesc}[formatter]{flush_softspace}{}
115 Send any pending whitespace buffered from a previous call to
116 \method{add_flowing_data()} to the associated writer object. This
117 should be called before any direct manipulation of the writer object.
118 \end{methoddesc}
120 \begin{methoddesc}[formatter]{push_alignment}{align}
121 Push a new alignment setting onto the alignment stack. This may be
122 \constant{AS_IS} if no change is desired. If the alignment value is
123 changed from the previous setting, the writer's \method{new_alignment()}
124 method is called with the \var{align} value.
125 \end{methoddesc}
127 \begin{methoddesc}[formatter]{pop_alignment}{}
128 Restore the previous alignment.
129 \end{methoddesc}
131 \begin{methoddesc}[formatter]{push_font}{\code{(}size, italic, bold, teletype\code{)}}
132 Change some or all font properties of the writer object. Properties
133 which are not set to \constant{AS_IS} are set to the values passed in
134 while others are maintained at their current settings. The writer's
135 \method{new_font()} method is called with the fully resolved font
136 specification.
137 \end{methoddesc}
139 \begin{methoddesc}[formatter]{pop_font}{}
140 Restore the previous font.
141 \end{methoddesc}
143 \begin{methoddesc}[formatter]{push_margin}{margin}
144 Increase the number of left margin indentations by one, associating
145 the logical tag \var{margin} with the new indentation. The initial
146 margin level is \code{0}. Changed values of the logical tag must be
147 true values; false values other than \constant{AS_IS} are not
148 sufficient to change the margin.
149 \end{methoddesc}
151 \begin{methoddesc}[formatter]{pop_margin}{}
152 Restore the previous margin.
153 \end{methoddesc}
155 \begin{methoddesc}[formatter]{push_style}{*styles}
156 Push any number of arbitrary style specifications. All styles are
157 pushed onto the styles stack in order. A tuple representing the
158 entire stack, including \constant{AS_IS} values, is passed to the
159 writer's \method{new_styles()} method.
160 \end{methoddesc}
162 \begin{methoddesc}[formatter]{pop_style}{\optional{n\code{ = 1}}}
163 Pop the last \var{n} style specifications passed to
164 \method{push_style()}. A tuple representing the revised stack,
165 including \constant{AS_IS} values, is passed to the writer's
166 \method{new_styles()} method.
167 \end{methoddesc}
169 \begin{methoddesc}[formatter]{set_spacing}{spacing}
170 Set the spacing style for the writer.
171 \end{methoddesc}
173 \begin{methoddesc}[formatter]{assert_line_data}{\optional{flag\code{ = 1}}}
174 Inform the formatter that data has been added to the current paragraph
175 out-of-band. This should be used when the writer has been manipulated
176 directly. The optional \var{flag} argument can be set to false if
177 the writer manipulations produced a hard line break at the end of the
178 output.
179 \end{methoddesc}
182 \subsection{Formatter Implementations}
184 Two implementations of formatter objects are provided by this module.
185 Most applications may use one of these classes without modification or
186 subclassing.
188 \begin{classdesc}{NullFormatter}{\optional{writer}}
189 A formatter which does nothing. If \var{writer} is omitted, a
190 \class{NullWriter} instance is created. No methods of the writer are
191 called by \class{NullFormatter} instances. Implementations should
192 inherit from this class if implementing a writer interface but don't
193 need to inherit any implementation.
194 \end{classdesc}
196 \begin{classdesc}{AbstractFormatter}{writer}
197 The standard formatter. This implementation has demonstrated wide
198 applicability to many writers, and may be used directly in most
199 circumstances. It has been used to implement a full-featured
200 world-wide web browser.
201 \end{classdesc}
205 \subsection{The Writer Interface}
207 Interfaces to create writers are dependent on the specific writer
208 class being instantiated. The interfaces described below are the
209 required interfaces which all writers must support once initialized.
210 Note that while most applications can use the
211 \class{AbstractFormatter} class as a formatter, the writer must
212 typically be provided by the application.
215 \begin{methoddesc}[writer]{flush}{}
216 Flush any buffered output or device control events.
217 \end{methoddesc}
219 \begin{methoddesc}[writer]{new_alignment}{align}
220 Set the alignment style. The \var{align} value can be any object,
221 but by convention is a string or \code{None}, where \code{None}
222 indicates that the writer's ``preferred'' alignment should be used.
223 Conventional \var{align} values are \code{'left'}, \code{'center'},
224 \code{'right'}, and \code{'justify'}.
225 \end{methoddesc}
227 \begin{methoddesc}[writer]{new_font}{font}
228 Set the font style. The value of \var{font} will be \code{None},
229 indicating that the device's default font should be used, or a tuple
230 of the form \code{(}\var{size}, \var{italic}, \var{bold},
231 \var{teletype}\code{)}. Size will be a string indicating the size of
232 font that should be used; specific strings and their interpretation
233 must be defined by the application. The \var{italic}, \var{bold}, and
234 \var{teletype} values are boolean indicators specifying which of those
235 font attributes should be used.
236 \end{methoddesc}
238 \begin{methoddesc}[writer]{new_margin}{margin, level}
239 Set the margin level to the integer \var{level} and the logical tag
240 to \var{margin}. Interpretation of the logical tag is at the
241 writer's discretion; the only restriction on the value of the logical
242 tag is that it not be a false value for non-zero values of
243 \var{level}.
244 \end{methoddesc}
246 \begin{methoddesc}[writer]{new_spacing}{spacing}
247 Set the spacing style to \var{spacing}.
248 \end{methoddesc}
250 \begin{methoddesc}[writer]{new_styles}{styles}
251 Set additional styles. The \var{styles} value is a tuple of
252 arbitrary values; the value \constant{AS_IS} should be ignored. The
253 \var{styles} tuple may be interpreted either as a set or as a stack
254 depending on the requirements of the application and writer
255 implementation.
256 \end{methoddesc}
258 \begin{methoddesc}[writer]{send_line_break}{}
259 Break the current line.
260 \end{methoddesc}
262 \begin{methoddesc}[writer]{send_paragraph}{blankline}
263 Produce a paragraph separation of at least \var{blankline} blank
264 lines, or the equivelent. The \var{blankline} value will be an
265 integer.
266 \end{methoddesc}
268 \begin{methoddesc}[writer]{send_hor_rule}{*args, **kw}
269 Display a horizontal rule on the output device. The arguments to this
270 method are entirely application- and writer-specific, and should be
271 interpreted with care. The method implementation may assume that a
272 line break has already been issued via \method{send_line_break()}.
273 \end{methoddesc}
275 \begin{methoddesc}[writer]{send_flowing_data}{data}
276 Output character data which may be word-wrapped and re-flowed as
277 needed. Within any sequence of calls to this method, the writer may
278 assume that spans of multiple whitespace characters have been
279 collapsed to single space characters.
280 \end{methoddesc}
282 \begin{methoddesc}[writer]{send_literal_data}{data}
283 Output character data which has already been formatted
284 for display. Generally, this should be interpreted to mean that line
285 breaks indicated by newline characters should be preserved and no new
286 line breaks should be introduced. The data may contain embedded
287 newline and tab characters, unlike data provided to the
288 \method{send_formatted_data()} interface.
289 \end{methoddesc}
291 \begin{methoddesc}[writer]{send_label_data}{data}
292 Set \var{data} to the left of the current left margin, if possible.
293 The value of \var{data} is not restricted; treatment of non-string
294 values is entirely application- and writer-dependent. This method
295 will only be called at the beginning of a line.
296 \end{methoddesc}
299 \subsection{Writer Implementations}
301 Three implementations of the writer object interface are provided as
302 examples by this module. Most applications will need to derive new
303 writer classes from the \class{NullWriter} class.
305 \begin{classdesc}{NullWriter}{}
306 A writer which only provides the interface definition; no actions are
307 taken on any methods. This should be the base class for all writers
308 which do not need to inherit any implementation methods.
309 \end{classdesc}
311 \begin{classdesc}{AbstractWriter}{}
312 A writer which can be used in debugging formatters, but not much
313 else. Each method simply announces itself by printing its name and
314 arguments on standard output.
315 \end{classdesc}
317 \begin{classdesc}{DumbWriter}{\optional{file\optional{, maxcol\code{ = 72}}}}
318 Simple writer class which writes output on the file object passed in
319 as \var{file} or, if \var{file} is omitted, on standard output. The
320 output is simply word-wrapped to the number of columns specified by
321 \var{maxcol}. This class is suitable for reflowing a sequence of
322 paragraphs.
323 \end{classdesc}