blob: 9ecd1cc322fde0813d172aae047b5acfa50e824f [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
Fred Drake19479911998-02-13 06:58:54 +00005\setindexsubitem{(in module formatter)}
Fred Drakee83b30d1996-10-08 21:53:33 +00006
7This module supports two interface definitions, each with mulitple
8implementations. The \emph{formatter} interface is used by the
Fred Drake3b5da761998-03-12 15:33:05 +00009\class{HTMLParser} class of the \module{htmllib} module, and the
Fred Drakee83b30d1996-10-08 21:53:33 +000010\emph{writer} interface is required by the formatter interface.
Fred Drake3b5da761998-03-12 15:33:05 +000011\withsubitem{(im module htmllib)}{\ttindex{HTMLParser}}
Fred Drakee83b30d1996-10-08 21:53:33 +000012
13Formatter objects transform an abstract flow of formatting events into
14specific output events on writer objects. Formatters manage several
15stack structures to allow various properties of a writer object to be
16changed and restored; writers need not be able to handle relative
17changes nor any sort of ``change back'' operation. Specific writer
18properties which may be controlled via formatter objects are
19horizontal alignment, font, and left margin indentations. A mechanism
20is provided which supports providing arbitrary, non-exclusive style
21settings to a writer as well. Additional interfaces facilitate
22formatting events which are not reversible, such as paragraph
23separation.
24
25Writer objects encapsulate device interfaces. Abstract devices, such
26as file formats, are supported as well as physical devices. The
27provided implementations all work with abstract devices. The
28interface makes available mechanisms for setting the properties which
29formatter objects manage and inserting data into the output.
30
31
32\subsection{The Formatter Interface}
33
34Interfaces to create formatters are dependent on the specific
35formatter class being instantiated. The interfaces described below
36are the required interfaces which all formatters must support once
37initialized.
38
39One data element is defined at the module level:
40
41\begin{datadesc}{AS_IS}
42Value 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
44other \code{push_\var{property}()} method. Pushing the \code{AS_IS}
45value allows the corresponding \code{pop_\var{property}()} method to
46be called without having to track whether the property was changed.
47\end{datadesc}
48
49The following attributes are defined for formatter instance objects:
50
Fred Drake3b5da761998-03-12 15:33:05 +000051\setindexsubitem{(formatter attribute)}
Fred Drake8f925951996-10-09 16:13:22 +000052
Fred Drakee83b30d1996-10-08 21:53:33 +000053\begin{datadesc}{writer}
54The writer instance with which the formatter interacts.
55\end{datadesc}
56
57
Fred Drake3b5da761998-03-12 15:33:05 +000058\setindexsubitem{(formatter method)}
Fred Drake8f925951996-10-09 16:13:22 +000059
Fred Drakee83b30d1996-10-08 21:53:33 +000060\begin{funcdesc}{end_paragraph}{blanklines}
Fred Drake3b5da761998-03-12 15:33:05 +000061Close any open paragraphs and insert at least \var{blanklines}
Fred Drakee83b30d1996-10-08 21:53:33 +000062before the next paragraph.
63\end{funcdesc}
64
65\begin{funcdesc}{add_line_break}{}
66Add a hard line break if one does not already exist. This does not
67break the logical paragraph.
68\end{funcdesc}
69
70\begin{funcdesc}{add_hor_rule}{*args\, **kw}
71Insert a horizontal rule in the output. A hard break is inserted if
72there is data in the current paragraph, but the logical paragraph is
73not broken. The arguments and keywords are passed on to the writer's
Fred Drake3b5da761998-03-12 15:33:05 +000074\method{send_line_break()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +000075\end{funcdesc}
76
77\begin{funcdesc}{add_flowing_data}{data}
78Provide data which should be formatted with collapsed whitespaces.
79Whitespace from preceeding and successive calls to
Fred Drake3b5da761998-03-12 15:33:05 +000080\method{add_flowing_data()} is considered as well when the whitespace
Fred Drakee83b30d1996-10-08 21:53:33 +000081collapse is performed. The data which is passed to this method is
82expected to be word-wrapped by the output device. Note that any
83word-wrapping still must be performed by the writer object due to the
84need to rely on device and font information.
85\end{funcdesc}
86
87\begin{funcdesc}{add_literal_data}{data}
88Provide data which should be passed to the writer unchanged.
89Whitespace, including newline and tab characters, are considered legal
Fred Drake3b5da761998-03-12 15:33:05 +000090in the value of \var{data}.
Fred Drakee83b30d1996-10-08 21:53:33 +000091\end{funcdesc}
92
93\begin{funcdesc}{add_label_data}{format, counter}
94Insert a label which should be placed to the left of the current left
95margin. This should be used for constructing bulleted or numbered
Fred Drake3b5da761998-03-12 15:33:05 +000096lists. If the \var{format} value is a string, it is interpreted as a
97format specification for \var{counter}, which should be an integer.
Fred Drakee83b30d1996-10-08 21:53:33 +000098The result of this formatting becomes the value of the label; if
Fred Drake3b5da761998-03-12 15:33:05 +000099\var{format} is not a string it is used as the label value directly.
Fred Drakee83b30d1996-10-08 21:53:33 +0000100The label value is passed as the only argument to the writer's
Fred Drake3b5da761998-03-12 15:33:05 +0000101\method{send_label_data()} method. Interpretation of non-string label
Fred Drakee83b30d1996-10-08 21:53:33 +0000102values is dependent on the associated writer.
103
104Format specifications are strings which, in combination with a counter
105value, are used to compute label values. Each character in the format
106string is copied to the label value, with some characters recognized
107to indicate a transform on the counter value. Specifically, the
Fred Drake3b5da761998-03-12 15:33:05 +0000108character \character{1} represents the counter value formatter as an
109arabic number, the characters \character{A} and \character{a}
110represent alphabetic representations of the counter value in upper and
111lower case, respectively, and \character{I} and \character{i}
112represent the counter value in Roman numerals, in upper and lower
113case. Note that the alphabetic and roman transforms require that the
114counter value be greater than zero.
Fred Drakee83b30d1996-10-08 21:53:33 +0000115\end{funcdesc}
116
117\begin{funcdesc}{flush_softspace}{}
118Send any pending whitespace buffered from a previous call to
Fred Drake3b5da761998-03-12 15:33:05 +0000119\method{add_flowing_data()} to the associated writer object. This
Fred Drakee83b30d1996-10-08 21:53:33 +0000120should be called before any direct manipulation of the writer object.
121\end{funcdesc}
122
123\begin{funcdesc}{push_alignment}{align}
124Push a new alignment setting onto the alignment stack. This may be
Fred Drake3b5da761998-03-12 15:33:05 +0000125\constant{AS_IS} if no change is desired. If the alignment value is
126changed from the previous setting, the writer's \method{new_alignment()}
127method is called with the \var{align} value.
Fred Drakee83b30d1996-10-08 21:53:33 +0000128\end{funcdesc}
129
130\begin{funcdesc}{pop_alignment}{}
131Restore the previous alignment.
132\end{funcdesc}
133
Fred Drake3b5da761998-03-12 15:33:05 +0000134\begin{funcdesc}{push_font}{\code{(}size, italic, bold, teletype\code{)}}
Fred Drakee83b30d1996-10-08 21:53:33 +0000135Change some or all font properties of the writer object. Properties
Fred Drake3b5da761998-03-12 15:33:05 +0000136which are not set to \constant{AS_IS} are set to the values passed in
Fred Drakee83b30d1996-10-08 21:53:33 +0000137while others are maintained at their current settings. The writer's
Fred Drake3b5da761998-03-12 15:33:05 +0000138\method{new_font()} method is called with the fully resolved font
Fred Drakee83b30d1996-10-08 21:53:33 +0000139specification.
140\end{funcdesc}
141
142\begin{funcdesc}{pop_font}{}
143Restore the previous font.
144\end{funcdesc}
145
146\begin{funcdesc}{push_margin}{margin}
147Increase the number of left margin indentations by one, associating
Fred Drake3b5da761998-03-12 15:33:05 +0000148the logical tag \var{margin} with the new indentation. The initial
Fred Drakee83b30d1996-10-08 21:53:33 +0000149margin level is \code{0}. Changed values of the logical tag must be
Fred Drake3b5da761998-03-12 15:33:05 +0000150true values; false values other than \constant{AS_IS} are not
151sufficient to change the margin.
Fred Drakee83b30d1996-10-08 21:53:33 +0000152\end{funcdesc}
153
154\begin{funcdesc}{pop_margin}{}
155Restore the previous margin.
156\end{funcdesc}
157
158\begin{funcdesc}{push_style}{*styles}
159Push any number of arbitrary style specifications. All styles are
160pushed onto the styles stack in order. A tuple representing the
Fred Drake3b5da761998-03-12 15:33:05 +0000161entire stack, including \constant{AS_IS} values, is passed to the
162writer's \method{new_styles()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +0000163\end{funcdesc}
164
165\begin{funcdesc}{pop_style}{\optional{n\code{ = 1}}}
Fred Drake3b5da761998-03-12 15:33:05 +0000166Pop the last \var{n} style specifications passed to
167\method{push_style()}. A tuple representing the revised stack,
168including \constant{AS_IS} values, is passed to the writer's
169\method{new_styles()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +0000170\end{funcdesc}
171
172\begin{funcdesc}{set_spacing}{spacing}
173Set the spacing style for the writer.
174\end{funcdesc}
175
176\begin{funcdesc}{assert_line_data}{\optional{flag\code{ = 1}}}
177Inform the formatter that data has been added to the current paragraph
178out-of-band. This should be used when the writer has been manipulated
Fred Drake3b5da761998-03-12 15:33:05 +0000179directly. The optional \var{flag} argument can be set to false if
Fred Drakee83b30d1996-10-08 21:53:33 +0000180the writer manipulations produced a hard line break at the end of the
181output.
182\end{funcdesc}
183
184
185\subsection{Formatter Implementations}
186
Fred Drake8f925951996-10-09 16:13:22 +0000187Two implementations of formatter objects are provided by this module.
188Most applications may use one of these classes without modification or
189subclassing.
190
Fred Drake19479911998-02-13 06:58:54 +0000191\setindexsubitem{(in module formatter)}
Fred Drake8f925951996-10-09 16:13:22 +0000192
Fred Drake3b5da761998-03-12 15:33:05 +0000193\begin{classdesc}{NullFormatter}{\optional{writer}}
194A formatter which does nothing. If \var{writer} is omitted, a
195\class{NullWriter} instance is created. No methods of the writer are
196called by \class{NullFormatter} instances. Implementations should
197inherit from this class if implementing a writer interface but don't
198need to inherit any implementation.
199\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000200
Fred Drake3b5da761998-03-12 15:33:05 +0000201\begin{classdesc}{AbstractFormatter}{writer}
Fred Drakee83b30d1996-10-08 21:53:33 +0000202The standard formatter. This implementation has demonstrated wide
203applicability to many writers, and may be used directly in most
Fred Drake8f925951996-10-09 16:13:22 +0000204circumstances. It has been used to implement a full-featured
205world-wide web browser.
Fred Drake3b5da761998-03-12 15:33:05 +0000206\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000207
208
209
210\subsection{The Writer Interface}
211
212Interfaces to create writers are dependent on the specific writer
213class being instantiated. The interfaces described below are the
214required interfaces which all writers must support once initialized.
Fred Drake3b5da761998-03-12 15:33:05 +0000215Note that while most applications can use the
216\class{AbstractFormatter} class as a formatter, the writer must
217typically be provided by the application.
Fred Drakee83b30d1996-10-08 21:53:33 +0000218
Fred Drake19479911998-02-13 06:58:54 +0000219\setindexsubitem{(writer object method)}
Fred Drake8f925951996-10-09 16:13:22 +0000220
Fred Drake591bbb11996-12-31 20:51:42 +0000221\begin{funcdesc}{flush}{}
222Flush any buffered output or device control events.
223\end{funcdesc}
224
Fred Drakee83b30d1996-10-08 21:53:33 +0000225\begin{funcdesc}{new_alignment}{align}
Fred Drake3b5da761998-03-12 15:33:05 +0000226Set the alignment style. The \var{align} value can be any object,
Fred Drakee83b30d1996-10-08 21:53:33 +0000227but by convention is a string or \code{None}, where \code{None}
228indicates that the writer's ``preferred'' alignment should be used.
Fred Drake3b5da761998-03-12 15:33:05 +0000229Conventional \var{align} values are \code{'left'}, \code{'center'},
Fred Drakee83b30d1996-10-08 21:53:33 +0000230\code{'right'}, and \code{'justify'}.
231\end{funcdesc}
232
233\begin{funcdesc}{new_font}{font}
Fred Drake3b5da761998-03-12 15:33:05 +0000234Set the font style. The value of \var{font} will be \code{None},
Fred Drakee83b30d1996-10-08 21:53:33 +0000235indicating that the device's default font should be used, or a tuple
Fred Drake3b5da761998-03-12 15:33:05 +0000236of the form \code{(}\var{size}, \var{italic}, \var{bold},
237\var{teletype}\code{)}. Size will be a string indicating the size of
238font that should be used; specific strings and their interpretation
239must be defined by the application. The \var{italic}, \var{bold}, and
240\var{teletype} values are boolean indicators specifying which of those
241font attributes should be used.
Fred Drakee83b30d1996-10-08 21:53:33 +0000242\end{funcdesc}
243
244\begin{funcdesc}{new_margin}{margin, level}
Fred Drake3b5da761998-03-12 15:33:05 +0000245Set the margin level to the integer \var{level} and the logical tag
246to \var{margin}. Interpretation of the logical tag is at the
Fred Drakee83b30d1996-10-08 21:53:33 +0000247writer's discretion; the only restriction on the value of the logical
248tag is that it not be a false value for non-zero values of
Fred Drake3b5da761998-03-12 15:33:05 +0000249\var{level}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000250\end{funcdesc}
251
252\begin{funcdesc}{new_spacing}{spacing}
Fred Drake3b5da761998-03-12 15:33:05 +0000253Set the spacing style to \var{spacing}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000254\end{funcdesc}
255
256\begin{funcdesc}{new_styles}{styles}
Fred Drake3b5da761998-03-12 15:33:05 +0000257Set additional styles. The \var{styles} value is a tuple of
258arbitrary values; the value \constant{AS_IS} should be ignored. The
259\var{styles} tuple may be interpreted either as a set or as a stack
Fred Drakee83b30d1996-10-08 21:53:33 +0000260depending on the requirements of the application and writer
261implementation.
262\end{funcdesc}
263
264\begin{funcdesc}{send_line_break}{}
265Break the current line.
266\end{funcdesc}
267
268\begin{funcdesc}{send_paragraph}{blankline}
Fred Drake3b5da761998-03-12 15:33:05 +0000269Produce a paragraph separation of at least \var{blankline} blank
270lines, or the equivelent. The \var{blankline} value will be an
Fred Drakee83b30d1996-10-08 21:53:33 +0000271integer.
272\end{funcdesc}
273
274\begin{funcdesc}{send_hor_rule}{*args\, **kw}
275Display a horizontal rule on the output device. The arguments to this
276method are entirely application- and writer-specific, and should be
277interpreted with care. The method implementation may assume that a
Fred Drake3b5da761998-03-12 15:33:05 +0000278line break has already been issued via \method{send_line_break()}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000279\end{funcdesc}
280
281\begin{funcdesc}{send_flowing_data}{data}
282Output character data which may be word-wrapped and re-flowed as
283needed. Within any sequence of calls to this method, the writer may
284assume that spans of multiple whitespace characters have been
285collapsed to single space characters.
286\end{funcdesc}
287
288\begin{funcdesc}{send_literal_data}{data}
289Output character data which has already been formatted
290for display. Generally, this should be interpreted to mean that line
291breaks indicated by newline characters should be preserved and no new
292line breaks should be introduced. The data may contain embedded
293newline and tab characters, unlike data provided to the
Fred Drake3b5da761998-03-12 15:33:05 +0000294\method{send_formatted_data()} interface.
Fred Drakee83b30d1996-10-08 21:53:33 +0000295\end{funcdesc}
296
297\begin{funcdesc}{send_label_data}{data}
Fred Drake3b5da761998-03-12 15:33:05 +0000298Set \var{data} to the left of the current left margin, if possible.
299The value of \var{data} is not restricted; treatment of non-string
Fred Drakee83b30d1996-10-08 21:53:33 +0000300values is entirely application- and writer-dependent. This method
301will only be called at the beginning of a line.
302\end{funcdesc}
303
304
305\subsection{Writer Implementations}
306
Fred Drake8f925951996-10-09 16:13:22 +0000307Three implementations of the writer object interface are provided as
308examples by this module. Most applications will need to derive new
Fred Drake3b5da761998-03-12 15:33:05 +0000309writer classes from the \class{NullWriter} class.
Fred Drake8f925951996-10-09 16:13:22 +0000310
Fred Drake19479911998-02-13 06:58:54 +0000311\setindexsubitem{(in module formatter)}
Fred Drake8f925951996-10-09 16:13:22 +0000312
Fred Drakee83b30d1996-10-08 21:53:33 +0000313\begin{funcdesc}{NullWriter}{}
314A writer which only provides the interface definition; no actions are
315taken on any methods. This should be the base class for all writers
316which do not need to inherit any implementation methods.
317\end{funcdesc}
318
319\begin{funcdesc}{AbstractWriter}{}
320A writer which can be used in debugging formatters, but not much
Fred Drake5081b221998-01-15 05:49:00 +0000321else. Each method simply announces itself by printing its name and
Fred Drakee83b30d1996-10-08 21:53:33 +0000322arguments on standard output.
323\end{funcdesc}
324
Fred Drake3b5da761998-03-12 15:33:05 +0000325\begin{funcdesc}{DumbWriter}{\optional{file\optional{\, maxcol\code{ = 72}}}}
Fred Drakee83b30d1996-10-08 21:53:33 +0000326Simple writer class which writes output on the file object passed in
Fred Drake3b5da761998-03-12 15:33:05 +0000327as \var{file} or, if \var{file} is omitted, on standard output. The
Fred Drakee83b30d1996-10-08 21:53:33 +0000328output is simply word-wrapped to the number of columns specified by
Fred Drake3b5da761998-03-12 15:33:05 +0000329\var{maxcol}. This class is suitable for reflowing a sequence of
Fred Drakee83b30d1996-10-08 21:53:33 +0000330paragraphs.
331\end{funcdesc}