329 lines · 13.7 KB · TeX
RawDownload
\section{\module{formatter} ---         Generic output formatting} \declaremodule{standard}{formatter}\modulesynopsis{Generic output formatter and device interface.}   This module supports two interface definitions, each with multipleimplementations.  The \emph{formatter} interface is used by the\class{HTMLParser} class of the \refmodule{htmllib} module, and the\emph{writer} interface is required by the formatter interface.\withsubitem{(class in htmllib)}{\ttindex{HTMLParser}} Formatter objects transform an abstract flow of formatting events intospecific output events on writer objects.  Formatters manage severalstack structures to allow various properties of a writer object to bechanged and restored; writers need not be able to handle relativechanges nor any sort of ``change back'' operation.  Specific writerproperties which may be controlled via formatter objects arehorizontal alignment, font, and left margin indentations.  A mechanismis provided which supports providing arbitrary, non-exclusive stylesettings to a writer as well.  Additional interfaces facilitateformatting events which are not reversible, such as paragraphseparation. Writer objects encapsulate device interfaces.  Abstract devices, suchas file formats, are supported as well as physical devices.  Theprovided implementations all work with abstract devices.  Theinterface makes available mechanisms for setting the properties whichformatter objects manage and inserting data into the output.  \subsection{The Formatter Interface \label{formatter-interface}} Interfaces to create formatters are dependent on the specificformatter class being instantiated.  The interfaces described beloware the required interfaces which all formatters must support onceinitialized. One data element is defined at the module level:  \begin{datadesc}{AS_IS}Value which can be used in the font specification passed to the\code{push_font()} method described below, or as the new value to anyother \code{push_\var{property}()} method.  Pushing the \code{AS_IS}value allows the corresponding \code{pop_\var{property}()} method tobe called without having to track whether the property was changed.\end{datadesc} The following attributes are defined for formatter instance objects:  \begin{memberdesc}[formatter]{writer}The writer instance with which the formatter interacts.\end{memberdesc}  \begin{methoddesc}[formatter]{end_paragraph}{blanklines}Close any open paragraphs and insert at least \var{blanklines}before the next paragraph.\end{methoddesc} \begin{methoddesc}[formatter]{add_line_break}{}Add a hard line break if one does not already exist.  This does notbreak the logical paragraph.\end{methoddesc} \begin{methoddesc}[formatter]{add_hor_rule}{*args, **kw}Insert a horizontal rule in the output.  A hard break is inserted ifthere is data in the current paragraph, but the logical paragraph isnot broken.  The arguments and keywords are passed on to the writer's\method{send_line_break()} method.\end{methoddesc} \begin{methoddesc}[formatter]{add_flowing_data}{data}Provide data which should be formatted with collapsed whitespace.Whitespace from preceding and successive calls to\method{add_flowing_data()} is considered as well when the whitespacecollapse is performed.  The data which is passed to this method isexpected to be word-wrapped by the output device.  Note that anyword-wrapping still must be performed by the writer object due to theneed to rely on device and font information.\end{methoddesc} \begin{methoddesc}[formatter]{add_literal_data}{data}Provide data which should be passed to the writer unchanged.Whitespace, including newline and tab characters, are considered legalin the value of \var{data}.  \end{methoddesc} \begin{methoddesc}[formatter]{add_label_data}{format, counter}Insert a label which should be placed to the left of the current leftmargin.  This should be used for constructing bulleted or numberedlists.  If the \var{format} value is a string, it is interpreted as aformat specification for \var{counter}, which should be an integer.The result of this formatting becomes the value of the label; if\var{format} is not a string it is used as the label value directly.The label value is passed as the only argument to the writer's\method{send_label_data()} method.  Interpretation of non-string labelvalues is dependent on the associated writer. Format specifications are strings which, in combination with a countervalue, are used to compute label values.  Each character in the formatstring is copied to the label value, with some characters recognizedto indicate a transform on the counter value.  Specifically, thecharacter \character{1} represents the counter value formatter as anArabic number, the characters \character{A} and \character{a}represent alphabetic representations of the counter value in upper andlower case, respectively, and \character{I} and \character{i}represent the counter value in Roman numerals, in upper and lowercase.  Note that the alphabetic and roman transforms require that thecounter value be greater than zero.\end{methoddesc} \begin{methoddesc}[formatter]{flush_softspace}{}Send any pending whitespace buffered from a previous call to\method{add_flowing_data()} to the associated writer object.  Thisshould be called before any direct manipulation of the writer object.\end{methoddesc} \begin{methoddesc}[formatter]{push_alignment}{align}Push a new alignment setting onto the alignment stack.  This may be\constant{AS_IS} if no change is desired.  If the alignment value ischanged from the previous setting, the writer's \method{new_alignment()}method is called with the \var{align} value.\end{methoddesc} \begin{methoddesc}[formatter]{pop_alignment}{}Restore the previous alignment.\end{methoddesc} \begin{methoddesc}[formatter]{push_font}{\code{(}size, italic, bold, teletype\code{)}}Change some or all font properties of the writer object.  Propertieswhich are not set to \constant{AS_IS} are set to the values passed inwhile others are maintained at their current settings.  The writer's\method{new_font()} method is called with the fully resolved fontspecification.\end{methoddesc} \begin{methoddesc}[formatter]{pop_font}{}Restore the previous font.\end{methoddesc} \begin{methoddesc}[formatter]{push_margin}{margin}Increase the number of left margin indentations by one, associatingthe logical tag \var{margin} with the new indentation.  The initialmargin level is \code{0}.  Changed values of the logical tag must betrue values; false values other than \constant{AS_IS} are notsufficient to change the margin.\end{methoddesc} \begin{methoddesc}[formatter]{pop_margin}{}Restore the previous margin.\end{methoddesc} \begin{methoddesc}[formatter]{push_style}{*styles}Push any number of arbitrary style specifications.  All styles arepushed onto the styles stack in order.  A tuple representing theentire stack, including \constant{AS_IS} values, is passed to thewriter's \method{new_styles()} method.\end{methoddesc} \begin{methoddesc}[formatter]{pop_style}{\optional{n\code{ = 1}}}Pop the last \var{n} style specifications passed to\method{push_style()}.  A tuple representing the revised stack,including \constant{AS_IS} values, is passed to the writer's\method{new_styles()} method.\end{methoddesc} \begin{methoddesc}[formatter]{set_spacing}{spacing}Set the spacing style for the writer.\end{methoddesc} \begin{methoddesc}[formatter]{assert_line_data}{\optional{flag\code{ = 1}}}Inform the formatter that data has been added to the current paragraphout-of-band.  This should be used when the writer has been manipulateddirectly.  The optional \var{flag} argument can be set to false ifthe writer manipulations produced a hard line break at the end of theoutput.\end{methoddesc}  \subsection{Formatter Implementations \label{formatter-impls}} Two implementations of formatter objects are provided by this module.Most applications may use one of these classes without modification orsubclassing. \begin{classdesc}{NullFormatter}{\optional{writer}}A formatter which does nothing.  If \var{writer} is omitted, a\class{NullWriter} instance is created.  No methods of the writer arecalled by \class{NullFormatter} instances.  Implementations shouldinherit from this class if implementing a writer interface but don'tneed to inherit any implementation.\end{classdesc} \begin{classdesc}{AbstractFormatter}{writer}The standard formatter.  This implementation has demonstrated wideapplicability to many writers, and may be used directly in mostcircumstances.  It has been used to implement a full-featuredWorld Wide Web browser.\end{classdesc}   \subsection{The Writer Interface \label{writer-interface}} Interfaces to create writers are dependent on the specific writerclass being instantiated.  The interfaces described below are therequired interfaces which all writers must support once initialized.Note that while most applications can use the\class{AbstractFormatter} class as a formatter, the writer musttypically be provided by the application.  \begin{methoddesc}[writer]{flush}{}Flush any buffered output or device control events.\end{methoddesc} \begin{methoddesc}[writer]{new_alignment}{align}Set the alignment style.  The \var{align} value can be any object,but by convention is a string or \code{None}, where \code{None}indicates that the writer's ``preferred'' alignment should be used.Conventional \var{align} values are \code{'left'}, \code{'center'},\code{'right'}, and \code{'justify'}.\end{methoddesc} \begin{methoddesc}[writer]{new_font}{font}Set the font style.  The value of \var{font} will be \code{None},indicating that the device's default font should be used, or a tupleof the form \code{(}\var{size}, \var{italic}, \var{bold},\var{teletype}\code{)}.  Size will be a string indicating the size offont that should be used; specific strings and their interpretationmust be defined by the application.  The \var{italic}, \var{bold}, and\var{teletype} values are Boolean values specifying which of thosefont attributes should be used.\end{methoddesc} \begin{methoddesc}[writer]{new_margin}{margin, level}Set the margin level to the integer \var{level} and the logical tagto \var{margin}.  Interpretation of the logical tag is at thewriter's discretion; the only restriction on the value of the logicaltag is that it not be a false value for non-zero values of\var{level}.\end{methoddesc} \begin{methoddesc}[writer]{new_spacing}{spacing}Set the spacing style to \var{spacing}.\end{methoddesc} \begin{methoddesc}[writer]{new_styles}{styles}Set additional styles.  The \var{styles} value is a tuple ofarbitrary values; the value \constant{AS_IS} should be ignored.  The\var{styles} tuple may be interpreted either as a set or as a stackdepending on the requirements of the application and writerimplementation.\end{methoddesc} \begin{methoddesc}[writer]{send_line_break}{}Break the current line.\end{methoddesc} \begin{methoddesc}[writer]{send_paragraph}{blankline}Produce a paragraph separation of at least \var{blankline} blanklines, or the equivalent.  The \var{blankline} value will be aninteger.  Note that the implementation will receive a call to\method{send_line_break()} before this call if a line break is needed; this method should not include ending the last line of the paragraph.It is only responsible for vertical spacing between paragraphs.\end{methoddesc} \begin{methoddesc}[writer]{send_hor_rule}{*args, **kw}Display a horizontal rule on the output device.  The arguments to thismethod are entirely application- and writer-specific, and should beinterpreted with care.  The method implementation may assume that aline break has already been issued via \method{send_line_break()}.\end{methoddesc} \begin{methoddesc}[writer]{send_flowing_data}{data}Output character data which may be word-wrapped and re-flowed asneeded.  Within any sequence of calls to this method, the writer mayassume that spans of multiple whitespace characters have beencollapsed to single space characters.\end{methoddesc} \begin{methoddesc}[writer]{send_literal_data}{data}Output character data which has already been formattedfor display.  Generally, this should be interpreted to mean that linebreaks indicated by newline characters should be preserved and no newline breaks should be introduced.  The data may contain embeddednewline and tab characters, unlike data provided to the\method{send_formatted_data()} interface.\end{methoddesc} \begin{methoddesc}[writer]{send_label_data}{data}Set \var{data} to the left of the current left margin, if possible.The value of \var{data} is not restricted; treatment of non-stringvalues is entirely application- and writer-dependent.  This methodwill only be called at the beginning of a line.\end{methoddesc}  \subsection{Writer Implementations \label{writer-impls}} Three implementations of the writer object interface are provided asexamples by this module.  Most applications will need to derive newwriter classes from the \class{NullWriter} class. \begin{classdesc}{NullWriter}{}A writer which only provides the interface definition; no actions aretaken on any methods.  This should be the base class for all writerswhich do not need to inherit any implementation methods.\end{classdesc} \begin{classdesc}{AbstractWriter}{}A writer which can be used in debugging formatters, but not muchelse.  Each method simply announces itself by printing its name andarguments on standard output.\end{classdesc} \begin{classdesc}{DumbWriter}{\optional{file\optional{, maxcol\code{ = 72}}}}Simple writer class which writes output on the file object passed inas \var{file} or, if \var{file} is omitted, on standard output.  Theoutput is simply word-wrapped to the number of columns specified by\var{maxcol}.  This class is suitable for reflowing a sequence ofparagraphs.\end{classdesc}