paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - ImprovedTableParser library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@14 | 6 | @copyright: 2008 MoinMoin:FlorianKrupicka (redirectedOutput code) |
paul@0 | 7 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 8 | """ |
paul@0 | 9 | |
paul@0 | 10 | from MoinMoin import wikiutil |
paul@0 | 11 | from StringIO import StringIO |
paul@5 | 12 | from MoinSupport import * |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@27 | 15 | __version__ = "0.2" |
paul@27 | 16 | |
paul@0 | 17 | # Regular expressions. |
paul@0 | 18 | |
paul@0 | 19 | syntax = { |
paul@0 | 20 | # At start of line: |
paul@18 | 21 | "rows" : (r"^==(?!.*?==$)", re.MULTILINE), # == not-heading |
paul@1 | 22 | |
paul@0 | 23 | # Within text: |
paul@23 | 24 | "markers" : (r"([{]{3,}|[}]{3,})", re.MULTILINE | re.DOTALL), # {{{... or }}}... |
paul@1 | 25 | "columns" : (r"\|\|[ \t]*", 0), # || ws-excl-nl |
paul@1 | 26 | |
paul@0 | 27 | # At start of column text: |
paul@1 | 28 | "column" : (r"^\s*<(.*?)>\s*(.*)", re.DOTALL), # ws < attributes > ws |
paul@0 | 29 | } |
paul@0 | 30 | |
paul@0 | 31 | patterns = {} |
paul@0 | 32 | for name, (value, flags) in syntax.items(): |
paul@0 | 33 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 34 | |
paul@4 | 35 | # Other regular expressions. |
paul@4 | 36 | |
paul@4 | 37 | leading_number_regexp = re.compile(r"\d*") |
paul@4 | 38 | |
paul@13 | 39 | # Constants. |
paul@13 | 40 | |
paul@13 | 41 | up_arrow = u'\u2191' |
paul@13 | 42 | down_arrow = u'\u2193' |
paul@13 | 43 | |
paul@0 | 44 | # Functions. |
paul@0 | 45 | |
paul@0 | 46 | def parse(s): |
paul@0 | 47 | |
paul@0 | 48 | "Parse 's', returning a table definition." |
paul@0 | 49 | |
paul@0 | 50 | table_attrs = {} |
paul@1 | 51 | rows = [] |
paul@0 | 52 | |
paul@1 | 53 | # The following will be redefined upon the construction of the first row. |
paul@1 | 54 | |
paul@1 | 55 | row_attrs = {} |
paul@1 | 56 | columns = [] |
paul@17 | 57 | columnnumber = 0 |
paul@17 | 58 | |
paul@17 | 59 | # The following will be redefined upon the construction of the first column. |
paul@17 | 60 | |
paul@17 | 61 | column_attrs = {} |
paul@1 | 62 | |
paul@1 | 63 | # Process exposed text and sections. |
paul@1 | 64 | |
paul@23 | 65 | marker = None |
paul@23 | 66 | is_region = True |
paul@1 | 67 | |
paul@1 | 68 | # Initially, start a new row. |
paul@1 | 69 | |
paul@1 | 70 | row_continued = False |
paul@1 | 71 | |
paul@23 | 72 | for match_text in patterns["markers"].split(s): |
paul@0 | 73 | |
paul@23 | 74 | # Only look for table features in exposed text. Where a section is |
paul@23 | 75 | # defined, a marker will have been read and all regions before the |
paul@23 | 76 | # closing marker will not be exposed. |
paul@1 | 77 | |
paul@23 | 78 | if is_region and not marker: |
paul@1 | 79 | |
paul@1 | 80 | # Extract each row from the definition. |
paul@1 | 81 | |
paul@23 | 82 | for row_text in patterns["rows"].split(match_text): |
paul@1 | 83 | |
paul@1 | 84 | # Only create a new row when a boundary has been found. |
paul@0 | 85 | |
paul@1 | 86 | if not row_continued: |
paul@17 | 87 | |
paul@17 | 88 | # Complete any existing row. |
paul@17 | 89 | |
paul@1 | 90 | if columns: |
paul@22 | 91 | extractAttributes(columns, row_attrs, table_attrs) |
paul@17 | 92 | span_columns(columns, columnnumber) |
paul@17 | 93 | |
paul@17 | 94 | # Replicate the last row to determine column usage. |
paul@17 | 95 | |
paul@17 | 96 | column_usage = [] |
paul@17 | 97 | |
paul@17 | 98 | for column_attrs, text in columns: |
paul@17 | 99 | rowspan = int(strip_token(column_attrs.get("rowspan", "1"))) |
paul@17 | 100 | if rowspan > 1: |
paul@17 | 101 | attrs = {} |
paul@17 | 102 | attrs.update(column_attrs) |
paul@17 | 103 | attrs["rowspan"] = str(rowspan - 1) |
paul@17 | 104 | attrs["rowcontinuation"] = True |
paul@17 | 105 | column_usage.append((attrs, text)) |
paul@17 | 106 | else: |
paul@17 | 107 | column_usage.append(({}, None)) |
paul@17 | 108 | |
paul@17 | 109 | columns = column_usage |
paul@17 | 110 | |
paul@17 | 111 | # Define a new collection of row attributes. |
paul@0 | 112 | |
paul@1 | 113 | row_attrs = {} |
paul@17 | 114 | |
paul@17 | 115 | # Reset the columns and make the list available for the |
paul@17 | 116 | # addition of new columns, starting a new column |
paul@17 | 117 | # immediately. |
paul@17 | 118 | |
paul@1 | 119 | rows.append((row_attrs, columns)) |
paul@1 | 120 | column_continued = False |
paul@17 | 121 | columnnumber = 0 |
paul@0 | 122 | |
paul@1 | 123 | # Extract each column from the row. |
paul@0 | 124 | |
paul@1 | 125 | for text in patterns["columns"].split(row_text): |
paul@1 | 126 | |
paul@1 | 127 | # Only create a new column when a boundary has been found. |
paul@1 | 128 | |
paul@1 | 129 | if not column_continued: |
paul@1 | 130 | |
paul@17 | 131 | # Complete any existing column. |
paul@17 | 132 | |
paul@17 | 133 | if columns: |
paul@17 | 134 | columnnumber = span_columns(columns, columnnumber) |
paul@17 | 135 | |
paul@1 | 136 | # Extract the attribute and text sections. |
paul@0 | 137 | |
paul@1 | 138 | match = patterns["column"].search(text) |
paul@1 | 139 | if match: |
paul@1 | 140 | attribute_text, text = match.groups() |
paul@17 | 141 | column_attrs = parseAttributes(attribute_text, True) |
paul@1 | 142 | else: |
paul@17 | 143 | column_attrs = {} |
paul@17 | 144 | |
paul@17 | 145 | # Define the new column with a mutable container |
paul@17 | 146 | # permitting the extension of the text. |
paul@17 | 147 | |
paul@17 | 148 | details = [column_attrs, text] |
paul@17 | 149 | |
paul@17 | 150 | # Find the next gap in the columns. |
paul@17 | 151 | |
paul@17 | 152 | while columnnumber != -1 and columnnumber < len(columns): |
paul@17 | 153 | attrs, text = columns[columnnumber] |
paul@17 | 154 | if text is None: |
paul@17 | 155 | columns[columnnumber] = details |
paul@17 | 156 | break |
paul@17 | 157 | columnnumber += 1 |
paul@17 | 158 | |
paul@17 | 159 | # Or start adding at the end of the row. |
paul@17 | 160 | |
paul@17 | 161 | else: |
paul@17 | 162 | columnnumber = -1 |
paul@17 | 163 | columns.append(details) |
paul@0 | 164 | |
paul@1 | 165 | else: |
paul@17 | 166 | columns[columnnumber][1] += text |
paul@1 | 167 | |
paul@1 | 168 | # Permit columns immediately following this one. |
paul@1 | 169 | |
paul@1 | 170 | column_continued = False |
paul@0 | 171 | |
paul@1 | 172 | # Permit a continuation of the current column. |
paul@1 | 173 | |
paul@1 | 174 | column_continued = True |
paul@1 | 175 | |
paul@1 | 176 | # Permit rows immediately following this one. |
paul@1 | 177 | |
paul@1 | 178 | row_continued = False |
paul@1 | 179 | |
paul@1 | 180 | # Permit a continuation if the current row. |
paul@0 | 181 | |
paul@1 | 182 | row_continued = True |
paul@1 | 183 | |
paul@23 | 184 | else: |
paul@23 | 185 | |
paul@23 | 186 | # Handle section markers. |
paul@23 | 187 | |
paul@23 | 188 | if not is_region: |
paul@23 | 189 | |
paul@23 | 190 | # Interpret the given marker, closing the current section if the |
paul@23 | 191 | # given marker is the corresponding end marker for the current |
paul@23 | 192 | # section. |
paul@0 | 193 | |
paul@23 | 194 | if marker: |
paul@23 | 195 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@23 | 196 | marker = None |
paul@23 | 197 | |
paul@23 | 198 | # Without a current marker, start a section if an appropriate marker |
paul@23 | 199 | # is given. |
paul@1 | 200 | |
paul@23 | 201 | elif match_text.startswith("{"): |
paul@23 | 202 | marker = match_text |
paul@23 | 203 | |
paul@23 | 204 | # Markers and section text are incorporated into the current column. |
paul@23 | 205 | |
paul@23 | 206 | columns[columnnumber][1] += match_text |
paul@23 | 207 | |
paul@23 | 208 | is_region = not is_region |
paul@1 | 209 | |
paul@17 | 210 | # Complete any final row. |
paul@17 | 211 | |
paul@1 | 212 | if columns: |
paul@22 | 213 | extractAttributes(columns, row_attrs, table_attrs) |
paul@0 | 214 | |
paul@0 | 215 | return table_attrs, rows |
paul@0 | 216 | |
paul@17 | 217 | def span_columns(columns, columnnumber): |
paul@17 | 218 | |
paul@17 | 219 | """ |
paul@17 | 220 | In the 'columns', make the column with the 'columnnumber' span the specified |
paul@17 | 221 | number of columns, returning the next appropriate column number. |
paul@17 | 222 | """ |
paul@17 | 223 | |
paul@17 | 224 | column_attrs, text = columns[columnnumber] |
paul@17 | 225 | |
paul@17 | 226 | # Handle any previous column spanning other columns. |
paul@17 | 227 | |
paul@17 | 228 | if column_attrs.has_key("colspan"): |
paul@17 | 229 | colspan = int(strip_token(column_attrs["colspan"])) |
paul@17 | 230 | |
paul@17 | 231 | # Duplicate the current column as continuation |
paul@17 | 232 | # columns for as long as the colspan is defined. |
paul@17 | 233 | |
paul@17 | 234 | colspan -= 1 |
paul@17 | 235 | while colspan > 0: |
paul@17 | 236 | attrs = {} |
paul@17 | 237 | attrs.update(column_attrs) |
paul@17 | 238 | attrs["colspan"] = str(colspan) |
paul@17 | 239 | attrs["colcontinuation"] = True |
paul@17 | 240 | |
paul@17 | 241 | if columnnumber != -1: |
paul@17 | 242 | columnnumber += 1 |
paul@17 | 243 | if columnnumber < len(columns): |
paul@17 | 244 | columns[columnnumber] = attrs, text |
paul@17 | 245 | else: |
paul@17 | 246 | columnnumber = -1 |
paul@17 | 247 | |
paul@17 | 248 | if columnnumber == -1: |
paul@17 | 249 | columns.append((attrs, text)) |
paul@17 | 250 | |
paul@17 | 251 | colspan -= 1 |
paul@17 | 252 | |
paul@17 | 253 | return columnnumber |
paul@17 | 254 | |
paul@22 | 255 | def extractAttributes(columns, row_attrs, table_attrs): |
paul@1 | 256 | |
paul@1 | 257 | """ |
paul@22 | 258 | Extract row- and table-level attributes from 'columns', storing them in |
paul@1 | 259 | 'row_attrs' and 'table_attrs' respectively. |
paul@1 | 260 | """ |
paul@1 | 261 | |
paul@22 | 262 | for column in columns: |
paul@22 | 263 | attrs = column[0] |
paul@22 | 264 | for name, value in attrs.items(): |
paul@22 | 265 | if name.startswith("row") and name not in ("rowspan", "rowcontinuation"): |
paul@22 | 266 | row_attrs[name] = value |
paul@22 | 267 | del attrs[name] |
paul@22 | 268 | elif name.startswith("table"): |
paul@22 | 269 | table_attrs[name] = value |
paul@22 | 270 | del attrs[name] |
paul@1 | 271 | |
paul@0 | 272 | # Formatting of embedded content. |
paul@0 | 273 | # NOTE: Borrowed from EventAggregator. |
paul@0 | 274 | |
paul@0 | 275 | def getParserClass(request, format): |
paul@0 | 276 | |
paul@0 | 277 | """ |
paul@0 | 278 | Return a parser class using the 'request' for the given 'format', returning |
paul@0 | 279 | a plain text parser if no parser can be found for the specified 'format'. |
paul@0 | 280 | """ |
paul@0 | 281 | |
paul@0 | 282 | try: |
paul@0 | 283 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@0 | 284 | except wikiutil.PluginMissingError: |
paul@0 | 285 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@0 | 286 | |
paul@10 | 287 | def getFormatterClass(request, format): |
paul@10 | 288 | |
paul@10 | 289 | """ |
paul@10 | 290 | Return a formatter class using the 'request' for the given output 'format', |
paul@10 | 291 | returning a plain text formatter if no formatter can be found for the |
paul@10 | 292 | specified 'format'. |
paul@10 | 293 | """ |
paul@10 | 294 | |
paul@10 | 295 | try: |
paul@14 | 296 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", format or "plain") |
paul@10 | 297 | except wikiutil.PluginMissingError: |
paul@14 | 298 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", "plain") |
paul@10 | 299 | |
paul@0 | 300 | def formatText(text, request, fmt): |
paul@0 | 301 | |
paul@0 | 302 | "Format the given 'text' using the specified 'request' and formatter 'fmt'." |
paul@0 | 303 | |
paul@0 | 304 | parser_cls = getParserClass(request, request.page.pi["format"]) |
paul@0 | 305 | parser = parser_cls(text, request, line_anchors=False) |
paul@14 | 306 | old_fmt = request.formatter |
paul@14 | 307 | request.formatter = fmt |
paul@14 | 308 | try: |
paul@14 | 309 | return redirectedOutput(request, parser, fmt, inhibit_p=True) |
paul@14 | 310 | finally: |
paul@14 | 311 | request.formatter = old_fmt |
paul@14 | 312 | |
paul@14 | 313 | def redirectedOutput(request, parser, fmt, **kw): |
paul@14 | 314 | |
paul@14 | 315 | "A fixed version of the request method of the same name." |
paul@14 | 316 | |
paul@14 | 317 | buf = StringIO() |
paul@14 | 318 | request.redirect(buf) |
paul@14 | 319 | try: |
paul@14 | 320 | parser.format(fmt, **kw) |
paul@14 | 321 | if hasattr(fmt, "flush"): |
paul@14 | 322 | buf.write(fmt.flush(True)) |
paul@14 | 323 | finally: |
paul@14 | 324 | request.redirect() |
paul@14 | 325 | text = buf.getvalue() |
paul@14 | 326 | buf.close() |
paul@14 | 327 | return text |
paul@0 | 328 | |
paul@4 | 329 | # Sorting utilities. |
paul@4 | 330 | |
paul@4 | 331 | def get_sort_columns(s, start=0): |
paul@4 | 332 | |
paul@4 | 333 | """ |
paul@4 | 334 | Split the comma-separated string 's', extracting the column specifications |
paul@20 | 335 | of the form <column>["n"] where the suffix "n" indicates an optional |
paul@4 | 336 | numeric conversion for that column. Column indexes start from the specified |
paul@4 | 337 | 'start' value (defaulting to 0). |
paul@4 | 338 | """ |
paul@4 | 339 | |
paul@4 | 340 | sort_columns = [] |
paul@4 | 341 | for column_spec in s.split(","): |
paul@4 | 342 | column_spec = column_spec.strip() |
paul@4 | 343 | |
paul@4 | 344 | ascending = True |
paul@4 | 345 | if column_spec.endswith("d"): |
paul@4 | 346 | column_spec = column_spec[:-1] |
paul@4 | 347 | ascending = False |
paul@4 | 348 | |
paul@4 | 349 | # Extract the conversion indicator and column index. |
paul@4 | 350 | # Ignore badly-specified columns. |
paul@4 | 351 | |
paul@4 | 352 | try: |
paul@6 | 353 | column = get_number(column_spec) |
paul@6 | 354 | suffix = column_spec[len(column):] |
paul@6 | 355 | fn = converters[suffix] |
paul@4 | 356 | sort_columns.append((max(0, int(column) - start), fn, ascending)) |
paul@4 | 357 | except ValueError: |
paul@4 | 358 | pass |
paul@4 | 359 | |
paul@4 | 360 | return sort_columns |
paul@4 | 361 | |
paul@6 | 362 | def get_column_types(sort_columns): |
paul@6 | 363 | |
paul@6 | 364 | """ |
paul@6 | 365 | Return a dictionary mapping column indexes to conversion functions. |
paul@6 | 366 | """ |
paul@6 | 367 | |
paul@6 | 368 | d = {} |
paul@6 | 369 | for column, fn, ascending in sort_columns: |
paul@6 | 370 | d[column] = fn, ascending |
paul@6 | 371 | return d |
paul@6 | 372 | |
paul@6 | 373 | def get_number(s): |
paul@6 | 374 | |
paul@6 | 375 | "From 's', get any leading number." |
paul@6 | 376 | |
paul@6 | 377 | match = leading_number_regexp.match(s) |
paul@6 | 378 | if match: |
paul@6 | 379 | return match.group() |
paul@6 | 380 | else: |
paul@6 | 381 | return "" |
paul@6 | 382 | |
paul@10 | 383 | def to_number(s, request): |
paul@4 | 384 | |
paul@12 | 385 | """ |
paul@12 | 386 | Convert 's' to a number, discarding any non-numeric trailing data. |
paul@12 | 387 | Return an empty string if 's' is empty. |
paul@12 | 388 | """ |
paul@4 | 389 | |
paul@12 | 390 | if s: |
paul@20 | 391 | return int(get_number(to_plain_text(s, request))) |
paul@12 | 392 | else: |
paul@12 | 393 | return s |
paul@4 | 394 | |
paul@10 | 395 | def to_plain_text(s, request): |
paul@10 | 396 | |
paul@10 | 397 | "Convert 's' to plain text." |
paul@10 | 398 | |
paul@14 | 399 | fmt = getFormatterClass(request, "plain")(request) |
paul@14 | 400 | fmt.setPage(request.page) |
paul@10 | 401 | return formatText(s, request, fmt) |
paul@10 | 402 | |
paul@10 | 403 | converters = { |
paul@10 | 404 | "n" : to_number, |
paul@10 | 405 | "" : to_plain_text, |
paul@10 | 406 | } |
paul@10 | 407 | |
paul@10 | 408 | suffixes = {} |
paul@10 | 409 | for key, value in converters.items(): |
paul@10 | 410 | suffixes[value] = key |
paul@10 | 411 | |
paul@4 | 412 | class Sorter: |
paul@4 | 413 | |
paul@4 | 414 | "A sorting helper class." |
paul@4 | 415 | |
paul@10 | 416 | def __init__(self, sort_columns, request): |
paul@4 | 417 | self.sort_columns = sort_columns |
paul@10 | 418 | self.request = request |
paul@4 | 419 | |
paul@4 | 420 | def __call__(self, row1, row2): |
paul@4 | 421 | row_attrs1, columns1 = row1 |
paul@4 | 422 | row_attrs2, columns2 = row2 |
paul@4 | 423 | |
paul@4 | 424 | # Apply the conversions to each column, comparing the results. |
paul@4 | 425 | |
paul@4 | 426 | for column, fn, ascending in self.sort_columns: |
paul@4 | 427 | column_attrs1, text1 = columns1[column] |
paul@4 | 428 | column_attrs2, text2 = columns2[column] |
paul@4 | 429 | |
paul@4 | 430 | # Ignore a column when a conversion is not possible. |
paul@4 | 431 | |
paul@4 | 432 | try: |
paul@12 | 433 | value1 = fn(text1, self.request) |
paul@12 | 434 | value2 = fn(text2, self.request) |
paul@12 | 435 | |
paul@12 | 436 | # Avoid empty strings appearing earlier than other values. |
paul@12 | 437 | |
paul@12 | 438 | if value1 == "" and value2 != "": |
paul@12 | 439 | result = 1 |
paul@12 | 440 | elif value1 != "" and value2 == "": |
paul@12 | 441 | result = -1 |
paul@12 | 442 | else: |
paul@12 | 443 | result = cmp(value1, value2) |
paul@4 | 444 | |
paul@4 | 445 | # Where the columns differ, return a result observing the sense |
paul@4 | 446 | # (ascending or descending) of the comparison for the column. |
paul@4 | 447 | |
paul@4 | 448 | if result != 0: |
paul@4 | 449 | return ascending and result or -result |
paul@4 | 450 | |
paul@4 | 451 | except ValueError: |
paul@4 | 452 | pass |
paul@4 | 453 | |
paul@4 | 454 | return 0 |
paul@4 | 455 | |
paul@16 | 456 | def write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, start=0): |
paul@6 | 457 | |
paul@6 | 458 | """ |
paul@16 | 459 | Using the 'request', write a sort control for the given 'columnnumber' in |
paul@16 | 460 | the collection of 'columns', using the existing 'sort_columns' and |
paul@16 | 461 | 'column_types' to construct labels and links that modify the sort criteria, |
paul@16 | 462 | and using the given 'table_name' to parameterise the links. |
paul@6 | 463 | """ |
paul@6 | 464 | |
paul@16 | 465 | fmt = request.formatter |
paul@16 | 466 | write = request.write |
paul@7 | 467 | _ = request.getText |
paul@7 | 468 | |
paul@7 | 469 | write(fmt.div(1, css_class="sortcolumns")) |
paul@6 | 470 | |
paul@16 | 471 | write(fmt.paragraph(1)) |
paul@16 | 472 | write(fmt.text(_("Sort by columns..."))) |
paul@16 | 473 | write(fmt.paragraph(0)) |
paul@16 | 474 | |
paul@6 | 475 | # Start with the existing criteria without this column being involved. |
paul@6 | 476 | |
paul@7 | 477 | revised_sort_columns = [(column, fn, ascending) |
paul@6 | 478 | for (column, fn, ascending) in sort_columns if column != columnnumber] |
paul@7 | 479 | |
paul@7 | 480 | # Get the specification of this column. |
paul@7 | 481 | |
paul@10 | 482 | columnfn, columnascending = column_types.get(columnnumber, (to_plain_text, True)) |
paul@7 | 483 | newsortcolumn = columnnumber, columnfn, columnascending |
paul@13 | 484 | newsortcolumn_reverse = columnnumber, columnfn, not columnascending |
paul@7 | 485 | newlabel = columns[columnnumber][1].strip() |
paul@7 | 486 | |
paul@7 | 487 | # Show this column in all possible places in the sorting criteria. |
paul@6 | 488 | |
paul@7 | 489 | write(fmt.number_list(1)) |
paul@7 | 490 | |
paul@7 | 491 | just_had_this_column = False |
paul@7 | 492 | |
paul@7 | 493 | for i, (column, fn, ascending) in enumerate(sort_columns): |
paul@7 | 494 | new_sort_columns = revised_sort_columns[:] |
paul@7 | 495 | new_sort_columns.insert(i, newsortcolumn) |
paul@7 | 496 | label = columns[column][1].strip() |
paul@6 | 497 | |
paul@13 | 498 | arrow = columnascending and down_arrow or up_arrow |
paul@13 | 499 | arrow_reverse = not columnascending and down_arrow or up_arrow |
paul@13 | 500 | |
paul@13 | 501 | sortcolumns = get_sort_column_output(new_sort_columns) |
paul@13 | 502 | new_sort_columns[i] = newsortcolumn_reverse |
paul@13 | 503 | sortcolumns_reverse = get_sort_column_output(new_sort_columns) |
paul@13 | 504 | |
paul@13 | 505 | # Columns permitting the insertion of the selected column. |
paul@7 | 506 | |
paul@7 | 507 | if column != columnnumber and not just_had_this_column: |
paul@7 | 508 | write(fmt.listitem(1, css_class="sortcolumn")) |
paul@7 | 509 | |
paul@13 | 510 | # Pop-up element showing the column inserted before the sort column. |
paul@13 | 511 | |
paul@7 | 512 | write(fmt.span(1, css_class="sortcolumn-container")) |
paul@7 | 513 | write(fmt.span(1, css_class="newsortcolumn")) |
paul@8 | 514 | write(formatText(newlabel, request, fmt)) |
paul@13 | 515 | |
paul@13 | 516 | write_sort_link(write, request, fmt, table_name, sortcolumns, arrow, "sortdirection") |
paul@13 | 517 | write_sort_link(write, request, fmt, table_name, sortcolumns_reverse, arrow_reverse, "sortdirection") |
paul@13 | 518 | |
paul@7 | 519 | write(fmt.span(0)) |
paul@7 | 520 | write(fmt.span(0)) |
paul@6 | 521 | |
paul@17 | 522 | # Link for selection of the modified sort criteria using the current |
paul@17 | 523 | # column and showing its particular direction. |
paul@7 | 524 | |
paul@17 | 525 | arrow = ascending and down_arrow or up_arrow |
paul@17 | 526 | arrow_reverse = not ascending and down_arrow or up_arrow |
paul@13 | 527 | write_sort_link(write, request, fmt, table_name, sortcolumns, u"%s %s" % (label, arrow), "") |
paul@13 | 528 | |
paul@13 | 529 | # Columns permitting removal or modification. |
paul@7 | 530 | |
paul@7 | 531 | else: |
paul@7 | 532 | write(fmt.listitem(1)) |
paul@7 | 533 | |
paul@7 | 534 | # Either show the column without a link, since the column to be |
paul@7 | 535 | # inserted is already before the current column. |
paul@6 | 536 | |
paul@7 | 537 | if just_had_this_column: |
paul@7 | 538 | just_had_this_column = False |
paul@17 | 539 | arrow = ascending and down_arrow or up_arrow |
paul@17 | 540 | arrow_reverse = not ascending and down_arrow or up_arrow |
paul@17 | 541 | |
paul@17 | 542 | # Write the current column with its particular direction. |
paul@17 | 543 | |
paul@7 | 544 | write(fmt.span(1, css_class="unlinkedcolumn")) |
paul@17 | 545 | write(formatText(u"%s %s" % (label, arrow), request, fmt)) |
paul@7 | 546 | write(fmt.span(0)) |
paul@7 | 547 | |
paul@7 | 548 | # Or show the column with a link for its removal. |
paul@6 | 549 | |
paul@7 | 550 | else: |
paul@7 | 551 | just_had_this_column = True |
paul@13 | 552 | sortcolumns_revised = get_sort_column_output(revised_sort_columns) |
paul@13 | 553 | write_sort_link(write, request, fmt, table_name, sortcolumns_revised, u"%s %s" % (label, arrow), "removecolumn") |
paul@13 | 554 | |
paul@16 | 555 | # Alternative sort direction. |
paul@13 | 556 | |
paul@16 | 557 | write_sort_link(write, request, fmt, table_name, sortcolumns_reverse, arrow_reverse, "altdirection") |
paul@7 | 558 | |
paul@7 | 559 | write(fmt.listitem(0)) |
paul@7 | 560 | |
paul@9 | 561 | if not just_had_this_column: |
paul@7 | 562 | |
paul@7 | 563 | # Write the sorting criteria with this column at the end. |
paul@7 | 564 | |
paul@7 | 565 | new_sort_columns = revised_sort_columns[:] |
paul@7 | 566 | new_sort_columns.append(newsortcolumn) |
paul@6 | 567 | |
paul@13 | 568 | sortcolumns = get_sort_column_output(new_sort_columns) |
paul@13 | 569 | new_sort_columns[-1] = newsortcolumn_reverse |
paul@13 | 570 | sortcolumns_reverse = get_sort_column_output(new_sort_columns) |
paul@13 | 571 | |
paul@13 | 572 | arrow = columnascending and down_arrow or up_arrow |
paul@13 | 573 | arrow_reverse = not columnascending and down_arrow or up_arrow |
paul@13 | 574 | |
paul@15 | 575 | write(fmt.listitem(1, css_class="appendcolumn")) |
paul@7 | 576 | |
paul@7 | 577 | # Pop-up element showing the column inserted before the sort column. |
paul@7 | 578 | |
paul@7 | 579 | write(fmt.span(1, css_class="newsortcolumn")) |
paul@15 | 580 | write_sort_link(write, request, fmt, table_name, sortcolumns, newlabel, "") |
paul@13 | 581 | write_sort_link(write, request, fmt, table_name, sortcolumns, arrow, "sortdirection") |
paul@13 | 582 | write_sort_link(write, request, fmt, table_name, sortcolumns_reverse, arrow_reverse, "sortdirection") |
paul@7 | 583 | write(fmt.span(0)) |
paul@7 | 584 | |
paul@7 | 585 | write(fmt.listitem(0)) |
paul@7 | 586 | |
paul@7 | 587 | write(fmt.number_list(0)) |
paul@7 | 588 | |
paul@7 | 589 | write(fmt.div(0)) |
paul@7 | 590 | |
paul@13 | 591 | def write_sort_link(write, request, fmt, table_name, sortcolumns, label, css_class): |
paul@13 | 592 | |
paul@13 | 593 | "Write a link expressing sort criteria." |
paul@13 | 594 | |
paul@13 | 595 | write(fmt.url(1, "?%s#%s" % ( |
paul@13 | 596 | wikiutil.makeQueryString("%s-sortcolumns=%s" % (table_name, sortcolumns)), |
paul@13 | 597 | fmt.qualify_id(fmt.sanitize_to_id(table_name)) |
paul@13 | 598 | ), css_class=css_class)) |
paul@13 | 599 | write(formatText(label, request, fmt)) |
paul@13 | 600 | write(fmt.url(0)) |
paul@13 | 601 | |
paul@7 | 602 | def get_sort_column_output(columns, start=0): |
paul@7 | 603 | |
paul@7 | 604 | "Return the output criteria for the given 'columns' indexed from 'start'." |
paul@7 | 605 | |
paul@7 | 606 | return ",".join([("%d%s%s" % (column + start, suffixes[fn], not ascending and "d" or "")) |
paul@7 | 607 | for (column, fn, ascending) in columns]) |
paul@6 | 608 | |
paul@0 | 609 | # Common formatting functions. |
paul@0 | 610 | |
paul@4 | 611 | def formatTable(text, request, fmt, attrs=None): |
paul@0 | 612 | |
paul@4 | 613 | """ |
paul@4 | 614 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@4 | 615 | The optional 'attrs' can be used to control the presentation of the table. |
paul@4 | 616 | """ |
paul@4 | 617 | |
paul@5 | 618 | # Parse the table region. |
paul@5 | 619 | |
paul@4 | 620 | table_attrs, table = parse(text) |
paul@4 | 621 | |
paul@11 | 622 | # Define the table name and an anchor attribute. |
paul@5 | 623 | |
paul@5 | 624 | table_name = attrs.get("name") |
paul@10 | 625 | if table_name: |
paul@10 | 626 | table_attrs["tableid"] = table_name |
paul@11 | 627 | else: |
paul@11 | 628 | table_name = table_attrs.get("tableid") |
paul@6 | 629 | |
paul@17 | 630 | # Only attempt to offer sorting capabilities if a table name is specified. |
paul@11 | 631 | |
paul@11 | 632 | if table_name: |
paul@17 | 633 | |
paul@17 | 634 | # Get the underlying column types. |
paul@11 | 635 | |
paul@17 | 636 | column_types = get_column_types(get_sort_columns(attrs.get("columntypes", ""))) |
paul@17 | 637 | |
paul@17 | 638 | # Get sorting criteria from the region. |
paul@17 | 639 | |
paul@17 | 640 | region_sortcolumns = attrs.get("sortcolumns", "") |
paul@11 | 641 | |
paul@17 | 642 | # Update the column types from the sort criteria. |
paul@11 | 643 | |
paul@17 | 644 | column_types.update(get_column_types(get_sort_columns(region_sortcolumns))) |
paul@17 | 645 | |
paul@17 | 646 | # Determine the applicable sort criteria using the request. |
paul@11 | 647 | |
paul@17 | 648 | sortcolumns = getQualifiedParameter(request, table_name, "sortcolumns") |
paul@17 | 649 | if sortcolumns is None: |
paul@17 | 650 | sortcolumns = region_sortcolumns |
paul@11 | 651 | |
paul@17 | 652 | # Define the final sort criteria. |
paul@5 | 653 | |
paul@17 | 654 | sort_columns = get_sort_columns(sortcolumns) |
paul@17 | 655 | data_start = int(getQualifiedParameter(request, table_name, "headers") or attrs.get("headers", "1")) |
paul@6 | 656 | |
paul@17 | 657 | # Update the column types from the final sort criteria. |
paul@17 | 658 | |
paul@17 | 659 | column_types.update(get_column_types(sort_columns)) |
paul@17 | 660 | |
paul@17 | 661 | # Sort the rows according to the values in each of the specified columns. |
paul@4 | 662 | |
paul@17 | 663 | if sort_columns: |
paul@17 | 664 | headers = table[:data_start] |
paul@17 | 665 | data = table[data_start:] |
paul@17 | 666 | |
paul@17 | 667 | # Perform the sort and reconstruct the table. |
paul@6 | 668 | |
paul@17 | 669 | sorter = Sorter(sort_columns, request) |
paul@17 | 670 | data.sort(cmp=sorter) |
paul@17 | 671 | table = headers + data |
paul@4 | 672 | |
paul@17 | 673 | # Otherwise, indicate that no sorting is being performed. |
paul@11 | 674 | |
paul@11 | 675 | else: |
paul@17 | 676 | sort_columns = None |
paul@4 | 677 | |
paul@4 | 678 | # Write the table. |
paul@4 | 679 | |
paul@6 | 680 | write = request.write |
paul@6 | 681 | write(fmt.table(1, table_attrs)) |
paul@0 | 682 | |
paul@6 | 683 | for rownumber, (row_attrs, columns) in enumerate(table): |
paul@6 | 684 | write(fmt.table_row(1, row_attrs)) |
paul@17 | 685 | sortable_heading = sort_columns is not None and rownumber == data_start - 1 |
paul@6 | 686 | |
paul@6 | 687 | for columnnumber, (column_attrs, column_text) in enumerate(columns): |
paul@17 | 688 | |
paul@17 | 689 | # Always skip column continuation cells. |
paul@17 | 690 | |
paul@17 | 691 | if column_attrs.get("colcontinuation"): |
paul@17 | 692 | continue |
paul@17 | 693 | |
paul@17 | 694 | # Where sorting has not occurred, preserve rowspans and do not write |
paul@17 | 695 | # cells that continue a rowspan. |
paul@17 | 696 | |
paul@17 | 697 | if not sort_columns: |
paul@17 | 698 | if column_attrs.get("rowcontinuation"): |
paul@17 | 699 | continue |
paul@17 | 700 | |
paul@17 | 701 | # Where sorting has occurred, replicate cell contents and remove any |
paul@17 | 702 | # rowspans. |
paul@17 | 703 | |
paul@17 | 704 | else: |
paul@17 | 705 | if column_attrs.has_key("rowspan"): |
paul@17 | 706 | del column_attrs["rowspan"] |
paul@17 | 707 | |
paul@17 | 708 | # Remove any continuation attributes that still apply. |
paul@17 | 709 | |
paul@17 | 710 | if column_attrs.has_key("rowcontinuation"): |
paul@17 | 711 | del column_attrs["rowcontinuation"] |
paul@17 | 712 | |
paul@6 | 713 | write(fmt.table_cell(1, column_attrs)) |
paul@7 | 714 | |
paul@17 | 715 | if sortable_heading: |
paul@7 | 716 | write(fmt.div(1, css_class="sortablecolumn")) |
paul@7 | 717 | |
paul@17 | 718 | write(formatText(column_text or "", request, fmt)) |
paul@0 | 719 | |
paul@6 | 720 | # Add sorting controls, if appropriate. |
paul@6 | 721 | |
paul@17 | 722 | if sortable_heading: |
paul@16 | 723 | write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name) |
paul@7 | 724 | write(fmt.div(0)) |
paul@0 | 725 | |
paul@6 | 726 | write(fmt.table_cell(0)) |
paul@0 | 727 | |
paul@6 | 728 | write(fmt.table_row(0)) |
paul@6 | 729 | |
paul@6 | 730 | write(fmt.table(0)) |
paul@0 | 731 | |
paul@0 | 732 | # vim: tabstop=4 expandtab shiftwidth=4 |