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@6 | 20 | |
paul@6 | 21 | category_membership_str = ur"^\s*(?:(Category\S+)(?:\s+(Category\S+))*)\s*$" |
paul@6 | 22 | category_membership_regexp = re.compile(category_membership_str, re.MULTILINE | re.UNICODE) |
paul@6 | 23 | category_declarations_regexp = re.compile("^----$\s*" + category_membership_str, re.MULTILINE | re.UNICODE) |
paul@3 | 24 | |
paul@3 | 25 | def getHeadingDetails(body, min_level=None, max_level=None): |
paul@3 | 26 | |
paul@3 | 27 | """ |
paul@3 | 28 | Return heading details from the given 'body' for headings with the given |
paul@3 | 29 | 'min_level' to 'max_level' range. Specifying None or omitting 'max_level' or |
paul@3 | 30 | 'min_level' removes the appropriate constraint on the range. |
paul@3 | 31 | |
paul@3 | 32 | A list of tuples containing the heading, the level, and the span (the start |
paul@3 | 33 | offset and the end offset as a tuple) is returned. |
paul@3 | 34 | """ |
paul@3 | 35 | |
paul@3 | 36 | headings = [] |
paul@3 | 37 | |
paul@3 | 38 | for match in heading_regexp.finditer(body): |
paul@3 | 39 | level = len(match.group("level")) |
paul@3 | 40 | |
paul@3 | 41 | if (min_level is None or min_level <= level) and \ |
paul@3 | 42 | (max_level is None or level <= max_level): |
paul@3 | 43 | |
paul@3 | 44 | headings.append((match.group("heading"), level, match.span())) |
paul@3 | 45 | |
paul@3 | 46 | return headings |
paul@3 | 47 | |
paul@4 | 48 | def getCategoryMembership(body): |
paul@4 | 49 | |
paul@4 | 50 | "From the given 'body', return the categories the page belongs to." |
paul@4 | 51 | |
paul@4 | 52 | match = category_membership_regexp.search(body) |
paul@4 | 53 | if match: |
paul@4 | 54 | return [x for x in match.groups() if x] |
paul@4 | 55 | else: |
paul@4 | 56 | return [] |
paul@4 | 57 | |
paul@6 | 58 | def getCategoryDeclarations(body): |
paul@6 | 59 | |
paul@6 | 60 | """ |
paul@6 | 61 | From the given 'body', return the category declaration sections in the page |
paul@6 | 62 | in the form of a list of tuples, each containing a list of categories, the |
paul@6 | 63 | start of the declaration region and the end of the region. |
paul@6 | 64 | """ |
paul@6 | 65 | |
paul@6 | 66 | return [([x for x in match.groups() if x], match.span()) for match in category_declarations_regexp.finditer(body)] |
paul@6 | 67 | |
paul@6 | 68 | def makeCategoryDeclaration(categories): |
paul@4 | 69 | |
paul@4 | 70 | "Return a category declaration string for the given 'categories'." |
paul@4 | 71 | |
paul@4 | 72 | return "\n----\n%s\n" % " ".join(categories) |
paul@4 | 73 | |
paul@0 | 74 | # Utility classes and associated functions. |
paul@0 | 75 | # NOTE: These are a subset of EventAggregatorSupport. |
paul@0 | 76 | |
paul@0 | 77 | class Form: |
paul@0 | 78 | |
paul@0 | 79 | """ |
paul@0 | 80 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@0 | 81 | environment. |
paul@0 | 82 | """ |
paul@0 | 83 | |
paul@0 | 84 | def __init__(self, form): |
paul@0 | 85 | self.form = form |
paul@0 | 86 | |
paul@0 | 87 | def get(self, name, default=None): |
paul@0 | 88 | values = self.form.getlist(name) |
paul@0 | 89 | if not values: |
paul@0 | 90 | return default |
paul@0 | 91 | else: |
paul@0 | 92 | return values |
paul@0 | 93 | |
paul@0 | 94 | def __getitem__(self, name): |
paul@0 | 95 | return self.form.getlist(name) |
paul@0 | 96 | |
paul@0 | 97 | class ActionSupport: |
paul@0 | 98 | |
paul@0 | 99 | """ |
paul@0 | 100 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@0 | 101 | convenience methods. |
paul@0 | 102 | """ |
paul@0 | 103 | |
paul@0 | 104 | def get_form(self): |
paul@0 | 105 | return get_form(self.request) |
paul@0 | 106 | |
paul@0 | 107 | def get_form(request): |
paul@0 | 108 | |
paul@0 | 109 | "Work around disruptive MoinMoin changes in 1.9." |
paul@0 | 110 | |
paul@0 | 111 | if hasattr(request, "values"): |
paul@0 | 112 | return Form(request.values) |
paul@0 | 113 | else: |
paul@0 | 114 | return request.form |
paul@0 | 115 | |
paul@0 | 116 | class send_headers: |
paul@0 | 117 | |
paul@0 | 118 | """ |
paul@0 | 119 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@0 | 120 | 1.9.x environment. |
paul@0 | 121 | """ |
paul@0 | 122 | |
paul@0 | 123 | def __init__(self, request): |
paul@0 | 124 | self.request = request |
paul@0 | 125 | |
paul@0 | 126 | def __call__(self, headers): |
paul@0 | 127 | for header in headers: |
paul@0 | 128 | parts = header.split(":") |
paul@0 | 129 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@0 | 130 | |
paul@3 | 131 | def escattr(s): |
paul@3 | 132 | return escape(s, 1) |
paul@3 | 133 | |
paul@0 | 134 | # vim: tabstop=4 expandtab shiftwidth=4 |