1
 3­    
:mod:`formatter` --- Generic output formatting
==============================================

.. module:: formatter
   :synopsis: Generic output formatter and device interface.


.. index:: single: HTMLParser (class in htmllib)

This module supports two interface definitions, each with multiple
implementations.  The *formatter* interface is used by the :class:`HTMLParser`
class of the :mod:`htmllib` module, and the *writer* interface is required by
the formatter interface.

Formatter objects transform an abstract flow of formatting events into specific
output events on writer objects.  Formatters manage several stack structures to
allow various properties of a writer object to be changed and restored; writers
need not be able to handle relative changes nor any sort of "change back"
operation.  Specific writer properties which may be controlled via formatter
objects are horizontal alignment, font, and left margin indentations.  A
mechanism is provided which supports providing arbitrary, non-exclusive style
settings to a writer as well.  Additional interfaces facilitate formatting
events which are not reversible, such as paragraph separation.

Writer objects encapsulate device interfaces.  Abstract devices, such as file
formats, are supported as well as physical devices.  The provided
implementations all work with abstract devices.  The interface makes available
mechanisms for setting the properties which formatter objects manage and
inserting data into the output.


.. _formatter-interface:

The Formatter Interface
-----------------------

Interfaces to create formatters are dependent on the specific formatter class
being instantiated.  The interfaces described below are the required interfaces
which all formatters must support once initialized.

One data element is defined at the module level:


.. data:: AS_IS

   Value which can be used in the font specification passed to the ``push_font()``
   method described below, or as the new value to any other ``push_property()``
   method.  Pushing the ``AS_IS`` value allows the corresponding ``pop_property()``
   method to be called without having to track whether the property was changed.

The following attributes are defined for formatter instance objects:


.. attribute:: formatter.writer

   The writer instance with which the formatter interacts.


.. method:: formatter.end_paragraph(blanklines)

   Close any open paragraphs and insert at least *blanklines* before the next
   paragraph.


.. method:: formatter.add_line_break()

   Add a hard line break if one does not already exist.  This does not break the
   logical paragraph.


.. method:: formatter.add_hor_rule(*args, **kw)

   Insert a horizontal rule in the output.  A hard break is inserted if there is
   data in the current paragraph, but the logical paragraph is not broken.  The
   arguments and keywords are passed on to the writer's :meth:`send_line_break`
   method.


.. method:: formatter.add_flowing_data(data)

   Provide data which should be formatted with collapsed whitespace. Whitespace
   from preceding and successive calls to :meth:`add_flowing_data` is considered as
   well when the whitespace collapse is performed.  The data which is passed to
   this method is expected to be word-wrapped by the output device.  Note that any
   word-wrapping still must be performed by the writer object due to the need to
   rely on device and font information.


.. method:: formatter.add_literal_data(data)

   Provide data which should be passed to the writer unchanged. Whitespace,
   including newline and tab characters, are considered legal in the value of
   *data*.


.. method:: formatter.add_label_data(format, counter)

   Insert a label which should be placed to the left of the current left margin.
   This should be used for constructing bulleted or numbered lists.  If the
   *format* value is a string, it is interpreted as a format specification for
   *counter*, which should be an integer. The result of this formatting becomes the
   value of the label; if *format* is not a string it is used as the l