# HG changeset patch # User Paul Boddie # Date 1315759816 -7200 # Node ID a2e8615152cfdd14e8749e4fba980e55d1a1221f # Parent 5e3955c2714ba2964097db2142d5982887df5e7a Introduced detection of certain syntactic features that should end sections, such as horizontal rules and Include macro invocations, as well as headings. diff -r 5e3955c2714b -r a2e8615152cf MoinContentSupport.py --- a/MoinContentSupport.py Sat Feb 26 21:45:56 2011 +0100 +++ b/MoinContentSupport.py Sun Sep 11 18:50:16 2011 +0200 @@ -16,30 +16,40 @@ # Regular expressions. # NOTE: These overlap with ImprovedMoinSearch and EventAggregator. -heading_regexp = re.compile(r"^(?P=+)\s*(?P.*?)\s*(?P=level)$", re.UNICODE | re.MULTILINE) +heading_regexp_str = r"^(?P=+)\s*(?P.*?)\s*(?P=level)$" +hrule_regexp_str = r"^----$" +include_regexp_str = r"^\s*<>$" +section_regexp_str = "(" + heading_regexp_str + "|" + hrule_regexp_str + "|" + include_regexp_str + ")" +section_regexp = re.compile(section_regexp_str, re.UNICODE | re.MULTILINE) category_membership_str = ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$" category_membership_regexp = re.compile(category_membership_str, re.MULTILINE | re.UNICODE) category_declarations_regexp = re.compile("^----$\s*" + category_membership_str, re.MULTILINE | re.UNICODE) -def getHeadingDetails(body, min_level=None, max_level=None): +def getSectionDetails(body, min_level=None, max_level=None): """ - Return heading details from the given 'body' for headings with the given + Return section details from the given 'body' for headings with the given 'min_level' to 'max_level' range. Specifying None or omitting 'max_level' or 'min_level' removes the appropriate constraint on the range. - A list of tuples containing the heading, the level, and the span (the start - offset and the end offset as a tuple) is returned. + A list of tuples is returned for each section divider. For headings, the + heading text and level number are returned as the first and second elements + of each tuple, whereas other section dividers (typically indicating the end + of a section) employ None as the first and second elements. The span (start + offset and end offset of the divider) is provided as a tuple in the third + element of each result tuple. """ headings = [] - for match in heading_regexp.finditer(body): - level = len(match.group("level")) + for match in section_regexp.finditer(body): + level = match.group("level") + level = level and len(match.group("level")) if (min_level is None or min_level <= level) and \ - (max_level is None or level <= max_level): + (max_level is None or level <= max_level) or \ + level is None: headings.append((match.group("heading"), level, match.span())) diff -r 5e3955c2714b -r a2e8615152cf actions/SectionBreakout.py --- a/actions/SectionBreakout.py Sat Feb 26 21:45:56 2011 +0100 +++ b/actions/SectionBreakout.py Sun Sep 11 18:50:16 2011 +0200 @@ -33,7 +33,7 @@ # Acquire heading details from the page. body = page.get_raw_body() - heading_details = getHeadingDetails(body, level, level) + heading_details = getSectionDetails(body, level, level) d = { "buttons_html" : buttons_html, @@ -55,7 +55,8 @@ ''' % d for heading, level, span in heading_details: - html += "%s
" % heading + if heading is not None: + html += "%s
" % escape(heading) html += ''' @@ -122,25 +123,36 @@ regions = [] current_region_start = None - for heading, found_level, (start, end) in getHeadingDetails(page_body): + for heading, found_level, (start, end) in getSectionDetails(page_body): - # Upon finding a suitable heading, begin a new region to be broken - # out. + # Where a heading is provided, consider starting a section. + + if found_level is not None: - if current_region_start is None and found_level >= level: - current_region_start = heading, start + # Upon finding a suitable heading, begin a new region to be broken + # out. - # Upon finding a higher-level heading, end any open region. + if current_region_start is None and found_level >= level: + current_region_start = heading, start + + # Upon finding a same-level or higher-level heading, end any open + # region. - elif current_region_start is not None and found_level <= level: - regions.append(current_region_start + (start,)) + elif current_region_start is not None and found_level <= level: + regions.append(current_region_start + (start,)) - # For headings at the requested level, open a new region. + # For headings at the requested level, open a new region. - if found_level == level: - current_region_start = heading, start - else: - current_region_start = None + if found_level == level: + current_region_start = heading, start + else: + current_region_start = None + + # Where no heading is provided, end the section. + + elif current_region_start is not None: + regions.append(current_region_start + (start,)) + current_region_start = None # End any open region.