MoinContentSuite

Changeset

4:cf4986b6fb39
2011-02-20 Paul Boddie raw files shortlog changelog graph Made sure that categories are propagated to subpages.
MoinContentSupport.py (file) actions/SectionBreakout.py (file)
     1.1 --- a/MoinContentSupport.py	Sun Feb 20 21:15:05 2011 +0100
     1.2 +++ b/MoinContentSupport.py	Sun Feb 20 22:02:35 2011 +0100
     1.3 @@ -14,9 +14,10 @@
     1.4  __version__ = "0.1"
     1.5  
     1.6  # Regular expressions.
     1.7 -# NOTE: These overlap with ImprovedMoinSearch.
     1.8 +# NOTE: These overlap with ImprovedMoinSearch and EventAggregator.
     1.9  
    1.10  heading_regexp = re.compile(r"^(?P<level>=+)\s*(?P<heading>.*?)\s*(?P=level)$", re.UNICODE | re.MULTILINE)
    1.11 +category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE)
    1.12  
    1.13  def getHeadingDetails(body, min_level=None, max_level=None):
    1.14  
    1.15 @@ -41,6 +42,22 @@
    1.16  
    1.17      return headings
    1.18  
    1.19 +def getCategoryMembership(body):
    1.20 +
    1.21 +    "From the given 'body', return the categories the page belongs to."
    1.22 +
    1.23 +    match = category_membership_regexp.search(body)
    1.24 +    if match:
    1.25 +        return [x for x in match.groups() if x]
    1.26 +    else:
    1.27 +        return []
    1.28 +
    1.29 +def getCategoryDeclaration(categories):
    1.30 +
    1.31 +    "Return a category declaration string for the given 'categories'."
    1.32 +
    1.33 +    return "\n----\n%s\n" % " ".join(categories)
    1.34 +
    1.35  # Utility classes and associated functions.
    1.36  # NOTE: These are a subset of EventAggregatorSupport.
    1.37  
     2.1 --- a/actions/SectionBreakout.py	Sun Feb 20 21:15:05 2011 +0100
     2.2 +++ b/actions/SectionBreakout.py	Sun Feb 20 22:02:35 2011 +0100
     2.3 @@ -13,7 +13,7 @@
     2.4  
     2.5  from MoinMoin.action import ActionBase
     2.6  from MoinMoin.PageEditor import PageEditor
     2.7 -from MoinContentSupport import ActionSupport, getHeadingDetails, escape, escattr
     2.8 +from MoinContentSupport import *
     2.9  import re
    2.10  
    2.11  # Action class and supporting functions.
    2.12 @@ -108,6 +108,8 @@
    2.13          # Acquire all heading details from the page.
    2.14  
    2.15          page_body = page.get_raw_body()
    2.16 +        categories = getCategoryMembership(page_body)
    2.17 +
    2.18          regions = []
    2.19          current_region_start = None
    2.20  
    2.21 @@ -141,6 +143,7 @@
    2.22  
    2.23          retained_regions = []
    2.24          retained_region_start = 0
    2.25 +        new_page_names = {}
    2.26  
    2.27          for heading, start, end in regions:
    2.28  
    2.29 @@ -148,13 +151,29 @@
    2.30  
    2.31              new_page_name = "%s/%s" % (page.page_name, heading)
    2.32  
    2.33 +            # Distinguish between pages which use the same heading.
    2.34 +
    2.35 +            n = new_page_names.get(new_page_name, 0)
    2.36 +            if n:
    2.37 +                new_page_names[new_page_name] = n + 1
    2.38 +                new_page_name += " (%d)" % (n + 1)
    2.39 +            else:
    2.40 +                new_page_names[new_page_name] = n + 1
    2.41 +
    2.42              # Open the page for editing.
    2.43  
    2.44              new_page = PageEditor(request, new_page_name)
    2.45 +            new_page_body = page_body[start:end]
    2.46 +            new_page_categories = getCategoryMembership(new_page_body)
    2.47  
    2.48 -            # Save the new version of the page.
    2.49 +            # Add categories if the parent page has any.
    2.50  
    2.51 -            new_page.saveText(page_body[start:end], 0)
    2.52 +            if new_page_categories != categories:
    2.53 +                new_page_body += getCategoryDeclaration(categories)
    2.54 +
    2.55 +            # Save the new page.
    2.56 +
    2.57 +            new_page.saveText(new_page_body, 0)
    2.58  
    2.59              # Retain the preceding region for the current page.
    2.60  
    2.61 @@ -176,7 +195,18 @@
    2.62          # Edit the current page.
    2.63  
    2.64          edited_page = PageEditor(request, page.page_name)
    2.65 -        edited_page.saveText("".join(retained_regions), 0)
    2.66 +        edited_page_body = "".join(retained_regions)
    2.67 +        edited_page_categories = getCategoryMembership(edited_page_body)
    2.68 +
    2.69 +        # Add categories if the parent page should have any, but these were
    2.70 +        # broken out.
    2.71 +
    2.72 +        if edited_page_categories != categories:
    2.73 +            edited_page_body += getCategoryDeclaration(categories)
    2.74 +
    2.75 +        # Save the current page.
    2.76 +
    2.77 +        edited_page.saveText(edited_page_body, 0)
    2.78  
    2.79          # NOTE: Perhaps show a message upon failure.
    2.80