blob: 8c9ab3e0cbaa5011b740e9954b8af853811c8f22 [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 Drakee83b30d1996-10-08 21:53:33 +00005
6This module supports two interface definitions, each with mulitple
7implementations. The \emph{formatter} interface is used by the
Fred Drake3b5da761998-03-12 15:33:05 +00008\class{HTMLParser} class of the \module{htmllib} module, and the
Fred Drakee83b30d1996-10-08 21:53:33 +00009\emph{writer} interface is required by the formatter interface.
Fred Drake3b5da761998-03-12 15:33:05 +000010\withsubitem{(im module htmllib)}{\ttindex{HTMLParser}}
Fred Drakee83b30d1996-10-08 21:53:33 +000011
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 Drake3b5da761998-03-12 15:33:05 +000050\setindexsubitem{(formatter attribute)}
Fred Drake8f925951996-10-09 16:13:22 +000051
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 Drake3b5da761998-03-12 15:33:05 +000057\setindexsubitem{(formatter method)}
Fred Drake8f925951996-10-09 16:13:22 +000058
Fred Drakee83b30d1996-10-08 21:53:33 +000059\begin{funcdesc}{end_paragraph}{blanklines}
Fred Drake3b5da761998-03-12 15:33:05 +000060Close any open paragraphs and insert at least \var{blanklines}
Fred Drakee83b30d1996-10-08 21:53:33 +000061before 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
Fred Drake3b5da761998-03-12 15:33:05 +000073\method{send_line_break()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +000074\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
Fred Drake3b5da761998-03-12 15:33:05 +000079\method{add_flowing_data()} is considered as well when the whitespace
Fred Drakee83b30d1996-10-08 21:53:33 +000080collapse 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
Fred Drake3b5da761998-03-12 15:33:05 +000089in the value of \var{data}.
Fred Drakee83b30d1996-10-08 21:53:33 +000090\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
Fred Drake3b5da761998-03-12 15:33:05 +000095lists. If the \var{format} value is a string, it is interpreted as a
96format specification for \var{counter}, which should be an integer.
Fred Drakee83b30d1996-10-08 21:53:33 +000097The result of this formatting becomes the value of the label; if
Fred Drake3b5da761998-03-12 15:33:05 +000098\var{format} is not a string it is used as the label value directly.
Fred Drakee83b30d1996-10-08 21:53:33 +000099The label value is passed as the only argument to the writer's
Fred Drake3b5da761998-03-12 15:33:05 +0000100\method{send_label_data()} method. Interpretation of non-string label
Fred Drakee83b30d1996-10-08 21:53:33 +0000101values 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
Fred Drake3b5da761998-03-12 15:33:05 +0000107character \character{1} represents the counter value formatter as an
108arabic number, the characters \character{A} and \character{a}
109represent alphabetic representations of the counter value in upper and
110lower case, respectively, and \character{I} and \character{i}
111represent the counter value in Roman numerals, in upper and lower
112case. Note that the alphabetic and roman transforms require that the
113counter value be greater than zero.
Fred Drakee83b30d1996-10-08 21:53:33 +0000114\end{funcdesc}
115
116\begin{funcdesc}{flush_softspace}{}
117Send any pending whitespace buffered from a previous call to
Fred Drake3b5da761998-03-12 15:33:05 +0000118\method{add_flowing_data()} to the associated writer object. This
Fred Drakee83b30d1996-10-08 21:53:33 +0000119should 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
Fred Drake3b5da761998-03-12 15:33:05 +0000124\constant{AS_IS} if no change is desired. If the alignment value is
125changed from the previous setting, the writer's \method{new_alignment()}
126method is called with the \var{align} value.
Fred Drakee83b30d1996-10-08 21:53:33 +0000127\end{funcdesc}
128
129\begin{funcdesc}{pop_alignment}{}
130Restore the previous alignment.
131\end{funcdesc}
132
Fred Drake3b5da761998-03-12 15:33:05 +0000133\begin{funcdesc}{push_font}{\code{(}size, italic, bold, teletype\code{)}}
Fred Drakee83b30d1996-10-08 21:53:33 +0000134Change some or all font properties of the writer object. Properties
Fred Drake3b5da761998-03-12 15:33:05 +0000135which are not set to \constant{AS_IS} are set to the values passed in
Fred Drakee83b30d1996-10-08 21:53:33 +0000136while others are maintained at their current settings. The writer's
Fred Drake3b5da761998-03-12 15:33:05 +0000137\method{new_font()} method is called with the fully resolved font
Fred Drakee83b30d1996-10-08 21:53:33 +0000138specification.
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
Fred Drake3b5da761998-03-12 15:33:05 +0000147the logical tag \var{margin} with the new indentation. The initial
Fred Drakee83b30d1996-10-08 21:53:33 +0000148margin level is \code{0}. Changed values of the logical tag must be
Fred Drake3b5da761998-03-12 15:33:05 +0000149true values; false values other than \constant{AS_IS} are not
150sufficient to change the margin.
Fred Drakee83b30d1996-10-08 21:53:33 +0000151\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
Fred Drake3b5da761998-03-12 15:33:05 +0000160entire stack, including \constant{AS_IS} values, is passed to the
161writer's \method{new_styles()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +0000162\end{funcdesc}
163
164\begin{funcdesc}{pop_style}{\optional{n\code{ = 1}}}
Fred Drake3b5da761998-03-12 15:33:05 +0000165Pop the last \var{n} style specifications passed to
166\method{push_style()}. A tuple representing the revised stack,
167including \constant{AS_IS} values, is passed to the writer's
168\method{new_styles()} method.
Fred Drakee83b30d1996-10-08 21:53:33 +0000169\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
Fred Drake3b5da761998-03-12 15:33:05 +0000178directly. The optional \var{flag} argument can be set to false if
Fred Drakee83b30d1996-10-08 21:53:33 +0000179the 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
Fred Drake3b5da761998-03-12 15:33:05 +0000190\begin{classdesc}{NullFormatter}{\optional{writer}}
191A formatter which does nothing. If \var{writer} is omitted, a
192\class{NullWriter} instance is created. No methods of the writer are
193called by \class{NullFormatter} instances. Implementations should
194inherit from this class if implementing a writer interface but don't
195need to inherit any implementation.
196\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000197
Fred Drake3b5da761998-03-12 15:33:05 +0000198\begin{classdesc}{AbstractFormatter}{writer}
Fred Drakee83b30d1996-10-08 21:53:33 +0000199The standard formatter. This implementation has demonstrated wide
200applicability to many writers, and may be used directly in most
Fred Drake8f925951996-10-09 16:13:22 +0000201circumstances. It has been used to implement a full-featured
202world-wide web browser.
Fred Drake3b5da761998-03-12 15:33:05 +0000203\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000204
205
206
207\subsection{The Writer Interface}
208
209Interfaces to create writers are dependent on the specific writer
210class being instantiated. The interfaces described below are the
211required interfaces which all writers must support once initialized.
Fred Drake3b5da761998-03-12 15:33:05 +0000212Note that while most applications can use the
213\class{AbstractFormatter} class as a formatter, the writer must
214typically be provided by the application.
Fred Drakee83b30d1996-10-08 21:53:33 +0000215
Fred Drake7bf5e081998-03-16 05:07:04 +0000216\setindexsubitem{(writer method)}
Fred Drake8f925951996-10-09 16:13:22 +0000217
Fred Drake591bbb11996-12-31 20:51:42 +0000218\begin{funcdesc}{flush}{}
219Flush any buffered output or device control events.
220\end{funcdesc}
221
Fred Drakee83b30d1996-10-08 21:53:33 +0000222\begin{funcdesc}{new_alignment}{align}
Fred Drake3b5da761998-03-12 15:33:05 +0000223Set the alignment style. The \var{align} value can be any object,
Fred Drakee83b30d1996-10-08 21:53:33 +0000224but by convention is a string or \code{None}, where \code{None}
225indicates that the writer's ``preferred'' alignment should be used.
Fred Drake3b5da761998-03-12 15:33:05 +0000226Conventional \var{align} values are \code{'left'}, \code{'center'},
Fred Drakee83b30d1996-10-08 21:53:33 +0000227\code{'right'}, and \code{'justify'}.
228\end{funcdesc}
229
230\begin{funcdesc}{new_font}{font}
Fred Drake3b5da761998-03-12 15:33:05 +0000231Set the font style. The value of \var{font} will be \code{None},
Fred Drakee83b30d1996-10-08 21:53:33 +0000232indicating that the device's default font should be used, or a tuple
Fred Drake3b5da761998-03-12 15:33:05 +0000233of the form \code{(}\var{size}, \var{italic}, \var{bold},
234\var{teletype}\code{)}. Size will be a string indicating the size of
235font that should be used; specific strings and their interpretation
236must be defined by the application. The \var{italic}, \var{bold}, and
237\var{teletype} values are boolean indicators specifying which of those
238font attributes should be used.
Fred Drakee83b30d1996-10-08 21:53:33 +0000239\end{funcdesc}
240
241\begin{funcdesc}{new_margin}{margin, level}
Fred Drake3b5da761998-03-12 15:33:05 +0000242Set the margin level to the integer \var{level} and the logical tag
243to \var{margin}. Interpretation of the logical tag is at the
Fred Drakee83b30d1996-10-08 21:53:33 +0000244writer's discretion; the only restriction on the value of the logical
245tag is that it not be a false value for non-zero values of
Fred Drake3b5da761998-03-12 15:33:05 +0000246\var{level}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000247\end{funcdesc}
248
249\begin{funcdesc}{new_spacing}{spacing}
Fred Drake3b5da761998-03-12 15:33:05 +0000250Set the spacing style to \var{spacing}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000251\end{funcdesc}
252
253\begin{funcdesc}{new_styles}{styles}
Fred Drake3b5da761998-03-12 15:33:05 +0000254Set additional styles. The \var{styles} value is a tuple of
255arbitrary values; the value \constant{AS_IS} should be ignored. The
256\var{styles} tuple may be interpreted either as a set or as a stack
Fred Drakee83b30d1996-10-08 21:53:33 +0000257depending on the requirements of the application and writer
258implementation.
259\end{funcdesc}
260
261\begin{funcdesc}{send_line_break}{}
262Break the current line.
263\end{funcdesc}
264
265\begin{funcdesc}{send_paragraph}{blankline}
Fred Drake3b5da761998-03-12 15:33:05 +0000266Produce a paragraph separation of at least \var{blankline} blank
267lines, or the equivelent. The \var{blankline} value will be an
Fred Drakee83b30d1996-10-08 21:53:33 +0000268integer.
269\end{funcdesc}
270
271\begin{funcdesc}{send_hor_rule}{*args\, **kw}
272Display a horizontal rule on the output device. The arguments to this
273method are entirely application- and writer-specific, and should be
274interpreted with care. The method implementation may assume that a
Fred Drake3b5da761998-03-12 15:33:05 +0000275line break has already been issued via \method{send_line_break()}.
Fred Drakee83b30d1996-10-08 21:53:33 +0000276\end{funcdesc}
277
278\begin{funcdesc}{send_flowing_data}{data}
279Output character data which may be word-wrapped and re-flowed as
280needed. Within any sequence of calls to this method, the writer may
281assume that spans of multiple whitespace characters have been
282collapsed to single space characters.
283\end{funcdesc}
284
285\begin{funcdesc}{send_literal_data}{data}
286Output character data which has already been formatted
287for display. Generally, this should be interpreted to mean that line
288breaks indicated by newline characters should be preserved and no new
289line breaks should be introduced. The data may contain embedded
290newline and tab characters, unlike data provided to the
Fred Drake3b5da761998-03-12 15:33:05 +0000291\method{send_formatted_data()} interface.
Fred Drakee83b30d1996-10-08 21:53:33 +0000292\end{funcdesc}
293
294\begin{funcdesc}{send_label_data}{data}
Fred Drake3b5da761998-03-12 15:33:05 +0000295Set \var{data} to the left of the current left margin, if possible.
296The value of \var{data} is not restricted; treatment of non-string
Fred Drakee83b30d1996-10-08 21:53:33 +0000297values is entirely application- and writer-dependent. This method
298will only be called at the beginning of a line.
299\end{funcdesc}
300
301
302\subsection{Writer Implementations}
303
Fred Drake8f925951996-10-09 16:13:22 +0000304Three implementations of the writer object interface are provided as
305examples by this module. Most applications will need to derive new
Fred Drake3b5da761998-03-12 15:33:05 +0000306writer classes from the \class{NullWriter} class.
Fred Drake8f925951996-10-09 16:13:22 +0000307
Fred Drake7bf5e081998-03-16 05:07:04 +0000308\begin{classdesc}{NullWriter}{}
Fred Drakee83b30d1996-10-08 21:53:33 +0000309A writer which only provides the interface definition; no actions are
310taken on any methods. This should be the base class for all writers
311which do not need to inherit any implementation methods.
Fred Drake7bf5e081998-03-16 05:07:04 +0000312\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000313
Fred Drake7bf5e081998-03-16 05:07:04 +0000314\begin{classdesc}{AbstractWriter}{}
Fred Drakee83b30d1996-10-08 21:53:33 +0000315A writer which can be used in debugging formatters, but not much
Fred Drake5081b221998-01-15 05:49:00 +0000316else. Each method simply announces itself by printing its name and
Fred Drakee83b30d1996-10-08 21:53:33 +0000317arguments on standard output.
Fred Drake7bf5e081998-03-16 05:07:04 +0000318\end{classdesc}
Fred Drakee83b30d1996-10-08 21:53:33 +0000319
Fred Drake7bf5e081998-03-16 05:07:04 +0000320\begin{classdesc}{DumbWriter}{\optional{file\optional{\, maxcol\code{ = 72}}}}
Fred Drakee83b30d1996-10-08 21:53:33 +0000321Simple writer class which writes output on the file object passed in
Fred Drake3b5da761998-03-12 15:33:05 +0000322as \var{file} or, if \var{file} is omitted, on standard output. The
Fred Drakee83b30d1996-10-08 21:53:33 +0000323output is simply word-wrapped to the number of columns specified by
Fred Drake3b5da761998-03-12 15:33:05 +0000324\var{maxcol}. This class is suitable for reflowing a sequence of
Fred Drakee83b30d1996-10-08 21:53:33 +0000325paragraphs.
Fred Drake7bf5e081998-03-16 05:07:04 +0000326\end{classdesc}