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