# HG changeset patch # User Paul Boddie # Date 1298235755 -3600 # Node ID cf4986b6fb395a27b094c5f2de198a4724f5eb66 # Parent 7a7c02523fb23068868c2fb11f781e45722a7ed7 Made sure that categories are propagated to subpages. diff -r 7a7c02523fb2 -r cf4986b6fb39 MoinContentSupport.py --- a/MoinContentSupport.py Sun Feb 20 21:15:05 2011 +0100 +++ b/MoinContentSupport.py Sun Feb 20 22:02:35 2011 +0100 @@ -14,9 +14,10 @@ __version__ = "0.1" # Regular expressions. -# NOTE: These overlap with ImprovedMoinSearch. +# NOTE: These overlap with ImprovedMoinSearch and EventAggregator. heading_regexp = re.compile(r"^(?P=+)\s*(?P.*?)\s*(?P=level)$", re.UNICODE | re.MULTILINE) +category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) def getHeadingDetails(body, min_level=None, max_level=None): @@ -41,6 +42,22 @@ return headings +def getCategoryMembership(body): + + "From the given 'body', return the categories the page belongs to." + + match = category_membership_regexp.search(body) + if match: + return [x for x in match.groups() if x] + else: + return [] + +def getCategoryDeclaration(categories): + + "Return a category declaration string for the given 'categories'." + + return "\n----\n%s\n" % " ".join(categories) + # Utility classes and associated functions. # NOTE: These are a subset of EventAggregatorSupport. diff -r 7a7c02523fb2 -r cf4986b6fb39 actions/SectionBreakout.py --- a/actions/SectionBreakout.py Sun Feb 20 21:15:05 2011 +0100 +++ b/actions/SectionBreakout.py Sun Feb 20 22:02:35 2011 +0100 @@ -13,7 +13,7 @@ from MoinMoin.action import ActionBase from MoinMoin.PageEditor import PageEditor -from MoinContentSupport import ActionSupport, getHeadingDetails, escape, escattr +from MoinContentSupport import * import re # Action class and supporting functions. @@ -108,6 +108,8 @@ # Acquire all heading details from the page. page_body = page.get_raw_body() + categories = getCategoryMembership(page_body) + regions = [] current_region_start = None @@ -141,6 +143,7 @@ retained_regions = [] retained_region_start = 0 + new_page_names = {} for heading, start, end in regions: @@ -148,13 +151,29 @@ new_page_name = "%s/%s" % (page.page_name, heading) + # Distinguish between pages which use the same heading. + + n = new_page_names.get(new_page_name, 0) + if n: + new_page_names[new_page_name] = n + 1 + new_page_name += " (%d)" % (n + 1) + else: + new_page_names[new_page_name] = n + 1 + # Open the page for editing. new_page = PageEditor(request, new_page_name) + new_page_body = page_body[start:end] + new_page_categories = getCategoryMembership(new_page_body) - # Save the new version of the page. + # Add categories if the parent page has any. - new_page.saveText(page_body[start:end], 0) + if new_page_categories != categories: + new_page_body += getCategoryDeclaration(categories) + + # Save the new page. + + new_page.saveText(new_page_body, 0) # Retain the preceding region for the current page. @@ -176,7 +195,18 @@ # Edit the current page. edited_page = PageEditor(request, page.page_name) - edited_page.saveText("".join(retained_regions), 0) + edited_page_body = "".join(retained_regions) + edited_page_categories = getCategoryMembership(edited_page_body) + + # Add categories if the parent page should have any, but these were + # broken out. + + if edited_page_categories != categories: + edited_page_body += getCategoryDeclaration(categories) + + # Save the current page. + + edited_page.saveText(edited_page_body, 0) # NOTE: Perhaps show a message upon failure.