1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinContentSupport library 4 5 @copyright: 2008, 2009, 2010, 2011 by Paul Boddie <paul@boddie.org.uk> 6 @copyright: 2000-2004 Juergen Hermann <jh@web.de>, 7 2005-2008 MoinMoin:ThomasWaldmann. 8 @license: GNU GPL (v2 or later), see COPYING.txt for details. 9 """ 10 11 from MoinMoin.wikiutil import escape 12 import re 13 14 __version__ = "0.1" 15 16 # Regular expressions. 17 # NOTE: These overlap with ImprovedMoinSearch and EventAggregator. 18 19 heading_regexp = re.compile(r"^(?P<level>=+)\s*(?P<heading>.*?)\s*(?P=level)$", re.UNICODE | re.MULTILINE) 20 category_membership_regexp = re.compile(ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$", re.MULTILINE | re.UNICODE) 21 22 def getHeadingDetails(body, min_level=None, max_level=None): 23 24 """ 25 Return heading details from the given 'body' for headings with the given 26 'min_level' to 'max_level' range. Specifying None or omitting 'max_level' or 27 'min_level' removes the appropriate constraint on the range. 28 29 A list of tuples containing the heading, the level, and the span (the start 30 offset and the end offset as a tuple) is returned. 31 """ 32 33 headings = [] 34 35 for match in heading_regexp.finditer(body): 36 level = len(match.group("level")) 37 38 if (min_level is None or min_level <= level) and \ 39 (max_level is None or level <= max_level): 40 41 headings.append((match.group("heading"), level, match.span())) 42 43 return headings 44 45 def getCategoryMembership(body): 46 47 "From the given 'body', return the categories the page belongs to." 48 49 match = category_membership_regexp.search(body) 50 if match: 51 return [x for x in match.groups() if x] 52 else: 53 return [] 54 55 def getCategoryDeclaration(categories): 56 57 "Return a category declaration string for the given 'categories'." 58 59 return "\n----\n%s\n" % " ".join(categories) 60 61 # Utility classes and associated functions. 62 # NOTE: These are a subset of EventAggregatorSupport. 63 64 class Form: 65 66 """ 67 A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x 68 environment. 69 """ 70 71 def __init__(self, form): 72 self.form = form 73 74 def get(self, name, default=None): 75 values = self.form.getlist(name) 76 if not values: 77 return default 78 else: 79 return values 80 81 def __getitem__(self, name): 82 return self.form.getlist(name) 83 84 class ActionSupport: 85 86 """ 87 Work around disruptive MoinMoin changes in 1.9, and also provide useful 88 convenience methods. 89 """ 90 91 def get_form(self): 92 return get_form(self.request) 93 94 def get_form(request): 95 96 "Work around disruptive MoinMoin changes in 1.9." 97 98 if hasattr(request, "values"): 99 return Form(request.values) 100 else: 101 return request.form 102 103 class send_headers: 104 105 """ 106 A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a 107 1.9.x environment. 108 """ 109 110 def __init__(self, request): 111 self.request = request 112 113 def __call__(self, headers): 114 for header in headers: 115 parts = header.split(":") 116 self.request.headers.add(parts[0], ":".join(parts[1:])) 117 118 def escattr(s): 119 return escape(s, 1) 120 121 # vim: tabstop=4 expandtab shiftwidth=4