1.1 --- a/EventAggregatorSupport.py Sat Jun 06 02:17:51 2009 +0200
1.2 +++ b/EventAggregatorSupport.py Sat Jun 06 23:37:14 2009 +0200
1.3 @@ -27,7 +27,14 @@
1.4 # Regular expressions where MoinMoin does not provide the required support.
1.5
1.6 category_regexp = None
1.7 -definition_list_regexp = re.compile(ur'^\s+(?P<term>.*?)::\s(?P<desc>.*?)$', re.UNICODE | re.MULTILINE)
1.8 +
1.9 +# Page parsing.
1.10 +
1.11 +definition_list_regexp = re.compile(ur'(?P<wholeterm>^(?P<optcomment>#*)\s+(?P<term>.*?)::\s)(?P<desc>.*?)$', re.UNICODE | re.MULTILINE)
1.12 +category_membership_regexp = re.compile(ur"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE)
1.13 +
1.14 +# Value parsing.
1.15 +
1.16 date_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})', re.UNICODE)
1.17 month_regexp = re.compile(ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})', re.UNICODE)
1.18 verbatim_regexp = re.compile(ur'(?:'
1.19 @@ -144,6 +151,12 @@
1.20
1.21 return "".join([s for s in verbatim_regexp.split(text) if s is not None])
1.22
1.23 +def getEncodedWikiText(text):
1.24 +
1.25 + "Encode the given 'text' in a verbatim representation."
1.26 +
1.27 + return "<<Verbatim(%s)>>" % text
1.28 +
1.29 def getFormat(page):
1.30
1.31 "Get the format used on 'page'."
1.32 @@ -162,6 +175,11 @@
1.33 if getFormat(page) == "wiki":
1.34 for match in definition_list_regexp.finditer(page.get_raw_body()):
1.35
1.36 + # Skip commented-out items.
1.37 +
1.38 + if match.group("optcomment"):
1.39 + continue
1.40 +
1.41 # Permit case-insensitive list terms.
1.42
1.43 term = match.group("term").lower()
1.44 @@ -189,6 +207,71 @@
1.45
1.46 return event_details
1.47
1.48 +def setEventDetails(body, event_details):
1.49 +
1.50 + """
1.51 + Set the event details in the given page 'body' using the 'event_details'
1.52 + dictionary, returning the new body text.
1.53 + """
1.54 +
1.55 + new_body_parts = []
1.56 + end_of_last_match = 0
1.57 +
1.58 + for match in definition_list_regexp.finditer(body):
1.59 +
1.60 + # Add preceding text to the new body.
1.61 +
1.62 + new_body_parts.append(body[end_of_last_match:match.start()])
1.63 + end_of_last_match = match.end()
1.64 +
1.65 + # Get the matching regions, adding the term to the new body.
1.66 +
1.67 + new_body_parts.append(match.group("wholeterm"))
1.68 +
1.69 + # Permit case-insensitive list terms.
1.70 +
1.71 + term = match.group("term").lower()
1.72 + desc = match.group("desc")
1.73 +
1.74 + # Special value type handling.
1.75 +
1.76 + if event_details.has_key(term):
1.77 +
1.78 + # Dates.
1.79 +
1.80 + if term in ("start", "end"):
1.81 + desc = desc.replace("YYYY-MM-DD", event_details[term])
1.82 +
1.83 + # Lists (whose elements may be quoted).
1.84 +
1.85 + elif term in ("topics", "categories"):
1.86 + desc = ", ".join(getEncodedWikiText(event_details[term]))
1.87 +
1.88 + # Labels which may well be quoted.
1.89 +
1.90 + elif term in ("title", "summary", "description"):
1.91 + desc = getEncodedWikiText(event_details[term])
1.92 +
1.93 + new_body_parts.append(desc)
1.94 +
1.95 + else:
1.96 + new_body_parts.append(body[end_of_last_match:])
1.97 +
1.98 + return "".join(new_body_parts)
1.99 +
1.100 +def setCategoryMembership(body, category_names):
1.101 +
1.102 + """
1.103 + Set the category membership in the given page 'body' using the specified
1.104 + 'category_names' and returning the new body text.
1.105 + """
1.106 +
1.107 + match = category_membership_regexp.search(body)
1.108 + if match:
1.109 + return "".join([body[:match.start()], " ".join(category_names), body[match.end():]])
1.110 + else:
1.111 + return body
1.112 +
1.113 def getEventSummary(event_page, event_details):
1.114
1.115 """