# HG changeset patch # User Paul Boddie # Date 1370819545 -7200 # Node ID 90465326c4dcfc153dce830833c8dd806c670c18 # Parent 16c6792343bc64ebfb21a07c7ad5be73bd4edbd9 Introduced parsing of block-level text in order to handle section-like macros. diff -r 16c6792343bc -r 90465326c4dc wikiparser.py --- a/wikiparser.py Sun Jun 09 20:41:26 2013 +0200 +++ b/wikiparser.py Mon Jun 10 01:12:25 2013 +0200 @@ -28,7 +28,7 @@ 2. Then, within these regions, the text is split into blocks. 1. First, lists are identified. 2. Additionally, other block-like elements are identified. - 3. Each block is then parsed. + 3. Each block is then split into regions. """ from common import * @@ -559,7 +559,7 @@ # Translate headings and blockquotes. if blocktypes.has_key(blocktype): - text = self.translate_content(blocktext) + text = self.translate_content(self.parse_text(blocktext)) for anchor in self.held_anchors: parts.append(anchor) parts.append(blocktypes[blocktype] % text) @@ -568,7 +568,7 @@ elif blocktype == "list": for listmarker, listitem in get_list_items(blocktext): - parts.append("%s %s" % (self.translate_marker(listmarker), self.translate_content(listitem))) + parts.append("%s %s" % (self.translate_marker(listmarker), self.translate_content(self.parse_text(listitem)))) # Translate table items. @@ -604,7 +604,7 @@ # Handle anonymous blocks. else: - parts.append(self.translate_content(blocktext)) + parts.append(self.translate_content(self.parse_text(blocktext))) if blocktype in headings: self.in_heading = False @@ -679,6 +679,16 @@ # Handle list, heading, blockquote or anonymous blocks. if type is None: + + # Where the region is the same as the provided text, return + # immediately. This is the base case of the recursive parsing + # process. + + if text == s: + return text + + # Otherwise, obtain and translate the blocks. + if preceded_by_block: parts.append("\n") @@ -707,9 +717,13 @@ parts += self.translate_section(sectiontype, options, text) preceded_by_block = True - # Translations of macros (which can look like sections). + # Translations of macros acting as sections. elif macrotypes.has_key(sectiontype): + + # Prevent the production of macros in places they would + # produce illegal Moin syntax. + if not self.forbids_macros(): self.macro = sectiontype argname = macroargs.get(sectiontype) @@ -718,6 +732,10 @@ "args" : quote_macro_argument((argname and ("%s=" % argname) or "") + options) }) self.macro = None + + # Include the contents of section-based macros where the + # macros themselves are not allowed. + else: parts.append(self.translate_content(text))