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@0 | 17 | # At start of line: |
paul@0 | 18 | "rows" : (r"^==", re.MULTILINE), # == |
paul@0 | 19 | # Within text: |
paul@0 | 20 | "columns" : (r"\|\|[ \t]*", 0), # || whitespace |
paul@0 | 21 | # At start of column text: |
paul@0 | 22 | "column" : (r"^\s*<(.*?)>\s*(.*)", re.DOTALL), # whitespace < attributes > whitespace |
paul@0 | 23 | } |
paul@0 | 24 | |
paul@0 | 25 | patterns = {} |
paul@0 | 26 | for name, (value, flags) in syntax.items(): |
paul@0 | 27 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 28 | |
paul@0 | 29 | # Functions. |
paul@0 | 30 | |
paul@0 | 31 | def parse(s): |
paul@0 | 32 | |
paul@0 | 33 | "Parse 's', returning a table definition." |
paul@0 | 34 | |
paul@0 | 35 | rows = [] |
paul@0 | 36 | table_attrs = {} |
paul@0 | 37 | |
paul@0 | 38 | # Extract each row from the definition. |
paul@0 | 39 | |
paul@0 | 40 | for row_text in patterns["rows"].split(s): |
paul@0 | 41 | columns = [] |
paul@0 | 42 | |
paul@0 | 43 | # Extract each column from the row. |
paul@0 | 44 | |
paul@0 | 45 | for text in patterns["columns"].split(row_text): |
paul@0 | 46 | |
paul@0 | 47 | # Extract the attribute and text sections. |
paul@0 | 48 | |
paul@0 | 49 | match = patterns["column"].search(text) |
paul@0 | 50 | if match: |
paul@0 | 51 | attribute_text, text = match.groups() |
paul@0 | 52 | columns.append((parseAttributes(attribute_text, True), text)) |
paul@0 | 53 | else: |
paul@0 | 54 | columns.append(({}, text)) |
paul@0 | 55 | |
paul@0 | 56 | # Extract row- and table-level attributes. |
paul@0 | 57 | |
paul@0 | 58 | row_attrs = {} |
paul@0 | 59 | |
paul@0 | 60 | if columns: |
paul@0 | 61 | attrs, column = columns[0] |
paul@0 | 62 | |
paul@0 | 63 | for name, value in attrs.items(): |
paul@0 | 64 | if name.startswith("row"): |
paul@0 | 65 | row_attrs[name] = value |
paul@0 | 66 | del attrs[name] |
paul@0 | 67 | elif name.startswith("table"): |
paul@0 | 68 | table_attrs[name] = value |
paul@0 | 69 | del attrs[name] |
paul@0 | 70 | |
paul@0 | 71 | rows.append((row_attrs, columns)) |
paul@0 | 72 | |
paul@0 | 73 | return table_attrs, rows |
paul@0 | 74 | |
paul@0 | 75 | def parseAttributes(s, escape=True): |
paul@0 | 76 | |
paul@0 | 77 | """ |
paul@0 | 78 | Parse the table attributes string 's', returning a mapping of names to |
paul@0 | 79 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@0 | 80 | for use with the formatter API. |
paul@0 | 81 | """ |
paul@0 | 82 | |
paul@0 | 83 | attrs = {} |
paul@0 | 84 | f = StringIO(s) |
paul@0 | 85 | name = None |
paul@0 | 86 | need_value = False |
paul@0 | 87 | |
paul@0 | 88 | for token in shlex(f): |
paul@0 | 89 | |
paul@0 | 90 | # Capture the name if needed. |
paul@0 | 91 | |
paul@0 | 92 | if name is None: |
paul@0 | 93 | name = escape and wikiutil.escape(token) or token |
paul@0 | 94 | |
paul@0 | 95 | # Detect either an equals sign or another name. |
paul@0 | 96 | |
paul@0 | 97 | elif not need_value: |
paul@0 | 98 | if token == "=": |
paul@0 | 99 | need_value = True |
paul@0 | 100 | else: |
paul@0 | 101 | attrs[name.lower()] = escape and "true" or True |
paul@0 | 102 | name = wikiutil.escape(token) |
paul@0 | 103 | |
paul@0 | 104 | # Otherwise, capture a value. |
paul@0 | 105 | |
paul@0 | 106 | else: |
paul@0 | 107 | # Quoting of attributes done similarly to parseAttributes. |
paul@0 | 108 | |
paul@0 | 109 | if escape and token: |
paul@0 | 110 | if token[0] in ("'", '"'): |
paul@0 | 111 | token = wikiutil.escape(token) |
paul@0 | 112 | else: |
paul@0 | 113 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@0 | 114 | |
paul@0 | 115 | attrs[name.lower()] = token |
paul@0 | 116 | name = None |
paul@0 | 117 | need_value = False |
paul@0 | 118 | |
paul@0 | 119 | return attrs |
paul@0 | 120 | |
paul@0 | 121 | # Formatting of embedded content. |
paul@0 | 122 | # NOTE: Borrowed from EventAggregator. |
paul@0 | 123 | |
paul@0 | 124 | def getParserClass(request, format): |
paul@0 | 125 | |
paul@0 | 126 | """ |
paul@0 | 127 | Return a parser class using the 'request' for the given 'format', returning |
paul@0 | 128 | a plain text parser if no parser can be found for the specified 'format'. |
paul@0 | 129 | """ |
paul@0 | 130 | |
paul@0 | 131 | try: |
paul@0 | 132 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@0 | 133 | except wikiutil.PluginMissingError: |
paul@0 | 134 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@0 | 135 | |
paul@0 | 136 | def formatText(text, request, fmt): |
paul@0 | 137 | |
paul@0 | 138 | "Format the given 'text' using the specified 'request' and formatter 'fmt'." |
paul@0 | 139 | |
paul@0 | 140 | parser_cls = getParserClass(request, request.page.pi["format"]) |
paul@0 | 141 | parser = parser_cls(text, request, line_anchors=False) |
paul@0 | 142 | return request.redirectedOutput(parser.format, fmt, inhibit_p=True) |
paul@0 | 143 | |
paul@0 | 144 | # Common formatting functions. |
paul@0 | 145 | |
paul@0 | 146 | def formatTable(text, request, fmt): |
paul@0 | 147 | |
paul@0 | 148 | "Format the given 'text' using the specified 'request' and formatter 'fmt'." |
paul@0 | 149 | |
paul@0 | 150 | attrs, table = parse(text) |
paul@0 | 151 | |
paul@0 | 152 | request.write(fmt.table(1, attrs)) |
paul@0 | 153 | |
paul@0 | 154 | for row_attrs, columns in table: |
paul@0 | 155 | request.write(fmt.table_row(1, row_attrs)) |
paul@0 | 156 | |
paul@0 | 157 | for column_attrs, column_text in columns: |
paul@0 | 158 | request.write(fmt.table_cell(1, column_attrs)) |
paul@0 | 159 | request.write(formatText(column_text, request, fmt)) |
paul@0 | 160 | request.write(fmt.table_cell(0)) |
paul@0 | 161 | |
paul@0 | 162 | request.write(fmt.table_row(0)) |
paul@0 | 163 | |
paul@0 | 164 | request.write(fmt.table(0)) |
paul@0 | 165 | |
paul@0 | 166 | # vim: tabstop=4 expandtab shiftwidth=4 |