blob: c0ebab4e2872f59ae6ab929bb0bb3d34a4c5477d [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
49\begin{datadesc}{writer}
50The writer instance with which the formatter interacts.
51\end{datadesc}
52
53
54\begin{funcdesc}{end_paragraph}{blanklines}
55Close any open paragraphs and insert at least \code{blanklines}
56before the next paragraph.
57\end{funcdesc}
58
59\begin{funcdesc}{add_line_break}{}
60Add a hard line break if one does not already exist. This does not
61break the logical paragraph.
62\end{funcdesc}
63
64\begin{funcdesc}{add_hor_rule}{*args\, **kw}
65Insert a horizontal rule in the output. A hard break is inserted if
66there is data in the current paragraph, but the logical paragraph is
67not broken. The arguments and keywords are passed on to the writer's
68\code{send_line_break()} method.
69\end{funcdesc}
70
71\begin{funcdesc}{add_flowing_data}{data}
72Provide data which should be formatted with collapsed whitespaces.
73Whitespace from preceeding and successive calls to
74\code{add_flowing_data()} is considered as well when the whitespace
75collapse is performed. The data which is passed to this method is
76expected to be word-wrapped by the output device. Note that any
77word-wrapping still must be performed by the writer object due to the
78need to rely on device and font information.
79\end{funcdesc}
80
81\begin{funcdesc}{add_literal_data}{data}
82Provide data which should be passed to the writer unchanged.
83Whitespace, including newline and tab characters, are considered legal
84in the value of \code{data}.
85\end{funcdesc}
86
87\begin{funcdesc}{add_label_data}{format, counter}
88Insert a label which should be placed to the left of the current left
89margin. This should be used for constructing bulleted or numbered
90lists. If the \code{format} value is a string, it is interpreted as a
91format specification for \code{counter}, which should be an integer.
92The result of this formatting becomes the value of the label; if
93\code{format} is not a string it is used as the label value directly.
94The label value is passed as the only argument to the writer's
95\code{send_label_data()} method. Interpretation of non-string label
96values is dependent on the associated writer.
97
98Format specifications are strings which, in combination with a counter
99value, are used to compute label values. Each character in the format
100string is copied to the label value, with some characters recognized
101to indicate a transform on the counter value. Specifically, the
102character ``\code{1}'' represents the counter value formatter as an
103arabic number, the characters ``\code{A}'' and ``\code{a}'' represent
104alphabetic representations of the counter value in upper and lower
105case, respectively, and ``\code{I}'' and ``\code{i}'' represent the
106counter value in Roman numerals, in upper and lower case. Note that
107the alphabetic and roman transforms require that the counter value be
108greater than zero.
109\end{funcdesc}
110
111\begin{funcdesc}{flush_softspace}{}
112Send any pending whitespace buffered from a previous call to
113\code{add_flowing_data()} to the associated writer object. This
114should be called before any direct manipulation of the writer object.
115\end{funcdesc}
116
117\begin{funcdesc}{push_alignment}{align}
118Push a new alignment setting onto the alignment stack. This may be
119\code{AS_IS} if no change is desired. If the alignment value is
120changed from the previous setting, the writer's \code{new_alignment()}
121method is called with the \code{align} value.
122\end{funcdesc}
123
124\begin{funcdesc}{pop_alignment}{}
125Restore the previous alignment.
126\end{funcdesc}
127
128\begin{funcdesc}{push_font}{(size, italic, bold, teletype)}
129Change some or all font properties of the writer object. Properties
130which are not set to \code{AS_IS} are set to the values passed in
131while others are maintained at their current settings. The writer's
132\code{new_font()} method is called with the fully resolved font
133specification.
134\end{funcdesc}
135
136\begin{funcdesc}{pop_font}{}
137Restore the previous font.
138\end{funcdesc}
139
140\begin{funcdesc}{push_margin}{margin}
141Increase the number of left margin indentations by one, associating
142the logical tag \code{margin} with the new indentation. The initial
143margin level is \code{0}. Changed values of the logical tag must be
144true values; false values other than \code{AS_IS} are not sufficient
145to change the margin.
146\end{funcdesc}
147
148\begin{funcdesc}{pop_margin}{}
149Restore the previous margin.
150\end{funcdesc}
151
152\begin{funcdesc}{push_style}{*styles}
153Push any number of arbitrary style specifications. All styles are
154pushed onto the styles stack in order. A tuple representing the
155entire stack, including \code{AS_IS} values, is passed to the writer's
156\code{new_styles()} method.
157\end{funcdesc}
158
159\begin{funcdesc}{pop_style}{\optional{n\code{ = 1}}}
160Pop the last \code{n} style specifications passed to
161\code{push_style()}. A tuple representing the revised stack,
162including \code{AS_IS} values, is passed to the writer's
163\code{new_styles()} method.
164\end{funcdesc}
165
166\begin{funcdesc}{set_spacing}{spacing}
167Set the spacing style for the writer.
168\end{funcdesc}
169
170\begin{funcdesc}{assert_line_data}{\optional{flag\code{ = 1}}}
171Inform the formatter that data has been added to the current paragraph
172out-of-band. This should be used when the writer has been manipulated
173directly. The optional \code{flag} argument can be set to false if
174the writer manipulations produced a hard line break at the end of the
175output.
176\end{funcdesc}
177
178
179\subsection{Formatter Implementations}
180
181\begin{funcdesc}{NullFormatter}{\optional{writer\code{ = None}}}
182A formatter which does nothing. If \code{writer} is omitted, a
183\code{NullWriter} instance is created. No methods of the writer are
184called by \code{NullWriter} instances.
185\end{funcdesc}
186
187\begin{funcdesc}{AbstractFormatter}{writer}
188The standard formatter. This implementation has demonstrated wide
189applicability to many writers, and may be used directly in most
190circumstances.
191\end{funcdesc}
192
193
194
195\subsection{The Writer Interface}
196
197Interfaces to create writers are dependent on the specific writer
198class being instantiated. The interfaces described below are the
199required interfaces which all writers must support once initialized.
200Note that while most applications can use the \code{AbstractFormatter}
201class as a formatter, the writer must typically be provided by the
202application.
203
204\begin{funcdesc}{new_alignment}{align}
205Set the alignment style. The \code{align} value can be any object,
206but by convention is a string or \code{None}, where \code{None}
207indicates that the writer's ``preferred'' alignment should be used.
208Conventional \code{align} values are \code{'left'}, \code{'center'},
209\code{'right'}, and \code{'justify'}.
210\end{funcdesc}
211
212\begin{funcdesc}{new_font}{font}
213Set the font style. The value of \code{font} will be \code{None},
214indicating that the device's default font should be used, or a tuple
215of the form (\var{size}, \var{italic}, \var{bold}, \var{teletype}).
216Size will be a string indicating the size of font that should be used;
217specific strings and their interpretation must be defined by the
218application. The \var{italic}, \var{bold}, and \var{teletype} values
219are boolean indicators specifying which of those font attributes
220should be used.
221\end{funcdesc}
222
223\begin{funcdesc}{new_margin}{margin, level}
224Set the margin level to the integer \code{level} and the logical tag
225to \code{margin}. Interpretation of the logical tag is at the
226writer's discretion; the only restriction on the value of the logical
227tag is that it not be a false value for non-zero values of
228\code{level}.
229\end{funcdesc}
230
231\begin{funcdesc}{new_spacing}{spacing}
232Set the spacing style to \code{spacing}.
233\end{funcdesc}
234
235\begin{funcdesc}{new_styles}{styles}
236Set additional styles. The \code{styles} value is a tuple of
237arbitrary values; the value \code{AS_IS} should be ignored. The
238\code{styles} tuple may be interpreted either as a set or as a stack
239depending on the requirements of the application and writer
240implementation.
241\end{funcdesc}
242
243\begin{funcdesc}{send_line_break}{}
244Break the current line.
245\end{funcdesc}
246
247\begin{funcdesc}{send_paragraph}{blankline}
248Produce a paragraph separation of at least \code{blankline} blank
249lines, or the equivelent. The \code{blankline} value will be an
250integer.
251\end{funcdesc}
252
253\begin{funcdesc}{send_hor_rule}{*args\, **kw}
254Display a horizontal rule on the output device. The arguments to this
255method are entirely application- and writer-specific, and should be
256interpreted with care. The method implementation may assume that a
257line break has already been issued via \code{send_line_break()}.
258\end{funcdesc}
259
260\begin{funcdesc}{send_flowing_data}{data}
261Output character data which may be word-wrapped and re-flowed as
262needed. Within any sequence of calls to this method, the writer may
263assume that spans of multiple whitespace characters have been
264collapsed to single space characters.
265\end{funcdesc}
266
267\begin{funcdesc}{send_literal_data}{data}
268Output character data which has already been formatted
269for display. Generally, this should be interpreted to mean that line
270breaks indicated by newline characters should be preserved and no new
271line breaks should be introduced. The data may contain embedded
272newline and tab characters, unlike data provided to the
273\code{send_formatted_data()} interface.
274\end{funcdesc}
275
276\begin{funcdesc}{send_label_data}{data}
277Set \code{data} to the left of the current left margin, if possible.
278The value of \code{data} is not restricted; treatment of non-string
279values is entirely application- and writer-dependent. This method
280will only be called at the beginning of a line.
281\end{funcdesc}
282
283
284\subsection{Writer Implementations}
285
286\begin{funcdesc}{NullWriter}{}
287A writer which only provides the interface definition; no actions are
288taken on any methods. This should be the base class for all writers
289which do not need to inherit any implementation methods.
290\end{funcdesc}
291
292\begin{funcdesc}{AbstractWriter}{}
293A writer which can be used in debugging formatters, but not much
294else. Each method simply accounces itself by printing its name and
295arguments on standard output.
296\end{funcdesc}
297
298\begin{funcdesc}{DumbWriter}{\optional{file\code{ = None}\optional{\, maxcol\code{ = 72}}}}
299Simple writer class which writes output on the file object passed in
300as \code{file} or, if \code{file} is omitted, on standard output. The
301output is simply word-wrapped to the number of columns specified by
302\code{maxcol}. This class is suitable for reflowing a sequence of
303paragraphs.
304\end{funcdesc}