1.1 --- a/moinformat/parsers/common.py Sun Jul 15 14:18:57 2018 +0200
1.2 +++ b/moinformat/parsers/common.py Sun Jul 15 14:19:42 2018 +0200
1.3 @@ -118,7 +118,7 @@
1.4 else:
1.5 return self.s[self.pos:self.start]
1.6
1.7 - def read_match(self, group=1):
1.8 + def match_group(self, group=1):
1.9
1.10 """
1.11 Return the matched text, updating the position in the stream. If 'group'
1.12 @@ -127,25 +127,39 @@
1.13 within group 1 can provide sections of the data.
1.14 """
1.15
1.16 + self.update_pos()
1.17 +
1.18 if self.match:
1.19 - _start, self.pos = self.match.span()
1.20 try:
1.21 return self.match.group(group)
1.22 except IndexError:
1.23 return ""
1.24 else:
1.25 - self.pos = len(self.s)
1.26 return None
1.27
1.28 - def match_groups(self):
1.29 + def match_groups(self, groups=None):
1.30
1.31 - "Return the match groups."
1.32 + "Return the match 'groups', or all groups if unspecified."
1.33 +
1.34 + self.update_pos()
1.35
1.36 if self.match:
1.37 - return self.match.groups()
1.38 + if groups is None:
1.39 + return self.match.groups()
1.40 + else:
1.41 + return self.match.groups(groups)
1.42 else:
1.43 return []
1.44
1.45 + def update_pos(self):
1.46 +
1.47 + "Update the position in the stream."
1.48 +
1.49 + if self.match:
1.50 + _start, self.pos = self.match.span()
1.51 + else:
1.52 + self.pos = len(self.s)
1.53 +
1.54
1.55
1.56 # Parser abstractions.
1.57 @@ -209,15 +223,15 @@
1.58
1.59 return self.items.read_until(self.get_patterns(pattern_names))
1.60
1.61 - def read_match(self, group=1):
1.62 + def match_group(self, group=1):
1.63
1.64 """
1.65 Return the group of the matching pattern with the given 'group' number.
1.66 """
1.67
1.68 - return self.items.read_match(group)
1.69 + return self.items.match_group(group)
1.70
1.71 - def read_matching(self):
1.72 + def matching_pattern(self):
1.73
1.74 "Return the name of the matching pattern."
1.75
1.76 @@ -303,7 +317,7 @@
1.77 """
1.78
1.79 if self.read_until(["header"], False) == "": # None means no header
1.80 - region.type = self.read_match()
1.81 + region.type = self.match_group()
1.82
1.83 def parse_region_opaque(self, region):
1.84
1.85 @@ -349,13 +363,13 @@
1.86
1.87 # End of input.
1.88
1.89 - if not self.read_matching():
1.90 + if not self.matching_pattern():
1.91 break
1.92
1.93 # Obtain any feature.
1.94
1.95 - feature = self.read_match()
1.96 - handler = self.handlers.get(self.read_matching())
1.97 + feature = self.match_group()
1.98 + handler = self.handlers.get(self.matching_pattern())
1.99
1.100 # Handle each feature or add text to the region.
1.101
2.1 --- a/moinformat/parsers/moin.py Sun Jul 15 14:18:57 2018 +0200
2.2 +++ b/moinformat/parsers/moin.py Sun Jul 15 14:19:42 2018 +0200
2.3 @@ -82,13 +82,13 @@
2.4
2.5 "Handle an attribute name within 'attrs'."
2.6
2.7 - name = self.read_match()
2.8 + name = self.match_group()
2.9 attr = TableAttr(name)
2.10
2.11 preceding = self.read_until(["attrvalue"], False)
2.12 if preceding == "":
2.13 - attr.quote = self.read_match(1)
2.14 - attr.value = self.read_match(2)
2.15 + attr.quote = self.match_group(1)
2.16 + attr.value = self.match_group(2)
2.17
2.18 attrs.append(attr)
2.19
2.20 @@ -103,7 +103,7 @@
2.21
2.22 "Handle a definition item within 'region'."
2.23
2.24 - pad = self.read_match(1)
2.25 + pad = self.match_group(1)
2.26 item = DefItem([], pad, extra)
2.27 self.parse_region_details(item, ["listitemend"])
2.28 self.add_node(region, item)
2.29 @@ -113,18 +113,18 @@
2.30
2.31 "Handle a definition term within 'region'."
2.32
2.33 - pad = self.read_match(1)
2.34 + pad = self.match_group(1)
2.35 term = DefTerm([], pad)
2.36 self.parse_region_details(term, ["deftermend", "deftermsep"])
2.37 self.add_node(region, term)
2.38 - if self.read_matching() == "deftermsep":
2.39 + if self.matching_pattern() == "deftermsep":
2.40 self.parse_defitem(region)
2.41
2.42 def parse_defterm_empty(self, region):
2.43
2.44 "Handle an empty definition term within 'region'."
2.45
2.46 - extra = self.read_match(1)
2.47 + extra = self.match_group(1)
2.48 self.parse_region_details(region, ["deftermsep"])
2.49 self.parse_defitem(region, extra)
2.50
2.51 @@ -132,7 +132,7 @@
2.52
2.53 "Handle emphasis and strong styles."
2.54
2.55 - n = len(self.read_match(1))
2.56 + n = len(self.match_group(1))
2.57
2.58 # Handle endings.
2.59
2.60 @@ -171,7 +171,7 @@
2.61
2.62 "Handle horizontal alignment within 'attrs'."
2.63
2.64 - value = self.read_match()
2.65 + value = self.match_group()
2.66 attr = TableAttr("halign", value == "(" and "left" or value == ")" and "right" or "center", True)
2.67 attrs.append(attr)
2.68
2.69 @@ -179,9 +179,9 @@
2.70
2.71 "Handle a heading."
2.72
2.73 - start_extra = self.read_match(1)
2.74 - level = len(self.read_match(2))
2.75 - start_pad = self.read_match(3)
2.76 + start_extra = self.match_group(1)
2.77 + level = len(self.match_group(2))
2.78 + start_pad = self.match_group(3)
2.79 heading = Heading([], level, start_extra, start_pad)
2.80 self.parse_region_details(heading, ["headingend"] + self.inline_pattern_names)
2.81 self.add_node(region, heading)
2.82 @@ -191,10 +191,10 @@
2.83
2.84 "Handle the end of a heading."
2.85
2.86 - level = len(self.read_match(2))
2.87 + level = len(self.match_group(2))
2.88 if heading.level == level:
2.89 - heading.end_pad = self.read_match(1)
2.90 - heading.end_extra = self.read_match(3)
2.91 + heading.end_pad = self.match_group(1)
2.92 + heading.end_extra = self.match_group(3)
2.93 raise StopIteration
2.94
2.95 def parse_list(self, item):
2.96 @@ -211,12 +211,12 @@
2.97
2.98 final = len(self.match_groups())
2.99
2.100 - indent = len(self.read_match(1))
2.101 - marker = self.read_match(2)
2.102 - space = self.read_match(final)
2.103 + indent = len(self.match_group(1))
2.104 + marker = self.match_group(2)
2.105 + space = self.match_group(final)
2.106
2.107 if final > 3:
2.108 - num = self.read_match(3)
2.109 + num = self.match_group(3)
2.110 else:
2.111 num = None
2.112
2.113 @@ -265,7 +265,7 @@
2.114
2.115 "Handle a horizontal rule within 'region'."
2.116
2.117 - length = len(self.read_match(1))
2.118 + length = len(self.match_group(1))
2.119 rule = Rule(length)
2.120 self.add_node(region, rule)
2.121 self.new_block(region)
2.122 @@ -276,8 +276,8 @@
2.123
2.124 # Parse the section and start a new block after the section.
2.125
2.126 - indent = len(self.read_match(2))
2.127 - level = len(self.read_match(3))
2.128 + indent = len(self.match_group(2))
2.129 + level = len(self.match_group(3))
2.130 self.add_node(region, self.parse_region(level, indent, "inline"))
2.131 self.new_block(region)
2.132
2.133 @@ -285,7 +285,7 @@
2.134
2.135 "Handle the end of a new section within 'region'."
2.136
2.137 - feature = self.read_match()
2.138 + feature = self.match_group()
2.139 if region.have_end(feature):
2.140 raise StopIteration
2.141 else:
2.142 @@ -349,8 +349,8 @@
2.143
2.144 # Handle the end of the row.
2.145
2.146 - if self.read_matching() == "tableend":
2.147 - trailing = self.read_match()
2.148 + if self.matching_pattern() == "tableend":
2.149 + trailing = self.match_group()
2.150
2.151 # If the cell was started but not finished, convert the row into text.
2.152
2.153 @@ -388,7 +388,7 @@
2.154
2.155 "Handle vertical alignment within 'attrs'."
2.156
2.157 - value = self.read_match()
2.158 + value = self.match_group()
2.159 attr = TableAttr("valign", value == "^" and "top" or "bottom", True)
2.160 attrs.append(attr)
2.161
2.162 @@ -408,8 +408,8 @@
2.163 self.parse_inline(region, Larger, "larger")
2.164
2.165 def parse_link(self, region):
2.166 - target = self.read_match(1)
2.167 - text = self.read_match(2)
2.168 + target = self.match_group(1)
2.169 + text = self.match_group(2)
2.170 link = Link(text and [Text(text)], target)
2.171 region.append_inline(link)
2.172
2.173 @@ -439,7 +439,7 @@
2.174
2.175 "Handle a table attribute."
2.176
2.177 - attrs.append(TableAttr(pattern_name, self.read_match(), True))
2.178 + attrs.append(TableAttr(pattern_name, self.match_group(), True))
2.179
2.180 def parse_colour(self, cell):
2.181 self.parse_table_attr(cell, "colour")
3.1 --- a/moinformat/parsers/table.py Sun Jul 15 14:18:57 2018 +0200
3.2 +++ b/moinformat/parsers/table.py Sun Jul 15 14:19:42 2018 +0200
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Moin wiki table parser.
3.6
3.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -54,14 +54,16 @@
3.13
3.14 # Detect the end of the table.
3.15
3.16 - if self.read_matching() == "regionend":
3.17 + pattern = self.matching_pattern()
3.18 +
3.19 + if pattern == "regionend":
3.20 break
3.21
3.22 - if self.read_matching() == "columnsep":
3.23 + elif pattern == "columnsep":
3.24 cell = TableCell([])
3.25 row.append(cell)
3.26
3.27 - elif self.read_matching() == "rowsep":
3.28 + elif pattern == "rowsep":
3.29 row = TableRow([])
3.30 table.append(row)
3.31 cell = TableCell([])
3.32 @@ -76,7 +78,7 @@
3.33
3.34 "Handle the end of a region within 'cell'."
3.35
3.36 - feature = self.read_match()
3.37 + feature = self.match_group()
3.38 if self.region.have_end(feature):
3.39 raise StopIteration
3.40 else: