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