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