# HG changeset patch # User Paul Boddie # Date 1342391772 -7200 # Node ID 69590f054c12844145d59b976888eb91ad6317dd # Parent bb1e664a32b9aa5c2a1868338881ceba7e7a7d1f Added a write parameter to the formatTable function for configurable output. Added formatting according to content type, supporting an extended parser API. diff -r bb1e664a32b9 -r 69590f054c12 ImprovedTableParser.py --- a/ImprovedTableParser.py Fri Jul 13 01:13:08 2012 +0200 +++ b/ImprovedTableParser.py Mon Jul 16 00:36:12 2012 +0200 @@ -401,17 +401,20 @@ return 0 -def write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, start=0): +def write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, start=0, write=None): """ Using the 'request', write a sort control for the given 'columnnumber' in the collection of 'columns', using the existing 'sort_columns' and 'column_types' to construct labels and links that modify the sort criteria, and using the given 'table_name' to parameterise the links. + + If the 'write' parameter is specified, use it to write output; otherwise, + write output using the request. """ fmt = request.formatter - write = request.write + write = write or request.write _ = request.getText write(fmt.div(1, css_class="sortcolumns")) @@ -556,11 +559,14 @@ # Common formatting functions. -def formatTable(text, request, fmt, attrs=None): +def formatTable(text, request, fmt, attrs=None, write=None): """ Format the given 'text' using the specified 'request' and formatter 'fmt'. The optional 'attrs' can be used to control the presentation of the table. + + If the 'write' parameter is specified, use it to write output; otherwise, + write output using the request. """ # Parse the table region. @@ -625,7 +631,7 @@ # Write the table. - write = request.write + write = write or request.write write(fmt.table(1, table_attrs)) for rownumber, (row_attrs, columns) in enumerate(table): @@ -668,7 +674,7 @@ # Add sorting controls, if appropriate. if sortable_heading: - write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name) + write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, write=write) write(fmt.div(0)) write(fmt.table_cell(0)) @@ -677,4 +683,27 @@ write(fmt.table(0)) +def formatTableForOutputType(text, request, mimetype, attrs=None, write=None): + + """ + Format the given 'text' using the specified 'request' for the given output + 'mimetype'. + + The optional 'attrs' can be used to control the presentation of the table. + + If the 'write' parameter is specified, use it to write output; otherwise, + write output using the request. + """ + + write = write or request.write + + if mimetype == "text/html": + write('') + write('') + fmt = request.html_formatter + fmt.setPage(request.page) + formatTable(text, request, fmt, attrs, write) + write('') + write('') + # vim: tabstop=4 expandtab shiftwidth=4 diff -r bb1e664a32b9 -r 69590f054c12 parsers/table.py --- a/parsers/table.py Fri Jul 13 01:13:08 2012 +0200 +++ b/parsers/table.py Mon Jul 16 00:36:12 2012 +0200 @@ -19,6 +19,10 @@ Dependencies = Dependencies extensions = [] + # Output content types preferred by this parser. + + output_mimetypes = ["text/html"] + def __init__(self, raw, request, **kw): """ @@ -30,10 +34,33 @@ self.request = request self.attrs = parseAttributes(kw.get("format_args", ""), False) - def format(self, fmt): + def format(self, fmt, write=None): + + """ + Format a table using the given formatter 'fmt'. If the 'write' parameter + is specified, use it to write output; otherwise, write output using the + request. + """ + + formatTable(self.raw, self.request, fmt, self.attrs, write=write) + + # Extra API methods. + + def formatForOutputType(self, mimetype, write=None): - "Format a table using the given formatter 'fmt'." + """ + Format a table for the given 'mimetype'. If the 'write' parameter is + specified, use it to write output; otherwise, write output using the + request. + """ - formatTable(self.raw, self.request, fmt, self.attrs) + formatTableForOutputType(self.raw, self.request, mimetype, self.attrs, write=write) + + # Class methods. + + def getOutputTypes(self): + return self.output_mimetypes + + getOutputTypes = classmethod(getOutputTypes) # vim: tabstop=4 expandtab shiftwidth=4