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