1.1 --- a/wikiparser.py Sun Jun 09 19:53:44 2013 +0200
1.2 +++ b/wikiparser.py Sun Jun 09 20:25:18 2013 +0200
1.3 @@ -49,12 +49,13 @@
1.4 """
1.5
1.6 last = 0
1.7 - regions = []
1.8 + regions = [""]
1.9 depth = 0
1.10
1.11 for match in sections_regexp.finditer(s):
1.12 start, end = match.span()
1.13 is_start = match.group("options")
1.14 + is_section = is_section_marker(match.group("type"))
1.15
1.16 # The start of a region is either indicated by a marker with options or
1.17 # by a marker where no region is currently active.
1.18 @@ -65,11 +66,17 @@
1.19 # "null" region.
1.20
1.21 if not depth:
1.22 - regions.append((None, s[last:start]))
1.23 + regions[-1] += s[last:start]
1.24
1.25 # A new region is maintained as a string.
1.26
1.27 - regions.append(s[start:end])
1.28 + if is_section:
1.29 + regions.append(s[start:end])
1.30 +
1.31 + # Certain markers may be standalone macros.
1.32 +
1.33 + else:
1.34 + regions[-1] += s[start:end]
1.35
1.36 # Where a region is active, add the text since the last match as
1.37 # well as the text in this match to the region.
1.38 @@ -77,10 +84,8 @@
1.39 else:
1.40 regions[-1] += s[last:end]
1.41
1.42 - # Any start marker will cause an increase in the depth of the region
1.43 - # nesting.
1.44 -
1.45 - depth += 1
1.46 + if is_section:
1.47 + depth += 1
1.48
1.49 # The end of a region is indicated by a marker with no options.
1.50
1.51 @@ -92,7 +97,7 @@
1.52
1.53 # Add to the string portion of the "null" region.
1.54
1.55 - regions[-1][1] += s[last:end]
1.56 + regions[-1] += s[last:end]
1.57
1.58 # Where a region is active, the end marker and preceding text is
1.59 # either incorporated into the current region if more than one
1.60 @@ -100,28 +105,28 @@
1.61 # current region and the details of the region are then obtained.
1.62
1.63 else:
1.64 - if depth > 1:
1.65 + if depth > 1 or not is_section:
1.66 regions[-1] += s[last:end]
1.67
1.68 # Terminate the active region, interpreting its contents.
1.69
1.70 else:
1.71 - regions[-1] = get_section_details(regions[-1] + s[last:end])
1.72 - depth -= 1
1.73 + regions[-1] += s[last:end]
1.74 + regions.append("")
1.75 +
1.76 + if is_section:
1.77 + depth -= 1
1.78
1.79 last = end
1.80
1.81 # Where a region is still active, terminate it.
1.82
1.83 - if depth:
1.84 - regions[-1] = get_section_details(regions[-1] + s[last:])
1.85 -
1.86 - # Otherwise, add a "null" region.
1.87 + regions[-1] += s[last:]
1.88
1.89 - else:
1.90 - regions.append((None, s[last:]))
1.91 + return [get_section_details(s) for s in regions if s]
1.92
1.93 - return regions
1.94 +def is_section_marker(sectiontype):
1.95 + return sectiontypes.has_key(sectiontype) or sectiontype == "color"
1.96
1.97 # Section inspection.
1.98
1.99 @@ -375,6 +380,7 @@
1.100 self.max_level = self.level = 0
1.101 self.in_heading = False
1.102 self.held_anchors = []
1.103 + self.macro = None
1.104
1.105 def translate_marker(self, marker):
1.106
1.107 @@ -694,12 +700,18 @@
1.108
1.109 # Translations of macros (which can look like sections).
1.110
1.111 - elif macrotypes.has_key(sectiontype) and not self.forbids_macros():
1.112 - argname = macroargs.get(sectiontype)
1.113 - parts.append(macrotypes[sectiontype] % {
1.114 - "content" : quote_macro_argument(self.parse_text(text)),
1.115 - "args" : quote_macro_argument((argname and ("%s=" % argname) or "") + options)
1.116 - })
1.117 + elif macrotypes.has_key(sectiontype):
1.118 + if not self.forbids_macros():
1.119 + self.macro = sectiontype
1.120 + argname = macroargs.get(sectiontype)
1.121 + parts.append(macrotypes[sectiontype] % {
1.122 + "content" : quote_macro_argument(self.parse_text(text)),
1.123 + "args" : quote_macro_argument((argname and ("%s=" % argname) or "") + options)
1.124 + })
1.125 + self.macro = None
1.126 + else:
1.127 + parts.append(self.translate_content(text))
1.128 +
1.129 preceded_by_block = False
1.130
1.131 # Unrecognised sections.
1.132 @@ -711,7 +723,7 @@
1.133 return "".join(parts)
1.134
1.135 def forbids_macros(self):
1.136 - return self.in_heading
1.137 + return self.in_heading or self.macro
1.138
1.139 def parse(s, out):
1.140