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