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