MoinLight

Changeset

291:e07397f18b2e
2020-05-03 Paul Boddie raw files shortlog changelog graph Fixed table cell attribute parsing where attributes are not actually being used. This tends to occur when the "<" attribute opening character is used to start normal cell text.
moinformat/parsers/moin.py (file) moinformat/tree/moin.py (file) tests/test_tables.tree (file) tests/test_tables.txt (file)
     1.1 --- a/moinformat/parsers/moin.py	Sun Apr 19 21:20:21 2020 +0200
     1.2 +++ b/moinformat/parsers/moin.py	Sun May 03 23:44:19 2020 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4  """
     1.5  Moin wiki format parser.
     1.6  
     1.7 -Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
     1.8 +Copyright (C) 2017, 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
     1.9  
    1.10  This program is free software; you can redistribute it and/or modify it under
    1.11  the terms of the GNU General Public License as published by the Free Software
    1.12 @@ -422,6 +422,15 @@
    1.13          attrs = TableAttrs([])
    1.14          self.parse_region_details(attrs, self.table_attr_pattern_names)
    1.15  
    1.16 +        # If no end marker was found, consider that the text was not table
    1.17 +        # attributes at all.
    1.18 +
    1.19 +        if attrs.incomplete:
    1.20 +            cell.append_inline(Text(serialise(attrs, self.get_serialiser())))
    1.21 +            if attrs.found_cell:
    1.22 +                self.end_region(cell)
    1.23 +            return
    1.24 +
    1.25          # Test the validity of the attributes.
    1.26  
    1.27          last = None
    1.28 @@ -653,17 +662,25 @@
    1.29  
    1.30          attrs.append(TableAttr(pattern_name, self.match_group("value"), True))
    1.31  
    1.32 -    def parse_colour(self, cell):
    1.33 -        self.parse_table_attr(cell, "bgcolor")
    1.34 +    def parse_colour(self, attrs):
    1.35 +        self.parse_table_attr(attrs, "bgcolor")
    1.36  
    1.37 -    def parse_colspan(self, cell):
    1.38 -        self.parse_table_attr(cell, "colspan")
    1.39 +    def parse_colspan(self, attrs):
    1.40 +        self.parse_table_attr(attrs, "colspan")
    1.41 +
    1.42 +    def parse_rowspan(self, attrs):
    1.43 +        self.parse_table_attr(attrs, "rowspan")
    1.44  
    1.45 -    def parse_rowspan(self, cell):
    1.46 -        self.parse_table_attr(cell, "rowspan")
    1.47 +    def parse_width(self, attrs):
    1.48 +        self.parse_table_attr(attrs, "width")
    1.49  
    1.50 -    def parse_width(self, cell):
    1.51 -        self.parse_table_attr(cell, "width")
    1.52 +    def parse_table_attrs_end(self, attrs):
    1.53 +        attrs.incomplete = False
    1.54 +        self.end_region(attrs)
    1.55 +
    1.56 +    def parse_table_attrs_cell(self, attrs):
    1.57 +        attrs.found_cell = True
    1.58 +        self.end_region(attrs)
    1.59  
    1.60  
    1.61  
    1.62 @@ -889,6 +906,8 @@
    1.63          "attrvalue"     : join(("=", group("quote", r"\Q"),                     # quote
    1.64                                       group("value", ".*?"),                     # non-quote... (optional)
    1.65                                       recur("quote"))),                          # quote
    1.66 +
    1.67 +        "bad_tablecell" : r"\|\|",                                              # ||
    1.68          }
    1.69  
    1.70      patterns = get_patterns(syntax)
    1.71 @@ -899,7 +918,8 @@
    1.72  
    1.73      table_attr_pattern_names = [
    1.74          "attrname", "colour", "colspan", "halign", "rowspan", "tableattrsend",
    1.75 -        "valign", "width"
    1.76 +        "valign", "width",
    1.77 +        "bad_tablecell"
    1.78          ]
    1.79  
    1.80      inline_without_links_pattern_names = [
    1.81 @@ -990,7 +1010,7 @@
    1.82          "super" : parse_super,
    1.83          "superend" : end_region,
    1.84          "tableattrs" : parse_table_attrs,
    1.85 -        "tableattrsend" : end_region,
    1.86 +        "tableattrsend" : parse_table_attrs_end,
    1.87          "tablerow" : parse_table_row,
    1.88          "tablecell" : end_region,
    1.89          "tableend" : end_region,
    1.90 @@ -998,6 +1018,7 @@
    1.91          "transclusionend" : end_region,
    1.92          "underline" : parse_underline,
    1.93          "underlineend" : end_region,
    1.94 +        "bad_tablecell" : parse_table_attrs_cell,
    1.95          "valign" : parse_valign,
    1.96          "verbatim" : parse_verbatim,
    1.97          "width" : parse_width,
     2.1 --- a/moinformat/tree/moin.py	Sun Apr 19 21:20:21 2020 +0200
     2.2 +++ b/moinformat/tree/moin.py	Sun May 03 23:44:19 2020 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4  """
     2.5  Moin wiki format document tree nodes.
     2.6  
     2.7 -Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
     2.8 +Copyright (C) 2017, 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
     2.9  
    2.10  This program is free software; you can redistribute it and/or modify it under
    2.11  the terms of the GNU General Public License as published by the Free Software
    2.12 @@ -466,6 +466,15 @@
    2.13  
    2.14      "A collection of table attributes."
    2.15  
    2.16 +    def __init__(self, nodes):
    2.17 +        Container.__init__(self, nodes)
    2.18 +
    2.19 +        # Parsing state flags, inconsequential to any final document tree.
    2.20 +        # If incomplete remains set, the attributes are discarded.
    2.21 +
    2.22 +        self.incomplete = True
    2.23 +        self.found_cell = False
    2.24 +
    2.25      def __repr__(self):
    2.26          return "TableAttrs(%r)" % self.nodes
    2.27  
    2.28 @@ -476,7 +485,8 @@
    2.29      def to_string(self, out):
    2.30          out.start_table_attrs()
    2.31          out.table_attrs(self.nodes)
    2.32 -        out.end_table_attrs()
    2.33 +        if not self.incomplete:
    2.34 +            out.end_table_attrs()
    2.35  
    2.36  class Table(Container):
    2.37  
     3.1 --- a/tests/test_tables.tree	Sun Apr 19 21:20:21 2020 +0200
     3.2 +++ b/tests/test_tables.tree	Sun May 03 23:44:19 2020 +0200
     3.3 @@ -49,4 +49,40 @@
     3.4        TableCell
     3.5          Macro
     3.6          Text
     3.7 +  Break
     3.8 +  Table
     3.9 +    TableRow
    3.10 +      TableCell
    3.11 +        Text
    3.12 +        FontStyle
    3.13 +          Text
    3.14 +        Text
    3.15 +      TableCell
    3.16 +        Text
    3.17 +        FontStyle
    3.18 +          Text
    3.19 +        Text
    3.20 +    TableRow
    3.21 +      TableCell
    3.22 +        Text
    3.23 +      TableCell
    3.24 +        Text
    3.25 +    TableRow
    3.26 +      TableCell
    3.27 +        Text
    3.28 +      TableCell
    3.29 +        Text
    3.30 +    TableRow
    3.31 +      TableCell
    3.32 +        Text
    3.33 +      TableCell
    3.34 +        Text
    3.35 +    TableRow
    3.36 +      TableCell
    3.37 +        Text
    3.38 +      TableCell
    3.39 +        Text
    3.40 +        FontStyle
    3.41 +          Text
    3.42 +        Text
    3.43    Block
     4.1 --- a/tests/test_tables.txt	Sun Apr 19 21:20:21 2020 +0200
     4.2 +++ b/tests/test_tables.txt	Sun May 03 23:44:19 2020 +0200
     4.3 @@ -9,3 +9,9 @@
     4.4  ||<20%%name="value"> Bad separator ||
     4.5  ||<20%xx-2> Strange attributes ||
     4.6  ||<<BR>> Macro, not attributes ||
     4.7 +
     4.8 +|| '''Value''' || '''Frequency''' ||
     4.9 +|| 0x01e0      || <= 45.25MHz     ||
    4.10 +|| 0x0140      || <= 92.50MHz     ||
    4.11 +|| 0x00a0      || <= 148.50MHz    ||
    4.12 +|| 0x0000      || ''Other''       ||