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