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