1 """Generic output formatting.
3 Formatter objects transform an abstract flow of formatting events into
4 specific output events on writer objects. Formatters manage several stack
5 structures to allow various properties of a writer object to be changed and
6 restored; writers need not be able to handle relative changes nor any sort
7 of ``change back'' operation. Specific writer properties which may be
8 controlled via formatter objects are horizontal alignment, font, and left
9 margin indentations. A mechanism is provided which supports providing
10 arbitrary, non-exclusive style settings to a writer as well. Additional
11 interfaces facilitate formatting events which are not reversible, such as
14 Writer objects encapsulate device interfaces. Abstract devices, such as
15 file formats, are supported as well as physical devices. The provided
16 implementations all work with abstract devices. The interface makes
17 available mechanisms for setting the properties which formatter objects
18 manage and inserting data into the output.
28 """A formatter which does nothing.
30 If the writer parameter is omitted, a NullWriter instance is created.
31 No methods of the writer are called by NullFormatter instances.
33 Implementations should inherit from this class if implementing a writer
34 interface but don't need to inherit any implementation.
38 def __init__(self
, writer
=None):
42 def end_paragraph(self
, blankline
): pass
43 def add_line_break(self
): pass
44 def add_hor_rule(self
, *args
, **kw
): pass
45 def add_label_data(self
, format
, counter
, blankline
=None): pass
46 def add_flowing_data(self
, data
): pass
47 def add_literal_data(self
, data
): pass
48 def flush_softspace(self
): pass
49 def push_alignment(self
, align
): pass
50 def pop_alignment(self
): pass
51 def push_font(self
, x
): pass
52 def pop_font(self
): pass
53 def push_margin(self
, margin
): pass
54 def pop_margin(self
): pass
55 def set_spacing(self
, spacing
): pass
56 def push_style(self
, *styles
): pass
57 def pop_style(self
, n
=1): pass
58 def assert_line_data(self
, flag
=1): pass
61 class AbstractFormatter
:
62 """The standard formatter.
64 This implementation has demonstrated wide applicability to many writers,
65 and may be used directly in most circumstances. It has been used to
66 implement a full-featured World Wide Web browser.
70 # Space handling policy: blank spaces at the boundary between elements
71 # are handled by the outermost context. "Literal" data is not checked
72 # to determine context, so spaces in literal data are handled directly
73 # in all circumstances.
75 def __init__(self
, writer
):
76 self
.writer
= writer
# Output device
77 self
.align
= None # Current alignment
78 self
.align_stack
= [] # Alignment stack
79 self
.font_stack
= [] # Font state
80 self
.margin_stack
= [] # Margin state
81 self
.spacing
= None # Vertical spacing state
82 self
.style_stack
= [] # Other state, e.g. color
83 self
.nospace
= 1 # Should leading space be suppressed
84 self
.softspace
= 0 # Should a space be inserted
85 self
.para_end
= 1 # Just ended a paragraph
86 self
.parskip
= 0 # Skipped space between paragraphs?
87 self
.hard_break
= 1 # Have a hard break
90 def end_paragraph(self
, blankline
):
91 if not self
.hard_break
:
92 self
.writer
.send_line_break()
94 if self
.parskip
< blankline
and not self
.have_label
:
95 self
.writer
.send_paragraph(blankline
- self
.parskip
)
96 self
.parskip
= blankline
98 self
.hard_break
= self
.nospace
= self
.para_end
= 1
101 def add_line_break(self
):
102 if not (self
.hard_break
or self
.para_end
):
103 self
.writer
.send_line_break()
104 self
.have_label
= self
.parskip
= 0
105 self
.hard_break
= self
.nospace
= 1
108 def add_hor_rule(self
, *args
, **kw
):
109 if not self
.hard_break
:
110 self
.writer
.send_line_break()
111 self
.writer
.send_hor_rule(*args
, **kw
)
112 self
.hard_break
= self
.nospace
= 1
113 self
.have_label
= self
.para_end
= self
.softspace
= self
.parskip
= 0
115 def add_label_data(self
, format
, counter
, blankline
= None):
116 if self
.have_label
or not self
.hard_break
:
117 self
.writer
.send_line_break()
118 if not self
.para_end
:
119 self
.writer
.send_paragraph((blankline
and 1) or 0)
120 if isinstance(format
, str):
121 self
.writer
.send_label_data(self
.format_counter(format
, counter
))
123 self
.writer
.send_label_data(format
)
124 self
.nospace
= self
.have_label
= self
.hard_break
= self
.para_end
= 1
125 self
.softspace
= self
.parskip
= 0
127 def format_counter(self
, format
, counter
):
131 label
= label
+ ('%d' % counter
)
134 label
= label
+ self
.format_letter(c
, counter
)
137 label
= label
+ self
.format_roman(c
, counter
)
142 def format_letter(self
, case
, counter
):
145 counter
, x
= divmod(counter
-1, 26)
146 # This makes a strong assumption that lowercase letters
147 # and uppercase letters form two contiguous blocks, with
149 s
= chr(ord(case
) + x
)
153 def format_roman(self
, case
, counter
):
154 ones
= ['i', 'x', 'c', 'm']
155 fives
= ['v', 'l', 'd']
157 # This will die of IndexError when counter is too big
159 counter
, x
= divmod(counter
, 10)
161 label
= ones
[index
] + ones
[index
+1] + label
163 label
= ones
[index
] + fives
[index
] + label
170 s
= s
+ ones
[index
]*x
177 def add_flowing_data(self
, data
):
179 prespace
= data
[:1].isspace()
180 postspace
= data
[-1:].isspace()
181 data
= " ".join(data
.split())
182 if self
.nospace
and not data
:
184 elif prespace
or self
.softspace
:
192 self
.hard_break
= self
.nospace
= self
.para_end
= \
193 self
.parskip
= self
.have_label
= 0
194 self
.softspace
= postspace
195 self
.writer
.send_flowing_data(data
)
197 def add_literal_data(self
, data
):
200 self
.writer
.send_flowing_data(" ")
201 self
.hard_break
= data
[-1:] == '\n'
202 self
.nospace
= self
.para_end
= self
.softspace
= \
203 self
.parskip
= self
.have_label
= 0
204 self
.writer
.send_literal_data(data
)
206 def flush_softspace(self
):
208 self
.hard_break
= self
.para_end
= self
.parskip
= \
209 self
.have_label
= self
.softspace
= 0
211 self
.writer
.send_flowing_data(' ')
213 def push_alignment(self
, align
):
214 if align
and align
!= self
.align
:
215 self
.writer
.new_alignment(align
)
217 self
.align_stack
.append(align
)
219 self
.align_stack
.append(self
.align
)
221 def pop_alignment(self
):
223 del self
.align_stack
[-1]
225 self
.align
= align
= self
.align_stack
[-1]
226 self
.writer
.new_alignment(align
)
229 self
.writer
.new_alignment(None)
231 def push_font(self
, font
):
232 size
, i
, b
, tt
= font
234 self
.hard_break
= self
.para_end
= self
.softspace
= 0
236 self
.writer
.send_flowing_data(' ')
238 csize
, ci
, cb
, ctt
= self
.font_stack
[-1]
239 if size
is AS_IS
: size
= csize
240 if i
is AS_IS
: i
= ci
241 if b
is AS_IS
: b
= cb
242 if tt
is AS_IS
: tt
= ctt
243 font
= (size
, i
, b
, tt
)
244 self
.font_stack
.append(font
)
245 self
.writer
.new_font(font
)
249 del self
.font_stack
[-1]
251 font
= self
.font_stack
[-1]
254 self
.writer
.new_font(font
)
256 def push_margin(self
, margin
):
257 self
.margin_stack
.append(margin
)
258 fstack
= filter(None, self
.margin_stack
)
259 if not margin
and fstack
:
261 self
.writer
.new_margin(margin
, len(fstack
))
263 def pop_margin(self
):
264 if self
.margin_stack
:
265 del self
.margin_stack
[-1]
266 fstack
= filter(None, self
.margin_stack
)
271 self
.writer
.new_margin(margin
, len(fstack
))
273 def set_spacing(self
, spacing
):
274 self
.spacing
= spacing
275 self
.writer
.new_spacing(spacing
)
277 def push_style(self
, *styles
):
279 self
.hard_break
= self
.para_end
= self
.softspace
= 0
281 self
.writer
.send_flowing_data(' ')
283 self
.style_stack
.append(style
)
284 self
.writer
.new_styles(tuple(self
.style_stack
))
286 def pop_style(self
, n
=1):
287 del self
.style_stack
[-n
:]
288 self
.writer
.new_styles(tuple(self
.style_stack
))
290 def assert_line_data(self
, flag
=1):
291 self
.nospace
= self
.hard_break
= not flag
292 self
.para_end
= self
.parskip
= self
.have_label
= 0
296 """Minimal writer interface to use in testing & inheritance.
298 A writer which only provides the interface definition; no actions are
299 taken on any methods. This should be the base class for all writers
300 which do not need to inherit any implementation methods.
303 def __init__(self
): pass
304 def flush(self
): pass
305 def new_alignment(self
, align
): pass
306 def new_font(self
, font
): pass
307 def new_margin(self
, margin
, level
): pass
308 def new_spacing(self
, spacing
): pass
309 def new_styles(self
, styles
): pass
310 def send_paragraph(self
, blankline
): pass
311 def send_line_break(self
): pass
312 def send_hor_rule(self
, *args
, **kw
): pass
313 def send_label_data(self
, data
): pass
314 def send_flowing_data(self
, data
): pass
315 def send_literal_data(self
, data
): pass
318 class AbstractWriter(NullWriter
):
319 """A writer which can be used in debugging formatters, but not much else.
321 Each method simply announces itself by printing its name and
322 arguments on standard output.
326 def new_alignment(self
, align
):
327 print "new_alignment(%r)" % (align
,)
329 def new_font(self
, font
):
330 print "new_font(%r)" % (font
,)
332 def new_margin(self
, margin
, level
):
333 print "new_margin(%r, %d)" % (margin
, level
)
335 def new_spacing(self
, spacing
):
336 print "new_spacing(%r)" % (spacing
,)
338 def new_styles(self
, styles
):
339 print "new_styles(%r)" % (styles
,)
341 def send_paragraph(self
, blankline
):
342 print "send_paragraph(%r)" % (blankline
,)
344 def send_line_break(self
):
345 print "send_line_break()"
347 def send_hor_rule(self
, *args
, **kw
):
348 print "send_hor_rule()"
350 def send_label_data(self
, data
):
351 print "send_label_data(%r)" % (data
,)
353 def send_flowing_data(self
, data
):
354 print "send_flowing_data(%r)" % (data
,)
356 def send_literal_data(self
, data
):
357 print "send_literal_data(%r)" % (data
,)
360 class DumbWriter(NullWriter
):
361 """Simple writer class which writes output on the file object passed in
362 as the file parameter or, if file is omitted, on standard output. The
363 output is simply word-wrapped to the number of columns specified by
364 the maxcol parameter. This class is suitable for reflowing a sequence
369 def __init__(self
, file=None, maxcol
=72):
370 self
.file = file or sys
.stdout
372 NullWriter
.__init
__(self
)
379 def send_paragraph(self
, blankline
):
380 self
.file.write('\n'*blankline
)
384 def send_line_break(self
):
385 self
.file.write('\n')
389 def send_hor_rule(self
, *args
, **kw
):
390 self
.file.write('\n')
391 self
.file.write('-'*self
.maxcol
)
392 self
.file.write('\n')
396 def send_literal_data(self
, data
):
397 self
.file.write(data
)
402 data
= data
.expandtabs()
403 self
.col
= self
.col
+ len(data
)
406 def send_flowing_data(self
, data
):
408 atbreak
= self
.atbreak
or data
[0].isspace()
411 write
= self
.file.write
412 for word
in data
.split():
414 if col
+ len(word
) >= maxcol
:
421 col
= col
+ len(word
)
424 self
.atbreak
= data
[-1].isspace()
427 def test(file = None):
429 f
= AbstractFormatter(w
)
433 fp
= open(sys
.argv
[1])
440 f
.add_flowing_data(line
)
444 if __name__
== '__main__':