/Doc/library/formatter.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 350 lines · 215 code · 135 blank · 0 comment · 0 complexity · 72c4c3a7942ddd484b97f1374ef9de9e MD5 · raw file

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