1.1 --- a/moinformat/parsers/moin.py Fri Aug 03 22:34:55 2018 +0200
1.2 +++ b/moinformat/parsers/moin.py Sat Aug 04 16:57:49 2018 +0200
1.3 @@ -70,6 +70,10 @@
1.4
1.5 self.macros = []
1.6
1.7 + # Record headings for identifier disambiguation.
1.8 +
1.9 + self.headings = []
1.10 +
1.11 # Principal parser methods.
1.12
1.13 def parse(self, s):
1.14 @@ -96,6 +100,10 @@
1.15 else:
1.16 self.parse_region_type(self.region)
1.17
1.18 + # Assign heading identifiers.
1.19 +
1.20 + self.identify_headings()
1.21 +
1.22 return self.region
1.23
1.24
1.25 @@ -119,6 +127,24 @@
1.26 macro = macro_cls(node, self.region)
1.27 macro.evaluate()
1.28
1.29 + # Heading disambiguation.
1.30 +
1.31 + def identify_headings(self):
1.32 +
1.33 + "Assign identifiers to headings based on their textual content."
1.34 +
1.35 + d = {}
1.36 +
1.37 + for heading in self.headings:
1.38 + text = heading.text_content()
1.39 +
1.40 + if not d.has_key(text):
1.41 + d[text] = 0
1.42 + heading.identifier = text
1.43 + else:
1.44 + d[text] += 1
1.45 + heading.identifier = "%s-%d" % (text, d[text])
1.46 +
1.47
1.48
1.49 # Parser methods supporting different page features.
1.50 @@ -238,6 +264,10 @@
1.51 self.add_node(region, heading)
1.52 self.new_block(region)
1.53
1.54 + # Record the heading for later processing.
1.55 +
1.56 + self.root.headings.append(heading)
1.57 +
1.58 def parse_heading_end(self, heading):
1.59
1.60 "Handle the end of a heading."