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@0 | 12 | import re |
paul@0 | 13 | |
paul@0 | 14 | # Regular expressions. |
paul@0 | 15 | |
paul@0 | 16 | syntax = { |
paul@1 | 17 | # For section markers. |
paul@1 | 18 | "markers" : (r"^\s*(?P<n>\\+)(?P<b>{|})(?P=n)(?P=b)(?P=n)(?P=b)", re.MULTILINE), |
paul@1 | 19 | "marker" : (r"(\\+)", 0), |
paul@1 | 20 | |
paul@0 | 21 | # At start of line: |
paul@1 | 22 | "sections" : (r"(^\s*{{{.*?^\s*}}})", re.MULTILINE | re.DOTALL), # {{{ ... }}} |
paul@1 | 23 | "rows" : (r"^==", re.MULTILINE), # == |
paul@1 | 24 | |
paul@0 | 25 | # Within text: |
paul@1 | 26 | "columns" : (r"\|\|[ \t]*", 0), # || ws-excl-nl |
paul@1 | 27 | |
paul@0 | 28 | # At start of column text: |
paul@1 | 29 | "column" : (r"^\s*<(.*?)>\s*(.*)", re.DOTALL), # ws < attributes > ws |
paul@0 | 30 | } |
paul@0 | 31 | |
paul@0 | 32 | patterns = {} |
paul@0 | 33 | for name, (value, flags) in syntax.items(): |
paul@0 | 34 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 35 | |
paul@4 | 36 | # Other regular expressions. |
paul@4 | 37 | |
paul@4 | 38 | leading_number_regexp = re.compile(r"\d*") |
paul@4 | 39 | |
paul@0 | 40 | # Functions. |
paul@0 | 41 | |
paul@0 | 42 | def parse(s): |
paul@0 | 43 | |
paul@0 | 44 | "Parse 's', returning a table definition." |
paul@0 | 45 | |
paul@1 | 46 | s = replaceMarkers(s) |
paul@1 | 47 | |
paul@0 | 48 | table_attrs = {} |
paul@1 | 49 | rows = [] |
paul@0 | 50 | |
paul@1 | 51 | # The following will be redefined upon the construction of the first row. |
paul@1 | 52 | |
paul@1 | 53 | row_attrs = {} |
paul@1 | 54 | columns = [] |
paul@1 | 55 | |
paul@1 | 56 | # Process exposed text and sections. |
paul@1 | 57 | |
paul@1 | 58 | exposed = True |
paul@1 | 59 | |
paul@1 | 60 | # Initially, start a new row. |
paul@1 | 61 | |
paul@1 | 62 | row_continued = False |
paul@1 | 63 | |
paul@1 | 64 | for region in patterns["sections"].split(s): |
paul@0 | 65 | |
paul@1 | 66 | # Only look for table features in exposed text. |
paul@1 | 67 | |
paul@1 | 68 | if exposed: |
paul@1 | 69 | |
paul@1 | 70 | # Extract each row from the definition. |
paul@1 | 71 | |
paul@1 | 72 | for row_text in patterns["rows"].split(region): |
paul@1 | 73 | |
paul@1 | 74 | # Only create a new row when a boundary has been found. |
paul@0 | 75 | |
paul@1 | 76 | if not row_continued: |
paul@1 | 77 | if columns: |
paul@1 | 78 | extractAttributes(columns[0][0], row_attrs, table_attrs) |
paul@0 | 79 | |
paul@1 | 80 | row_attrs = {} |
paul@1 | 81 | columns = [] |
paul@1 | 82 | rows.append((row_attrs, columns)) |
paul@1 | 83 | column_continued = False |
paul@0 | 84 | |
paul@1 | 85 | # Extract each column from the row. |
paul@0 | 86 | |
paul@1 | 87 | for text in patterns["columns"].split(row_text): |
paul@1 | 88 | |
paul@1 | 89 | # Only create a new column when a boundary has been found. |
paul@1 | 90 | |
paul@1 | 91 | if not column_continued: |
paul@1 | 92 | |
paul@1 | 93 | # Extract the attribute and text sections. |
paul@0 | 94 | |
paul@1 | 95 | match = patterns["column"].search(text) |
paul@1 | 96 | if match: |
paul@1 | 97 | attribute_text, text = match.groups() |
paul@1 | 98 | columns.append([parseAttributes(attribute_text, True), text]) |
paul@1 | 99 | else: |
paul@1 | 100 | columns.append([{}, text]) |
paul@0 | 101 | |
paul@1 | 102 | else: |
paul@1 | 103 | columns[-1][1] += text |
paul@1 | 104 | |
paul@1 | 105 | # Permit columns immediately following this one. |
paul@1 | 106 | |
paul@1 | 107 | column_continued = False |
paul@0 | 108 | |
paul@1 | 109 | # Permit a continuation of the current column. |
paul@1 | 110 | |
paul@1 | 111 | column_continued = True |
paul@1 | 112 | |
paul@1 | 113 | # Permit rows immediately following this one. |
paul@1 | 114 | |
paul@1 | 115 | row_continued = False |
paul@1 | 116 | |
paul@1 | 117 | # Permit a continuation if the current row. |
paul@0 | 118 | |
paul@1 | 119 | row_continued = True |
paul@1 | 120 | |
paul@1 | 121 | # Write any section into the current column. |
paul@0 | 122 | |
paul@1 | 123 | else: |
paul@1 | 124 | columns[-1][1] += region |
paul@1 | 125 | |
paul@1 | 126 | exposed = not exposed |
paul@1 | 127 | |
paul@1 | 128 | if columns: |
paul@1 | 129 | extractAttributes(columns[0][0], row_attrs, table_attrs) |
paul@0 | 130 | |
paul@0 | 131 | return table_attrs, rows |
paul@0 | 132 | |
paul@1 | 133 | def extractAttributes(attrs, row_attrs, table_attrs): |
paul@1 | 134 | |
paul@1 | 135 | """ |
paul@1 | 136 | Extract row- and table-level attributes from 'attrs', storing them in |
paul@1 | 137 | 'row_attrs' and 'table_attrs' respectively. |
paul@1 | 138 | """ |
paul@1 | 139 | |
paul@1 | 140 | for name, value in attrs.items(): |
paul@3 | 141 | if name.startswith("row") and name != "rowspan": |
paul@1 | 142 | row_attrs[name] = value |
paul@1 | 143 | del attrs[name] |
paul@1 | 144 | elif name.startswith("table"): |
paul@1 | 145 | table_attrs[name] = value |
paul@1 | 146 | del attrs[name] |
paul@1 | 147 | |
paul@1 | 148 | def replaceMarkers(s): |
paul@1 | 149 | |
paul@1 | 150 | "Convert the section notation in 's'." |
paul@1 | 151 | |
paul@1 | 152 | l = [] |
paul@1 | 153 | last = 0 |
paul@1 | 154 | |
paul@1 | 155 | # Get each marker and convert it. |
paul@1 | 156 | |
paul@1 | 157 | for match in patterns["markers"].finditer(s): |
paul@1 | 158 | start, stop = match.span() |
paul@1 | 159 | l.append(s[last:start]) |
paul@1 | 160 | |
paul@1 | 161 | # Convert the marker. |
paul@1 | 162 | |
paul@1 | 163 | marker = [] |
paul@1 | 164 | brace = True |
paul@1 | 165 | for text in patterns["marker"].split(match.group()): |
paul@1 | 166 | if brace: |
paul@1 | 167 | marker.append(text) |
paul@1 | 168 | else: |
paul@1 | 169 | marker.append(text[:-1]) |
paul@1 | 170 | brace = not brace |
paul@1 | 171 | |
paul@1 | 172 | l.append("".join(marker)) |
paul@1 | 173 | last = stop |
paul@1 | 174 | else: |
paul@1 | 175 | l.append(s[last:]) |
paul@1 | 176 | |
paul@1 | 177 | return "".join(l) |
paul@1 | 178 | |
paul@0 | 179 | def parseAttributes(s, escape=True): |
paul@0 | 180 | |
paul@0 | 181 | """ |
paul@0 | 182 | Parse the table attributes string 's', returning a mapping of names to |
paul@0 | 183 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@4 | 184 | for use with the formatter API. If 'escape' is set to a false value, the |
paul@4 | 185 | attributes will have any quoting removed. |
paul@0 | 186 | """ |
paul@0 | 187 | |
paul@0 | 188 | attrs = {} |
paul@0 | 189 | f = StringIO(s) |
paul@0 | 190 | name = None |
paul@0 | 191 | need_value = False |
paul@0 | 192 | |
paul@0 | 193 | for token in shlex(f): |
paul@0 | 194 | |
paul@0 | 195 | # Capture the name if needed. |
paul@0 | 196 | |
paul@0 | 197 | if name is None: |
paul@4 | 198 | name = escape and wikiutil.escape(token) or strip_token(token) |
paul@0 | 199 | |
paul@0 | 200 | # Detect either an equals sign or another name. |
paul@0 | 201 | |
paul@0 | 202 | elif not need_value: |
paul@0 | 203 | if token == "=": |
paul@0 | 204 | need_value = True |
paul@0 | 205 | else: |
paul@0 | 206 | attrs[name.lower()] = escape and "true" or True |
paul@0 | 207 | name = wikiutil.escape(token) |
paul@0 | 208 | |
paul@0 | 209 | # Otherwise, capture a value. |
paul@0 | 210 | |
paul@0 | 211 | else: |
paul@4 | 212 | # Quoting of attributes done similarly to wikiutil.parseAttributes. |
paul@0 | 213 | |
paul@4 | 214 | if token: |
paul@4 | 215 | if escape: |
paul@4 | 216 | if token[0] in ("'", '"'): |
paul@4 | 217 | token = wikiutil.escape(token) |
paul@4 | 218 | else: |
paul@4 | 219 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@0 | 220 | else: |
paul@4 | 221 | token = strip_token(token) |
paul@0 | 222 | |
paul@0 | 223 | attrs[name.lower()] = token |
paul@0 | 224 | name = None |
paul@0 | 225 | need_value = False |
paul@0 | 226 | |
paul@0 | 227 | return attrs |
paul@0 | 228 | |
paul@4 | 229 | def strip_token(token): |
paul@4 | 230 | |
paul@4 | 231 | "Return the given 'token' stripped of quoting." |
paul@4 | 232 | |
paul@4 | 233 | if token[0] in ("'", '"') and token[-1] == token[0]: |
paul@4 | 234 | return token[1:-1] |
paul@4 | 235 | else: |
paul@4 | 236 | return token |
paul@4 | 237 | |
paul@0 | 238 | # Formatting of embedded content. |
paul@0 | 239 | # NOTE: Borrowed from EventAggregator. |
paul@0 | 240 | |
paul@0 | 241 | def getParserClass(request, format): |
paul@0 | 242 | |
paul@0 | 243 | """ |
paul@0 | 244 | Return a parser class using the 'request' for the given 'format', returning |
paul@0 | 245 | a plain text parser if no parser can be found for the specified 'format'. |
paul@0 | 246 | """ |
paul@0 | 247 | |
paul@0 | 248 | try: |
paul@0 | 249 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@0 | 250 | except wikiutil.PluginMissingError: |
paul@0 | 251 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@0 | 252 | |
paul@0 | 253 | def formatText(text, request, fmt): |
paul@0 | 254 | |
paul@0 | 255 | "Format the given 'text' using the specified 'request' and formatter 'fmt'." |
paul@0 | 256 | |
paul@0 | 257 | parser_cls = getParserClass(request, request.page.pi["format"]) |
paul@0 | 258 | parser = parser_cls(text, request, line_anchors=False) |
paul@0 | 259 | return request.redirectedOutput(parser.format, fmt, inhibit_p=True) |
paul@0 | 260 | |
paul@4 | 261 | # Sorting utilities. |
paul@4 | 262 | |
paul@4 | 263 | def get_sort_columns(s, start=0): |
paul@4 | 264 | |
paul@4 | 265 | """ |
paul@4 | 266 | Split the comma-separated string 's', extracting the column specifications |
paul@4 | 267 | of the form <column>["n"] where the prefix "n" indicates an optional |
paul@4 | 268 | numeric conversion for that column. Column indexes start from the specified |
paul@4 | 269 | 'start' value (defaulting to 0). |
paul@4 | 270 | """ |
paul@4 | 271 | |
paul@4 | 272 | sort_columns = [] |
paul@4 | 273 | for column_spec in s.split(","): |
paul@4 | 274 | column_spec = column_spec.strip() |
paul@4 | 275 | |
paul@4 | 276 | ascending = True |
paul@4 | 277 | if column_spec.endswith("d"): |
paul@4 | 278 | column_spec = column_spec[:-1] |
paul@4 | 279 | ascending = False |
paul@4 | 280 | |
paul@4 | 281 | # Extract the conversion indicator and column index. |
paul@4 | 282 | |
paul@4 | 283 | if column_spec.endswith("n"): |
paul@4 | 284 | column = column_spec[:-1] |
paul@4 | 285 | fn = to_number |
paul@4 | 286 | else: |
paul@4 | 287 | column = column_spec |
paul@4 | 288 | fn = str |
paul@4 | 289 | |
paul@4 | 290 | # Ignore badly-specified columns. |
paul@4 | 291 | |
paul@4 | 292 | try: |
paul@4 | 293 | sort_columns.append((max(0, int(column) - start), fn, ascending)) |
paul@4 | 294 | except ValueError: |
paul@4 | 295 | pass |
paul@4 | 296 | |
paul@4 | 297 | return sort_columns |
paul@4 | 298 | |
paul@4 | 299 | def to_number(s): |
paul@4 | 300 | |
paul@4 | 301 | "Convert 's' to a number, discarding any non-numeric trailing data." |
paul@4 | 302 | |
paul@4 | 303 | match = leading_number_regexp.match(s) |
paul@4 | 304 | if match: |
paul@4 | 305 | return int(match.group()) |
paul@4 | 306 | else: |
paul@4 | 307 | raise ValueError, s |
paul@4 | 308 | |
paul@4 | 309 | class Sorter: |
paul@4 | 310 | |
paul@4 | 311 | "A sorting helper class." |
paul@4 | 312 | |
paul@4 | 313 | def __init__(self, sort_columns): |
paul@4 | 314 | self.sort_columns = sort_columns |
paul@4 | 315 | |
paul@4 | 316 | def __call__(self, row1, row2): |
paul@4 | 317 | row_attrs1, columns1 = row1 |
paul@4 | 318 | row_attrs2, columns2 = row2 |
paul@4 | 319 | |
paul@4 | 320 | # Apply the conversions to each column, comparing the results. |
paul@4 | 321 | |
paul@4 | 322 | for column, fn, ascending in self.sort_columns: |
paul@4 | 323 | column_attrs1, text1 = columns1[column] |
paul@4 | 324 | column_attrs2, text2 = columns2[column] |
paul@4 | 325 | |
paul@4 | 326 | # Ignore a column when a conversion is not possible. |
paul@4 | 327 | |
paul@4 | 328 | try: |
paul@4 | 329 | text1 = fn(text1) |
paul@4 | 330 | text2 = fn(text2) |
paul@4 | 331 | result = cmp(text1, text2) |
paul@4 | 332 | |
paul@4 | 333 | # Where the columns differ, return a result observing the sense |
paul@4 | 334 | # (ascending or descending) of the comparison for the column. |
paul@4 | 335 | |
paul@4 | 336 | if result != 0: |
paul@4 | 337 | return ascending and result or -result |
paul@4 | 338 | |
paul@4 | 339 | except ValueError: |
paul@4 | 340 | pass |
paul@4 | 341 | |
paul@4 | 342 | return 0 |
paul@4 | 343 | |
paul@0 | 344 | # Common formatting functions. |
paul@0 | 345 | |
paul@4 | 346 | def formatTable(text, request, fmt, attrs=None): |
paul@0 | 347 | |
paul@4 | 348 | """ |
paul@4 | 349 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@4 | 350 | The optional 'attrs' can be used to control the presentation of the table. |
paul@4 | 351 | """ |
paul@4 | 352 | |
paul@4 | 353 | table_attrs, table = parse(text) |
paul@4 | 354 | |
paul@4 | 355 | # Sort the rows according to the values in each of the specified columns. |
paul@0 | 356 | |
paul@4 | 357 | if attrs.has_key("sortcolumns"): |
paul@4 | 358 | data_start = int(attrs.get("headers", "1")) |
paul@4 | 359 | headers = table[:data_start] |
paul@4 | 360 | data = table[data_start:] |
paul@4 | 361 | |
paul@4 | 362 | # Get the sort columns using Unix sort-like notation. |
paul@0 | 363 | |
paul@4 | 364 | sorter = Sorter(get_sort_columns(attrs["sortcolumns"])) |
paul@4 | 365 | data.sort(cmp=sorter) |
paul@4 | 366 | |
paul@4 | 367 | table = headers + data |
paul@4 | 368 | |
paul@4 | 369 | # Write the table. |
paul@4 | 370 | |
paul@4 | 371 | request.write(fmt.table(1, table_attrs)) |
paul@0 | 372 | |
paul@0 | 373 | for row_attrs, columns in table: |
paul@0 | 374 | request.write(fmt.table_row(1, row_attrs)) |
paul@0 | 375 | |
paul@0 | 376 | for column_attrs, column_text in columns: |
paul@0 | 377 | request.write(fmt.table_cell(1, column_attrs)) |
paul@0 | 378 | request.write(formatText(column_text, request, fmt)) |
paul@0 | 379 | request.write(fmt.table_cell(0)) |
paul@0 | 380 | |
paul@0 | 381 | request.write(fmt.table_row(0)) |
paul@0 | 382 | |
paul@0 | 383 | request.write(fmt.table(0)) |
paul@0 | 384 | |
paul@0 | 385 | # vim: tabstop=4 expandtab shiftwidth=4 |