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