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