1.1 --- a/tests/test_sections_nested.txt Sun Jun 09 20:25:18 2013 +0200
1.2 +++ b/tests/test_sections_nested.txt Sun Jun 09 20:41:26 2013 +0200
1.3 @@ -1,2 +1,6 @@
1.4 ||Table heading||Another heading||
1.5 |{{preformatted}}|{{more preformatted text}}|
1.6 +
1.7 +{info}
1.8 +This is some {color:#ff0000}coloured text{color} in a section.
1.9 +{info}
2.1 --- a/wikiparser.py Sun Jun 09 20:25:18 2013 +0200
2.2 +++ b/wikiparser.py Sun Jun 09 20:41:26 2013 +0200
2.3 @@ -35,6 +35,7 @@
2.4 import re
2.5 import sys
2.6 import codecs
2.7 +import operator
2.8
2.9 # Section extraction.
2.10
2.11 @@ -381,6 +382,7 @@
2.12 self.in_heading = False
2.13 self.held_anchors = []
2.14 self.macro = None
2.15 + self.sections = []
2.16
2.17 def translate_marker(self, marker):
2.18
2.19 @@ -513,7 +515,7 @@
2.20 s = s.replace(before, after)
2.21 return s
2.22
2.23 - def translate_content(self, text, sectiontype=None):
2.24 + def translate_content(self, text):
2.25
2.26 """
2.27 Return a translation of the given 'text'. If the optional 'sectiontype' is
2.28 @@ -522,7 +524,7 @@
2.29 """
2.30
2.31 parts = []
2.32 - preformatted = sectiontype in preformatted_sectiontypes
2.33 + preformatted = self.is_preformatted()
2.34
2.35 last = 0
2.36 for match in content_regexp.finditer(text):
2.37 @@ -531,7 +533,7 @@
2.38
2.39 # Handle unformatted sections.
2.40
2.41 - if sectiontype in ("code", "noformat"):
2.42 + if self.sections and self.sections[-1] in ("code", "noformat"):
2.43 parts.append(match.group())
2.44 else:
2.45 parts.append(self.translate_content_match(match))
2.46 @@ -541,6 +543,9 @@
2.47 parts.append(self.translate_text(text[last:], preformatted))
2.48 return "".join(parts)
2.49
2.50 + def is_preformatted(self):
2.51 + return reduce(operator.or_, [x in preformatted_sectiontypes for x in self.sections], False)
2.52 +
2.53 def translate_block(self, blocktype, blocktext):
2.54
2.55 "Translate the block with the given 'blocktype' and 'blocktext'."
2.56 @@ -617,14 +622,16 @@
2.57
2.58 # Enter the section.
2.59
2.60 - self.enter_section()
2.61 + self.enter_section(sectiontype)
2.62
2.63 - mointype = sectiontypes.get(sectiontype)
2.64 - section_content = self.translate_content(text.strip(), sectiontype)
2.65 + # Sections can contain other sections.
2.66 +
2.67 + section_content = self.parse_text(text.strip())
2.68
2.69 # Nest the section appropriately.
2.70
2.71 opening, closing = self.nest_section()
2.72 + mointype = sectiontypes.get(sectiontype)
2.73
2.74 parts.append("%s%s\n" % (opening, mointype or ""))
2.75 if options:
2.76 @@ -638,14 +645,16 @@
2.77
2.78 return parts
2.79
2.80 - def enter_section(self):
2.81 + def enter_section(self, sectiontype=None):
2.82 self.level += 1
2.83 self.max_level = max(self.level, self.max_level)
2.84 + self.sections.append(sectiontype)
2.85
2.86 def leave_section(self):
2.87 self.level -= 1
2.88 if not self.level:
2.89 self.max_level = 0
2.90 + self.sections.pop()
2.91
2.92 def nest_section(self):
2.93 level = 3 + self.max_level - self.level