# HG changeset patch # User Paul Boddie # Date 1370803286 -7200 # Node ID 16c6792343bc64ebfb21a07c7ad5be73bd4edbd9 # Parent baa808cc348848936833e6af2b0c030da8057a4b Introduced the parsing of nested sections (in order to prevent the false identification of macros) and the tracking of sections (in order to determine whether a preformatted region is active). diff -r baa808cc3488 -r 16c6792343bc tests/test_sections_nested.txt --- a/tests/test_sections_nested.txt Sun Jun 09 20:25:18 2013 +0200 +++ b/tests/test_sections_nested.txt Sun Jun 09 20:41:26 2013 +0200 @@ -1,2 +1,6 @@ ||Table heading||Another heading|| |{{preformatted}}|{{more preformatted text}}| + +{info} +This is some {color:#ff0000}coloured text{color} in a section. +{info} diff -r baa808cc3488 -r 16c6792343bc wikiparser.py --- a/wikiparser.py Sun Jun 09 20:25:18 2013 +0200 +++ b/wikiparser.py Sun Jun 09 20:41:26 2013 +0200 @@ -35,6 +35,7 @@ import re import sys import codecs +import operator # Section extraction. @@ -381,6 +382,7 @@ self.in_heading = False self.held_anchors = [] self.macro = None + self.sections = [] def translate_marker(self, marker): @@ -513,7 +515,7 @@ s = s.replace(before, after) return s - def translate_content(self, text, sectiontype=None): + def translate_content(self, text): """ Return a translation of the given 'text'. If the optional 'sectiontype' is @@ -522,7 +524,7 @@ """ parts = [] - preformatted = sectiontype in preformatted_sectiontypes + preformatted = self.is_preformatted() last = 0 for match in content_regexp.finditer(text): @@ -531,7 +533,7 @@ # Handle unformatted sections. - if sectiontype in ("code", "noformat"): + if self.sections and self.sections[-1] in ("code", "noformat"): parts.append(match.group()) else: parts.append(self.translate_content_match(match)) @@ -541,6 +543,9 @@ parts.append(self.translate_text(text[last:], preformatted)) return "".join(parts) + def is_preformatted(self): + return reduce(operator.or_, [x in preformatted_sectiontypes for x in self.sections], False) + def translate_block(self, blocktype, blocktext): "Translate the block with the given 'blocktype' and 'blocktext'." @@ -617,14 +622,16 @@ # Enter the section. - self.enter_section() + self.enter_section(sectiontype) - mointype = sectiontypes.get(sectiontype) - section_content = self.translate_content(text.strip(), sectiontype) + # Sections can contain other sections. + + section_content = self.parse_text(text.strip()) # Nest the section appropriately. opening, closing = self.nest_section() + mointype = sectiontypes.get(sectiontype) parts.append("%s%s\n" % (opening, mointype or "")) if options: @@ -638,14 +645,16 @@ return parts - def enter_section(self): + def enter_section(self, sectiontype=None): self.level += 1 self.max_level = max(self.level, self.max_level) + self.sections.append(sectiontype) def leave_section(self): self.level -= 1 if not self.level: self.max_level = 0 + self.sections.pop() def nest_section(self): level = 3 + self.max_level - self.level