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