paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinSupport library (derived from EventAggregatorSupport) |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012 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@0 | 11 | from DateSupport import * |
paul@1 | 12 | from MoinMoin.Page import Page |
paul@25 | 13 | from MoinMoin import config, search, wikiutil |
paul@10 | 14 | from StringIO import StringIO |
paul@10 | 15 | from shlex import shlex |
paul@0 | 16 | import re |
paul@0 | 17 | import time |
paul@0 | 18 | |
paul@35 | 19 | # Moin 1.9 request parameters. |
paul@35 | 20 | |
paul@35 | 21 | try: |
paul@35 | 22 | from MoinMoin.support.werkzeug.datastructures import MultiDict |
paul@35 | 23 | except ImportError: |
paul@35 | 24 | pass |
paul@35 | 25 | |
paul@10 | 26 | __version__ = "0.2" |
paul@0 | 27 | |
paul@0 | 28 | # Content type parsing. |
paul@0 | 29 | |
paul@0 | 30 | encoding_regexp_str = ur'(?P<content_type>[^\s;]*)(?:;\s*charset=(?P<encoding>[-A-Za-z0-9]+))?' |
paul@0 | 31 | encoding_regexp = re.compile(encoding_regexp_str) |
paul@0 | 32 | |
paul@2 | 33 | # Accept header parsing. |
paul@2 | 34 | |
paul@2 | 35 | accept_regexp_str = ur';\s*q=' |
paul@2 | 36 | accept_regexp = re.compile(accept_regexp_str) |
paul@2 | 37 | |
paul@25 | 38 | # Extraction of shared fragments. |
paul@25 | 39 | |
paul@25 | 40 | marker_regexp_str = r"([{]{3,}|[}]{3,})" |
paul@25 | 41 | marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... |
paul@25 | 42 | |
paul@29 | 43 | # Extraction of headings. |
paul@29 | 44 | |
paul@29 | 45 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@29 | 46 | |
paul@25 | 47 | # Category extraction from pages. |
paul@25 | 48 | |
paul@25 | 49 | category_regexp = None |
paul@25 | 50 | |
paul@25 | 51 | # Simple content parsing. |
paul@25 | 52 | |
paul@25 | 53 | verbatim_regexp = re.compile(ur'(?:' |
paul@25 | 54 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@25 | 55 | ur'|' |
paul@25 | 56 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@25 | 57 | ur'|' |
paul@25 | 58 | ur'!(?P<verbatim3>.*?)(\s|$)?' |
paul@25 | 59 | ur'|' |
paul@25 | 60 | ur'`(?P<monospace>.*?)`' |
paul@25 | 61 | ur'|' |
paul@25 | 62 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@25 | 63 | ur')', re.UNICODE) |
paul@25 | 64 | |
paul@25 | 65 | # Category discovery. |
paul@0 | 66 | |
paul@25 | 67 | def getCategoryPattern(request): |
paul@25 | 68 | global category_regexp |
paul@25 | 69 | |
paul@25 | 70 | try: |
paul@25 | 71 | return request.cfg.cache.page_category_regexact |
paul@25 | 72 | except AttributeError: |
paul@25 | 73 | |
paul@25 | 74 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@25 | 75 | |
paul@25 | 76 | if category_regexp is None: |
paul@25 | 77 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@25 | 78 | return category_regexp |
paul@25 | 79 | |
paul@25 | 80 | def getCategories(request): |
paul@25 | 81 | |
paul@25 | 82 | """ |
paul@25 | 83 | From the AdvancedSearch macro, return a list of category page names using |
paul@25 | 84 | the given 'request'. |
paul@25 | 85 | """ |
paul@25 | 86 | |
paul@25 | 87 | # This will return all pages with "Category" in the title. |
paul@25 | 88 | |
paul@25 | 89 | cat_filter = getCategoryPattern(request).search |
paul@25 | 90 | return request.rootpage.getPageList(filter=cat_filter) |
paul@25 | 91 | |
paul@25 | 92 | def getCategoryMapping(category_pagenames, request): |
paul@2 | 93 | |
paul@2 | 94 | """ |
paul@25 | 95 | For the given 'category_pagenames' return a list of tuples of the form |
paul@25 | 96 | (category name, category page name) using the given 'request'. |
paul@25 | 97 | """ |
paul@25 | 98 | |
paul@25 | 99 | cat_pattern = getCategoryPattern(request) |
paul@25 | 100 | mapping = [] |
paul@25 | 101 | for pagename in category_pagenames: |
paul@25 | 102 | name = cat_pattern.match(pagename).group("key") |
paul@25 | 103 | if name != "Category": |
paul@25 | 104 | mapping.append((name, pagename)) |
paul@25 | 105 | mapping.sort() |
paul@25 | 106 | return mapping |
paul@25 | 107 | |
paul@25 | 108 | def getCategoryPages(pagename, request): |
paul@25 | 109 | |
paul@25 | 110 | """ |
paul@25 | 111 | Return the pages associated with the given category 'pagename' using the |
paul@25 | 112 | 'request'. |
paul@25 | 113 | """ |
paul@25 | 114 | |
paul@25 | 115 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@25 | 116 | results = search.searchPages(request, query, "page_name") |
paul@27 | 117 | return filterCategoryPages(results, request) |
paul@27 | 118 | |
paul@27 | 119 | def filterCategoryPages(results, request): |
paul@27 | 120 | |
paul@27 | 121 | "Filter category pages from the given 'results' using the 'request'." |
paul@25 | 122 | |
paul@25 | 123 | cat_pattern = getCategoryPattern(request) |
paul@25 | 124 | pages = [] |
paul@25 | 125 | for page in results.hits: |
paul@25 | 126 | if not cat_pattern.match(page.page_name): |
paul@25 | 127 | pages.append(page) |
paul@25 | 128 | return pages |
paul@25 | 129 | |
paul@25 | 130 | def getAllCategoryPages(category_names, request): |
paul@25 | 131 | |
paul@25 | 132 | """ |
paul@25 | 133 | Return all pages belonging to the categories having the given |
paul@25 | 134 | 'category_names', using the given 'request'. |
paul@25 | 135 | """ |
paul@25 | 136 | |
paul@25 | 137 | pages = [] |
paul@25 | 138 | pagenames = set() |
paul@25 | 139 | |
paul@25 | 140 | for category_name in category_names: |
paul@25 | 141 | |
paul@25 | 142 | # Get the pages and page names in the category. |
paul@25 | 143 | |
paul@25 | 144 | pages_in_category = getCategoryPages(category_name, request) |
paul@25 | 145 | |
paul@25 | 146 | # Visit each page in the category. |
paul@25 | 147 | |
paul@25 | 148 | for page_in_category in pages_in_category: |
paul@25 | 149 | pagename = page_in_category.page_name |
paul@25 | 150 | |
paul@25 | 151 | # Only process each page once. |
paul@25 | 152 | |
paul@25 | 153 | if pagename in pagenames: |
paul@25 | 154 | continue |
paul@25 | 155 | else: |
paul@25 | 156 | pagenames.add(pagename) |
paul@25 | 157 | |
paul@25 | 158 | pages.append(page_in_category) |
paul@25 | 159 | |
paul@25 | 160 | return pages |
paul@25 | 161 | |
paul@28 | 162 | def getPagesForSearch(search_pattern, request): |
paul@27 | 163 | |
paul@27 | 164 | """ |
paul@27 | 165 | Return result pages for a search employing the given 'search_pattern' and |
paul@28 | 166 | using the given 'request'. |
paul@27 | 167 | """ |
paul@27 | 168 | |
paul@27 | 169 | query = search.QueryParser().parse_query(search_pattern) |
paul@27 | 170 | results = search.searchPages(request, query, "page_name") |
paul@27 | 171 | return filterCategoryPages(results, request) |
paul@27 | 172 | |
paul@25 | 173 | # WikiDict functions. |
paul@25 | 174 | |
paul@25 | 175 | def getWikiDict(pagename, request): |
paul@25 | 176 | |
paul@25 | 177 | """ |
paul@25 | 178 | Return the WikiDict provided by the given 'pagename' using the given |
paul@25 | 179 | 'request'. |
paul@2 | 180 | """ |
paul@2 | 181 | |
paul@25 | 182 | if pagename and Page(request, pagename).exists() and request.user.may.read(pagename): |
paul@25 | 183 | if hasattr(request.dicts, "dict"): |
paul@25 | 184 | return request.dicts.dict(pagename) |
paul@25 | 185 | else: |
paul@25 | 186 | return request.dicts[pagename] |
paul@0 | 187 | else: |
paul@25 | 188 | return None |
paul@25 | 189 | |
paul@25 | 190 | # Searching-related functions. |
paul@25 | 191 | |
paul@25 | 192 | def getPagesFromResults(result_pages, request): |
paul@25 | 193 | |
paul@25 | 194 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@25 | 195 | |
paul@25 | 196 | return [Page(request, page.page_name) for page in result_pages] |
paul@25 | 197 | |
paul@25 | 198 | # Region/section parsing. |
paul@25 | 199 | |
paul@25 | 200 | def getRegions(s, include_non_regions=False): |
paul@25 | 201 | |
paul@25 | 202 | """ |
paul@25 | 203 | Parse the string 's', returning a list of explicitly declared regions. |
paul@25 | 204 | |
paul@25 | 205 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 206 | included for text between explicitly declared regions. |
paul@25 | 207 | """ |
paul@25 | 208 | |
paul@25 | 209 | regions = [] |
paul@25 | 210 | marker = None |
paul@25 | 211 | is_block = True |
paul@25 | 212 | |
paul@25 | 213 | # Start a region for exposed text, if appropriate. |
paul@25 | 214 | |
paul@25 | 215 | if include_non_regions: |
paul@25 | 216 | regions.append("") |
paul@25 | 217 | |
paul@25 | 218 | for match_text in marker_regexp.split(s): |
paul@25 | 219 | |
paul@25 | 220 | # Capture section text. |
paul@25 | 221 | |
paul@25 | 222 | if is_block: |
paul@25 | 223 | if marker or include_non_regions: |
paul@25 | 224 | regions[-1] += match_text |
paul@25 | 225 | |
paul@25 | 226 | # Handle section markers. |
paul@25 | 227 | |
paul@31 | 228 | else: |
paul@25 | 229 | |
paul@25 | 230 | # Close any open sections, returning to exposed text regions. |
paul@25 | 231 | |
paul@25 | 232 | if marker: |
paul@31 | 233 | |
paul@31 | 234 | # Add any marker to the current region, regardless of whether it |
paul@31 | 235 | # successfully closes a section. |
paul@31 | 236 | |
paul@31 | 237 | regions[-1] += match_text |
paul@31 | 238 | |
paul@25 | 239 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@25 | 240 | marker = None |
paul@25 | 241 | |
paul@25 | 242 | # Start a region for exposed text, if appropriate. |
paul@25 | 243 | |
paul@25 | 244 | if include_non_regions: |
paul@25 | 245 | regions.append("") |
paul@25 | 246 | |
paul@31 | 247 | # Without a current marker, start a new section. |
paul@25 | 248 | |
paul@31 | 249 | else: |
paul@25 | 250 | marker = match_text |
paul@25 | 251 | regions.append("") |
paul@25 | 252 | |
paul@31 | 253 | # Add the marker to the new region. |
paul@25 | 254 | |
paul@31 | 255 | regions[-1] += match_text |
paul@0 | 256 | |
paul@25 | 257 | # The match text alternates between text between markers and the markers |
paul@25 | 258 | # themselves. |
paul@25 | 259 | |
paul@25 | 260 | is_block = not is_block |
paul@25 | 261 | |
paul@25 | 262 | return regions |
paul@25 | 263 | |
paul@25 | 264 | def getFragmentsFromRegions(regions): |
paul@25 | 265 | |
paul@25 | 266 | """ |
paul@25 | 267 | Return fragments from the given 'regions', each having the form |
paul@32 | 268 | (format, attributes, body text). |
paul@25 | 269 | """ |
paul@25 | 270 | |
paul@25 | 271 | fragments = [] |
paul@25 | 272 | |
paul@25 | 273 | for region in regions: |
paul@32 | 274 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@32 | 275 | fragments.append((format, attributes, body)) |
paul@25 | 276 | |
paul@32 | 277 | return fragments |
paul@25 | 278 | |
paul@32 | 279 | def getFragmentFromRegion(region): |
paul@25 | 280 | |
paul@32 | 281 | """ |
paul@32 | 282 | Return a fragment for the given 'region' having the form (format, |
paul@32 | 283 | attributes, body text, header, close), where the 'header' is the original |
paul@32 | 284 | declaration of the 'region' or None if no explicit region is defined, and |
paul@32 | 285 | 'close' is the closing marker of the 'region' or None if no explicit region |
paul@32 | 286 | is defined. |
paul@32 | 287 | """ |
paul@25 | 288 | |
paul@32 | 289 | if region.startswith("{{{"): |
paul@25 | 290 | |
paul@32 | 291 | body = region.lstrip("{") |
paul@32 | 292 | level = len(region) - len(body) |
paul@32 | 293 | body = body.rstrip("}").lstrip() |
paul@32 | 294 | |
paul@32 | 295 | # Remove any prelude and process metadata. |
paul@32 | 296 | |
paul@32 | 297 | if body.startswith("#!"): |
paul@25 | 298 | |
paul@32 | 299 | try: |
paul@32 | 300 | declaration, body = body.split("\n", 1) |
paul@32 | 301 | except ValueError: |
paul@32 | 302 | declaration = body |
paul@32 | 303 | body = "" |
paul@25 | 304 | |
paul@32 | 305 | arguments = declaration[2:] |
paul@32 | 306 | |
paul@32 | 307 | # Get any parser/format declaration. |
paul@25 | 308 | |
paul@32 | 309 | if arguments and not arguments[0].isspace(): |
paul@32 | 310 | details = arguments.split(None, 1) |
paul@32 | 311 | if len(details) == 2: |
paul@32 | 312 | format, arguments = details |
paul@32 | 313 | else: |
paul@32 | 314 | format = details[0] |
paul@32 | 315 | arguments = "" |
paul@32 | 316 | else: |
paul@32 | 317 | format = None |
paul@25 | 318 | |
paul@32 | 319 | # Get the attributes/arguments for the region. |
paul@32 | 320 | |
paul@32 | 321 | attributes = parseAttributes(arguments, False) |
paul@25 | 322 | |
paul@32 | 323 | # Add an entry for the format in the attribute dictionary. |
paul@25 | 324 | |
paul@32 | 325 | if format and not attributes.has_key(format): |
paul@32 | 326 | attributes[format] = True |
paul@32 | 327 | |
paul@32 | 328 | return format, attributes, body, level * "{" + declaration + "\n", level * "}" |
paul@25 | 329 | |
paul@25 | 330 | else: |
paul@32 | 331 | return None, {}, body, level * "{" + "\n", level * "}" |
paul@25 | 332 | |
paul@32 | 333 | else: |
paul@32 | 334 | return None, {}, region, None, None |
paul@25 | 335 | |
paul@25 | 336 | def getFragments(s, include_non_regions=False): |
paul@25 | 337 | |
paul@25 | 338 | """ |
paul@25 | 339 | Return fragments for the given string 's', each having the form |
paul@25 | 340 | (format, arguments, body text). |
paul@25 | 341 | |
paul@25 | 342 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 343 | included for text between explicitly declared regions. |
paul@25 | 344 | """ |
paul@25 | 345 | |
paul@25 | 346 | return getFragmentsFromRegions(getRegions(s, include_non_regions)) |
paul@25 | 347 | |
paul@29 | 348 | # Heading extraction. |
paul@29 | 349 | |
paul@29 | 350 | def getHeadings(s): |
paul@29 | 351 | |
paul@29 | 352 | """ |
paul@29 | 353 | Return tuples of the form (level, title, span) for headings found within the |
paul@29 | 354 | given string 's'. The span is itself a (start, end) tuple indicating the |
paul@29 | 355 | matching region of 's' for a heading declaration. |
paul@29 | 356 | """ |
paul@29 | 357 | |
paul@29 | 358 | headings = [] |
paul@29 | 359 | |
paul@29 | 360 | for match in heading_regexp.finditer(s): |
paul@29 | 361 | headings.append( |
paul@29 | 362 | (len(match.group("level")), match.group("heading"), match.span()) |
paul@29 | 363 | ) |
paul@29 | 364 | |
paul@29 | 365 | return headings |
paul@29 | 366 | |
paul@25 | 367 | # Region/section attribute parsing. |
paul@0 | 368 | |
paul@10 | 369 | def parseAttributes(s, escape=True): |
paul@10 | 370 | |
paul@10 | 371 | """ |
paul@10 | 372 | Parse the section attributes string 's', returning a mapping of names to |
paul@10 | 373 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@10 | 374 | for use with the formatter API. If 'escape' is set to a false value, the |
paul@10 | 375 | attributes will have any quoting removed. |
paul@10 | 376 | """ |
paul@10 | 377 | |
paul@10 | 378 | attrs = {} |
paul@10 | 379 | f = StringIO(s) |
paul@10 | 380 | name = None |
paul@10 | 381 | need_value = False |
paul@34 | 382 | lex = shlex(f) |
paul@34 | 383 | lex.wordchars += "-" |
paul@10 | 384 | |
paul@34 | 385 | for token in lex: |
paul@10 | 386 | |
paul@10 | 387 | # Capture the name if needed. |
paul@10 | 388 | |
paul@10 | 389 | if name is None: |
paul@10 | 390 | name = escape and wikiutil.escape(token) or strip_token(token) |
paul@10 | 391 | |
paul@10 | 392 | # Detect either an equals sign or another name. |
paul@10 | 393 | |
paul@10 | 394 | elif not need_value: |
paul@10 | 395 | if token == "=": |
paul@10 | 396 | need_value = True |
paul@10 | 397 | else: |
paul@10 | 398 | attrs[name.lower()] = escape and "true" or True |
paul@10 | 399 | name = wikiutil.escape(token) |
paul@10 | 400 | |
paul@10 | 401 | # Otherwise, capture a value. |
paul@10 | 402 | |
paul@10 | 403 | else: |
paul@10 | 404 | # Quoting of attributes done similarly to wikiutil.parseAttributes. |
paul@10 | 405 | |
paul@10 | 406 | if token: |
paul@10 | 407 | if escape: |
paul@10 | 408 | if token[0] in ("'", '"'): |
paul@10 | 409 | token = wikiutil.escape(token) |
paul@10 | 410 | else: |
paul@10 | 411 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@10 | 412 | else: |
paul@10 | 413 | token = strip_token(token) |
paul@10 | 414 | |
paul@10 | 415 | attrs[name.lower()] = token |
paul@10 | 416 | name = None |
paul@10 | 417 | need_value = False |
paul@10 | 418 | |
paul@13 | 419 | # Handle any name-only attributes at the end of the collection. |
paul@13 | 420 | |
paul@13 | 421 | if name and not need_value: |
paul@13 | 422 | attrs[name.lower()] = escape and "true" or True |
paul@13 | 423 | |
paul@10 | 424 | return attrs |
paul@10 | 425 | |
paul@10 | 426 | def strip_token(token): |
paul@10 | 427 | |
paul@10 | 428 | "Return the given 'token' stripped of quoting." |
paul@10 | 429 | |
paul@10 | 430 | if token[0] in ("'", '"') and token[-1] == token[0]: |
paul@10 | 431 | return token[1:-1] |
paul@10 | 432 | else: |
paul@10 | 433 | return token |
paul@10 | 434 | |
paul@25 | 435 | # Request-related classes and associated functions. |
paul@0 | 436 | |
paul@0 | 437 | class Form: |
paul@0 | 438 | |
paul@0 | 439 | """ |
paul@0 | 440 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@0 | 441 | environment. |
paul@0 | 442 | """ |
paul@0 | 443 | |
paul@35 | 444 | def __init__(self, request): |
paul@35 | 445 | self.request = request |
paul@35 | 446 | self.form = request.values |
paul@0 | 447 | |
paul@0 | 448 | def has_key(self, name): |
paul@0 | 449 | return not not self.form.getlist(name) |
paul@0 | 450 | |
paul@0 | 451 | def get(self, name, default=None): |
paul@0 | 452 | values = self.form.getlist(name) |
paul@0 | 453 | if not values: |
paul@0 | 454 | return default |
paul@0 | 455 | else: |
paul@0 | 456 | return values |
paul@0 | 457 | |
paul@0 | 458 | def __getitem__(self, name): |
paul@0 | 459 | return self.form.getlist(name) |
paul@0 | 460 | |
paul@35 | 461 | def __setitem__(self, name, value): |
paul@35 | 462 | try: |
paul@35 | 463 | self.form.setlist(name, value) |
paul@35 | 464 | except TypeError: |
paul@35 | 465 | self._write_enable() |
paul@35 | 466 | self.form.setlist(name, value) |
paul@35 | 467 | |
paul@33 | 468 | def __delitem__(self, name): |
paul@35 | 469 | try: |
paul@35 | 470 | del self.form[name] |
paul@35 | 471 | except TypeError: |
paul@35 | 472 | self._write_enable() |
paul@35 | 473 | del self.form[name] |
paul@35 | 474 | |
paul@35 | 475 | def _write_enable(self): |
paul@35 | 476 | self.form = self.request.values = MultiDict(self.form) |
paul@33 | 477 | |
paul@33 | 478 | def keys(self): |
paul@33 | 479 | return self.form.keys() |
paul@33 | 480 | |
paul@32 | 481 | def items(self): |
paul@35 | 482 | return self.form.lists() |
paul@32 | 483 | |
paul@0 | 484 | class ActionSupport: |
paul@0 | 485 | |
paul@0 | 486 | """ |
paul@0 | 487 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@0 | 488 | convenience methods. |
paul@0 | 489 | """ |
paul@0 | 490 | |
paul@0 | 491 | def get_form(self): |
paul@0 | 492 | return get_form(self.request) |
paul@0 | 493 | |
paul@0 | 494 | def _get_selected(self, value, input_value): |
paul@0 | 495 | |
paul@0 | 496 | """ |
paul@0 | 497 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 498 | otherwise) if 'value' matches 'input_value'. |
paul@0 | 499 | """ |
paul@0 | 500 | |
paul@0 | 501 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@0 | 502 | |
paul@0 | 503 | def _get_selected_for_list(self, value, input_values): |
paul@0 | 504 | |
paul@0 | 505 | """ |
paul@0 | 506 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 507 | otherwise) if 'value' matches one of the 'input_values'. |
paul@0 | 508 | """ |
paul@0 | 509 | |
paul@0 | 510 | return value in input_values and 'selected="selected"' or '' |
paul@0 | 511 | |
paul@26 | 512 | def get_option_list(self, value, values): |
paul@26 | 513 | |
paul@26 | 514 | """ |
paul@26 | 515 | Return a list of HTML element definitions for options describing the |
paul@26 | 516 | given 'values', selecting the option with the specified 'value' if |
paul@26 | 517 | present. |
paul@26 | 518 | """ |
paul@26 | 519 | |
paul@26 | 520 | options = [] |
paul@26 | 521 | for available_value in values: |
paul@26 | 522 | selected = self._get_selected(available_value, value) |
paul@26 | 523 | options.append('<option value="%s" %s>%s</option>' % ( |
paul@26 | 524 | escattr(available_value), selected, wikiutil.escape(available_value))) |
paul@26 | 525 | return options |
paul@26 | 526 | |
paul@0 | 527 | def _get_input(self, form, name, default=None): |
paul@0 | 528 | |
paul@0 | 529 | """ |
paul@0 | 530 | Return the input from 'form' having the given 'name', returning either |
paul@0 | 531 | the input converted to an integer or the given 'default' (optional, None |
paul@0 | 532 | if not specified). |
paul@0 | 533 | """ |
paul@0 | 534 | |
paul@0 | 535 | value = form.get(name, [None])[0] |
paul@0 | 536 | if not value: # true if 0 obtained |
paul@0 | 537 | return default |
paul@0 | 538 | else: |
paul@0 | 539 | return int(value) |
paul@0 | 540 | |
paul@0 | 541 | def get_form(request): |
paul@0 | 542 | |
paul@0 | 543 | "Work around disruptive MoinMoin changes in 1.9." |
paul@0 | 544 | |
paul@0 | 545 | if hasattr(request, "values"): |
paul@35 | 546 | return Form(request) |
paul@0 | 547 | else: |
paul@0 | 548 | return request.form |
paul@0 | 549 | |
paul@0 | 550 | class send_headers_cls: |
paul@0 | 551 | |
paul@0 | 552 | """ |
paul@0 | 553 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@0 | 554 | 1.9.x environment. |
paul@0 | 555 | """ |
paul@0 | 556 | |
paul@0 | 557 | def __init__(self, request): |
paul@0 | 558 | self.request = request |
paul@0 | 559 | |
paul@0 | 560 | def __call__(self, headers): |
paul@0 | 561 | for header in headers: |
paul@0 | 562 | parts = header.split(":") |
paul@0 | 563 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@0 | 564 | |
paul@0 | 565 | def get_send_headers(request): |
paul@0 | 566 | |
paul@0 | 567 | "Return a function that can send response headers." |
paul@0 | 568 | |
paul@0 | 569 | if hasattr(request, "http_headers"): |
paul@0 | 570 | return request.http_headers |
paul@0 | 571 | elif hasattr(request, "emit_http_headers"): |
paul@0 | 572 | return request.emit_http_headers |
paul@0 | 573 | else: |
paul@0 | 574 | return send_headers_cls(request) |
paul@0 | 575 | |
paul@0 | 576 | def escattr(s): |
paul@0 | 577 | return wikiutil.escape(s, 1) |
paul@0 | 578 | |
paul@0 | 579 | def getPathInfo(request): |
paul@0 | 580 | if hasattr(request, "getPathinfo"): |
paul@0 | 581 | return request.getPathinfo() |
paul@0 | 582 | else: |
paul@0 | 583 | return request.path |
paul@0 | 584 | |
paul@14 | 585 | def getHeader(request, header_name, prefix=None): |
paul@17 | 586 | |
paul@17 | 587 | """ |
paul@17 | 588 | Using the 'request', return the value of the header with the given |
paul@17 | 589 | 'header_name', using the optional 'prefix' to obtain protocol-specific |
paul@17 | 590 | headers if necessary. |
paul@17 | 591 | |
paul@17 | 592 | If no value is found for the given 'header_name', None is returned. |
paul@17 | 593 | """ |
paul@17 | 594 | |
paul@14 | 595 | if hasattr(request, "getHeader"): |
paul@14 | 596 | return request.getHeader(header_name) |
paul@14 | 597 | elif hasattr(request, "headers"): |
paul@17 | 598 | return request.headers.get(header_name) |
paul@14 | 599 | else: |
paul@17 | 600 | return request.env.get((prefix and prefix + "_" or "") + header_name.upper()) |
paul@14 | 601 | |
paul@23 | 602 | def writeHeaders(request, mimetype, metadata, status=None): |
paul@23 | 603 | |
paul@23 | 604 | """ |
paul@23 | 605 | Using the 'request', write resource headers using the given 'mimetype', |
paul@23 | 606 | based on the given 'metadata'. If the optional 'status' is specified, set |
paul@23 | 607 | the status header to the given value. |
paul@23 | 608 | """ |
paul@23 | 609 | |
paul@23 | 610 | send_headers = get_send_headers(request) |
paul@23 | 611 | |
paul@23 | 612 | # Define headers. |
paul@23 | 613 | |
paul@23 | 614 | headers = ["Content-Type: %s; charset=%s" % (mimetype, config.charset)] |
paul@23 | 615 | |
paul@23 | 616 | # Define the last modified time. |
paul@23 | 617 | # NOTE: Consider using request.httpDate. |
paul@23 | 618 | |
paul@23 | 619 | latest_timestamp = metadata.get("last-modified") |
paul@23 | 620 | if latest_timestamp: |
paul@23 | 621 | headers.append("Last-Modified: %s" % latest_timestamp.as_HTTP_datetime_string()) |
paul@23 | 622 | |
paul@23 | 623 | if status: |
paul@23 | 624 | headers.append("Status: %s" % status) |
paul@23 | 625 | |
paul@23 | 626 | send_headers(headers) |
paul@23 | 627 | |
paul@2 | 628 | # Content/media type and preferences support. |
paul@2 | 629 | |
paul@2 | 630 | class MediaRange: |
paul@2 | 631 | |
paul@2 | 632 | "A content/media type value which supports whole categories of data." |
paul@2 | 633 | |
paul@2 | 634 | def __init__(self, media_range, accept_parameters=None): |
paul@2 | 635 | self.media_range = media_range |
paul@2 | 636 | self.accept_parameters = accept_parameters or {} |
paul@2 | 637 | |
paul@2 | 638 | parts = media_range.split(";") |
paul@2 | 639 | self.media_type = parts[0] |
paul@2 | 640 | self.parameters = getMappingFromParameterStrings(parts[1:]) |
paul@2 | 641 | |
paul@2 | 642 | # The media type is divided into category and subcategory. |
paul@2 | 643 | |
paul@2 | 644 | parts = self.media_type.split("/") |
paul@2 | 645 | self.category = parts[0] |
paul@2 | 646 | self.subcategory = "/".join(parts[1:]) |
paul@2 | 647 | |
paul@2 | 648 | def get_parts(self): |
paul@3 | 649 | |
paul@3 | 650 | "Return the category, subcategory parts." |
paul@3 | 651 | |
paul@2 | 652 | return self.category, self.subcategory |
paul@2 | 653 | |
paul@2 | 654 | def get_specificity(self): |
paul@3 | 655 | |
paul@3 | 656 | """ |
paul@3 | 657 | Return the specificity of the media type in terms of the scope of the |
paul@3 | 658 | category and subcategory, and also in terms of any qualifying |
paul@3 | 659 | parameters. |
paul@3 | 660 | """ |
paul@3 | 661 | |
paul@2 | 662 | if "*" in self.get_parts(): |
paul@2 | 663 | return -list(self.get_parts()).count("*") |
paul@2 | 664 | else: |
paul@2 | 665 | return len(self.parameters) |
paul@2 | 666 | |
paul@2 | 667 | def permits(self, other): |
paul@3 | 668 | |
paul@3 | 669 | """ |
paul@3 | 670 | Return whether this media type permits the use of the 'other' media type |
paul@3 | 671 | if suggested as suitable content. |
paul@3 | 672 | """ |
paul@3 | 673 | |
paul@2 | 674 | if not isinstance(other, MediaRange): |
paul@2 | 675 | other = MediaRange(other) |
paul@2 | 676 | |
paul@2 | 677 | category = categoryPermits(self.category, other.category) |
paul@2 | 678 | subcategory = categoryPermits(self.subcategory, other.subcategory) |
paul@2 | 679 | |
paul@2 | 680 | if category and subcategory: |
paul@2 | 681 | if "*" not in (category, subcategory): |
paul@2 | 682 | return not self.parameters or self.parameters == other.parameters |
paul@2 | 683 | else: |
paul@2 | 684 | return True |
paul@2 | 685 | else: |
paul@2 | 686 | return False |
paul@2 | 687 | |
paul@2 | 688 | def __eq__(self, other): |
paul@3 | 689 | |
paul@3 | 690 | """ |
paul@3 | 691 | Return whether this media type is effectively the same as the 'other' |
paul@3 | 692 | media type. |
paul@3 | 693 | """ |
paul@3 | 694 | |
paul@2 | 695 | if not isinstance(other, MediaRange): |
paul@2 | 696 | other = MediaRange(other) |
paul@2 | 697 | |
paul@2 | 698 | category = categoryMatches(self.category, other.category) |
paul@2 | 699 | subcategory = categoryMatches(self.subcategory, other.subcategory) |
paul@2 | 700 | |
paul@2 | 701 | if category and subcategory: |
paul@2 | 702 | if "*" not in (category, subcategory): |
paul@2 | 703 | return self.parameters == other.parameters or \ |
paul@2 | 704 | not self.parameters or not other.parameters |
paul@2 | 705 | else: |
paul@2 | 706 | return True |
paul@2 | 707 | else: |
paul@2 | 708 | return False |
paul@2 | 709 | |
paul@2 | 710 | def __ne__(self, other): |
paul@2 | 711 | return not self.__eq__(other) |
paul@2 | 712 | |
paul@2 | 713 | def __hash__(self): |
paul@2 | 714 | return hash(self.media_range) |
paul@2 | 715 | |
paul@2 | 716 | def __repr__(self): |
paul@2 | 717 | return "MediaRange(%r)" % self.media_range |
paul@2 | 718 | |
paul@2 | 719 | def categoryMatches(this, that): |
paul@2 | 720 | |
paul@2 | 721 | """ |
paul@2 | 722 | Return the basis of a match between 'this' and 'that' or False if the given |
paul@2 | 723 | categories do not match. |
paul@2 | 724 | """ |
paul@2 | 725 | |
paul@2 | 726 | return (this == "*" or this == that) and this or \ |
paul@2 | 727 | that == "*" and that or False |
paul@2 | 728 | |
paul@2 | 729 | def categoryPermits(this, that): |
paul@2 | 730 | |
paul@2 | 731 | """ |
paul@2 | 732 | Return whether 'this' category permits 'that' category. Where 'this' is a |
paul@2 | 733 | wildcard ("*"), 'that' should always match. A value of False is returned if |
paul@2 | 734 | the categories do not otherwise match. |
paul@2 | 735 | """ |
paul@2 | 736 | |
paul@2 | 737 | return (this == "*" or this == that) and this or False |
paul@2 | 738 | |
paul@2 | 739 | def getMappingFromParameterStrings(l): |
paul@2 | 740 | |
paul@2 | 741 | """ |
paul@2 | 742 | Return a mapping representing the list of "name=value" strings given by 'l'. |
paul@2 | 743 | """ |
paul@2 | 744 | |
paul@2 | 745 | parameters = {} |
paul@2 | 746 | |
paul@2 | 747 | for parameter in l: |
paul@2 | 748 | parts = parameter.split("=") |
paul@2 | 749 | name = parts[0].strip() |
paul@2 | 750 | value = "=".join(parts[1:]).strip() |
paul@2 | 751 | parameters[name] = value |
paul@2 | 752 | |
paul@2 | 753 | return parameters |
paul@2 | 754 | |
paul@2 | 755 | def getContentPreferences(accept): |
paul@2 | 756 | |
paul@2 | 757 | """ |
paul@2 | 758 | Return a mapping from media types to parameters for content/media types |
paul@2 | 759 | extracted from the given 'accept' header value. The mapping is returned in |
paul@2 | 760 | the form of a list of (media type, parameters) tuples. |
paul@2 | 761 | |
paul@2 | 762 | See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 |
paul@2 | 763 | """ |
paul@2 | 764 | |
paul@2 | 765 | preferences = [] |
paul@2 | 766 | |
paul@2 | 767 | for field in accept.split(","): |
paul@2 | 768 | |
paul@2 | 769 | # The media type with parameters (defined by the "media-range") is |
paul@2 | 770 | # separated from any other parameters (defined as "accept-extension" |
paul@2 | 771 | # parameters) by a quality parameter. |
paul@2 | 772 | |
paul@2 | 773 | fparts = accept_regexp.split(field) |
paul@2 | 774 | |
paul@2 | 775 | # The first part is always the media type. |
paul@2 | 776 | |
paul@2 | 777 | media_type = fparts[0].strip() |
paul@2 | 778 | |
paul@2 | 779 | # Any other parts can be interpreted as extension parameters. |
paul@2 | 780 | |
paul@2 | 781 | if len(fparts) > 1: |
paul@2 | 782 | fparts = ("q=" + ";q=".join(fparts[1:])).split(";") |
paul@2 | 783 | else: |
paul@2 | 784 | fparts = [] |
paul@2 | 785 | |
paul@2 | 786 | # Each field in the preferences can incorporate parameters separated by |
paul@2 | 787 | # semicolon characters. |
paul@2 | 788 | |
paul@2 | 789 | parameters = getMappingFromParameterStrings(fparts) |
paul@2 | 790 | media_range = MediaRange(media_type, parameters) |
paul@2 | 791 | preferences.append(media_range) |
paul@2 | 792 | |
paul@2 | 793 | return ContentPreferences(preferences) |
paul@2 | 794 | |
paul@2 | 795 | class ContentPreferences: |
paul@2 | 796 | |
paul@2 | 797 | "A wrapper around content preference information." |
paul@2 | 798 | |
paul@2 | 799 | def __init__(self, preferences): |
paul@2 | 800 | self.preferences = preferences |
paul@2 | 801 | |
paul@2 | 802 | def __iter__(self): |
paul@2 | 803 | return iter(self.preferences) |
paul@2 | 804 | |
paul@2 | 805 | def get_ordered(self, by_quality=0): |
paul@2 | 806 | |
paul@2 | 807 | """ |
paul@2 | 808 | Return a list of content/media types in descending order of preference. |
paul@2 | 809 | If 'by_quality' is set to a true value, the "q" value will be used as |
paul@2 | 810 | the primary measure of preference; otherwise, only the specificity will |
paul@2 | 811 | be considered. |
paul@2 | 812 | """ |
paul@2 | 813 | |
paul@2 | 814 | ordered = {} |
paul@2 | 815 | |
paul@2 | 816 | for media_range in self.preferences: |
paul@2 | 817 | specificity = media_range.get_specificity() |
paul@2 | 818 | |
paul@2 | 819 | if by_quality: |
paul@2 | 820 | q = float(media_range.accept_parameters.get("q", "1")) |
paul@2 | 821 | key = q, specificity |
paul@2 | 822 | else: |
paul@2 | 823 | key = specificity |
paul@2 | 824 | |
paul@2 | 825 | if not ordered.has_key(key): |
paul@2 | 826 | ordered[key] = [] |
paul@2 | 827 | |
paul@2 | 828 | ordered[key].append(media_range) |
paul@2 | 829 | |
paul@2 | 830 | # Return the preferences in descending order of quality and specificity. |
paul@2 | 831 | |
paul@2 | 832 | keys = ordered.keys() |
paul@2 | 833 | keys.sort(reverse=True) |
paul@2 | 834 | return [ordered[key] for key in keys] |
paul@2 | 835 | |
paul@14 | 836 | def get_acceptable_types(self, available): |
paul@2 | 837 | |
paul@2 | 838 | """ |
paul@14 | 839 | Return content/media types from those in the 'available' list supported |
paul@14 | 840 | by the known preferences grouped by preference level in descending order |
paul@14 | 841 | of preference. |
paul@2 | 842 | """ |
paul@2 | 843 | |
paul@2 | 844 | matches = {} |
paul@2 | 845 | available = set(available[:]) |
paul@2 | 846 | |
paul@2 | 847 | for level in self.get_ordered(): |
paul@2 | 848 | for media_range in level: |
paul@2 | 849 | |
paul@2 | 850 | # Attempt to match available types. |
paul@2 | 851 | |
paul@2 | 852 | found = set() |
paul@2 | 853 | for available_type in available: |
paul@2 | 854 | if media_range.permits(available_type): |
paul@2 | 855 | q = float(media_range.accept_parameters.get("q", "1")) |
paul@2 | 856 | if not matches.has_key(q): |
paul@2 | 857 | matches[q] = [] |
paul@2 | 858 | matches[q].append(available_type) |
paul@2 | 859 | found.add(available_type) |
paul@2 | 860 | |
paul@2 | 861 | # Stop looking for matches for matched available types. |
paul@2 | 862 | |
paul@2 | 863 | if found: |
paul@2 | 864 | available.difference_update(found) |
paul@2 | 865 | |
paul@2 | 866 | # Sort the matches in descending order of quality. |
paul@2 | 867 | |
paul@2 | 868 | all_q = matches.keys() |
paul@2 | 869 | |
paul@2 | 870 | if all_q: |
paul@2 | 871 | all_q.sort(reverse=True) |
paul@14 | 872 | return [matches[q] for q in all_q] |
paul@2 | 873 | else: |
paul@14 | 874 | return [] |
paul@14 | 875 | |
paul@14 | 876 | def get_preferred_types(self, available): |
paul@14 | 877 | |
paul@14 | 878 | """ |
paul@14 | 879 | Return the preferred content/media types from those in the 'available' |
paul@14 | 880 | list, given the known preferences. |
paul@14 | 881 | """ |
paul@14 | 882 | |
paul@14 | 883 | preferred = self.get_acceptable_types(available) |
paul@14 | 884 | if preferred: |
paul@14 | 885 | return preferred[0] |
paul@14 | 886 | else: |
paul@14 | 887 | return [] |
paul@2 | 888 | |
paul@25 | 889 | # Content type parsing. |
paul@25 | 890 | |
paul@25 | 891 | def getContentTypeAndEncoding(content_type): |
paul@25 | 892 | |
paul@25 | 893 | """ |
paul@25 | 894 | Return a tuple with the content/media type and encoding, extracted from the |
paul@25 | 895 | given 'content_type' header value. |
paul@25 | 896 | """ |
paul@25 | 897 | |
paul@25 | 898 | m = encoding_regexp.search(content_type) |
paul@25 | 899 | if m: |
paul@25 | 900 | return m.group("content_type"), m.group("encoding") |
paul@25 | 901 | else: |
paul@25 | 902 | return None, None |
paul@25 | 903 | |
paul@1 | 904 | # Page access functions. |
paul@1 | 905 | |
paul@1 | 906 | def getPageURL(page): |
paul@1 | 907 | |
paul@1 | 908 | "Return the URL of the given 'page'." |
paul@1 | 909 | |
paul@1 | 910 | request = page.request |
paul@1 | 911 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@1 | 912 | |
paul@1 | 913 | def getFormat(page): |
paul@1 | 914 | |
paul@1 | 915 | "Get the format used on the given 'page'." |
paul@1 | 916 | |
paul@1 | 917 | return page.pi["format"] |
paul@1 | 918 | |
paul@1 | 919 | def getMetadata(page): |
paul@1 | 920 | |
paul@1 | 921 | """ |
paul@1 | 922 | Return a dictionary containing items describing for the given 'page' the |
paul@1 | 923 | page's "created" time, "last-modified" time, "sequence" (or revision number) |
paul@1 | 924 | and the "last-comment" made about the last edit. |
paul@1 | 925 | """ |
paul@1 | 926 | |
paul@1 | 927 | request = page.request |
paul@1 | 928 | |
paul@1 | 929 | # Get the initial revision of the page. |
paul@1 | 930 | |
paul@1 | 931 | revisions = page.getRevList() |
paul@26 | 932 | |
paul@26 | 933 | if not revisions: |
paul@26 | 934 | return {} |
paul@26 | 935 | |
paul@1 | 936 | event_page_initial = Page(request, page.page_name, rev=revisions[-1]) |
paul@1 | 937 | |
paul@1 | 938 | # Get the created and last modified times. |
paul@1 | 939 | |
paul@1 | 940 | initial_revision = getPageRevision(event_page_initial) |
paul@1 | 941 | |
paul@1 | 942 | metadata = {} |
paul@1 | 943 | metadata["created"] = initial_revision["timestamp"] |
paul@1 | 944 | latest_revision = getPageRevision(page) |
paul@1 | 945 | metadata["last-modified"] = latest_revision["timestamp"] |
paul@1 | 946 | metadata["sequence"] = len(revisions) - 1 |
paul@1 | 947 | metadata["last-comment"] = latest_revision["comment"] |
paul@1 | 948 | |
paul@1 | 949 | return metadata |
paul@0 | 950 | |
paul@0 | 951 | def getPageRevision(page): |
paul@0 | 952 | |
paul@0 | 953 | "Return the revision details dictionary for the given 'page'." |
paul@0 | 954 | |
paul@0 | 955 | # From Page.edit_info... |
paul@0 | 956 | |
paul@0 | 957 | if hasattr(page, "editlog_entry"): |
paul@0 | 958 | line = page.editlog_entry() |
paul@0 | 959 | else: |
paul@0 | 960 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@0 | 961 | |
paul@0 | 962 | # Similar to Page.mtime_usecs behaviour... |
paul@0 | 963 | |
paul@0 | 964 | if line: |
paul@0 | 965 | timestamp = line.ed_time_usecs |
paul@0 | 966 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@0 | 967 | comment = line.comment |
paul@0 | 968 | else: |
paul@0 | 969 | mtime = 0 |
paul@0 | 970 | comment = "" |
paul@0 | 971 | |
paul@0 | 972 | # Leave the time zone empty. |
paul@0 | 973 | |
paul@0 | 974 | return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + (None,)), "comment" : comment} |
paul@0 | 975 | |
paul@11 | 976 | # Page parsing and formatting of embedded content. |
paul@11 | 977 | |
paul@15 | 978 | def getPageParserClass(request): |
paul@15 | 979 | |
paul@15 | 980 | "Using 'request', return a parser class for the current page's format." |
paul@15 | 981 | |
paul@16 | 982 | return getParserClass(request, getFormat(request.page)) |
paul@15 | 983 | |
paul@11 | 984 | def getParserClass(request, format): |
paul@11 | 985 | |
paul@11 | 986 | """ |
paul@11 | 987 | Return a parser class using the 'request' for the given 'format', returning |
paul@11 | 988 | a plain text parser if no parser can be found for the specified 'format'. |
paul@11 | 989 | """ |
paul@11 | 990 | |
paul@11 | 991 | try: |
paul@11 | 992 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@11 | 993 | except wikiutil.PluginMissingError: |
paul@11 | 994 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@11 | 995 | |
paul@15 | 996 | def getFormatterClass(request, format): |
paul@15 | 997 | |
paul@15 | 998 | """ |
paul@15 | 999 | Return a formatter class using the 'request' for the given output 'format', |
paul@15 | 1000 | returning a plain text formatter if no formatter can be found for the |
paul@15 | 1001 | specified 'format'. |
paul@15 | 1002 | """ |
paul@15 | 1003 | |
paul@15 | 1004 | try: |
paul@15 | 1005 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", format or "plain") |
paul@15 | 1006 | except wikiutil.PluginMissingError: |
paul@15 | 1007 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", "plain") |
paul@15 | 1008 | |
paul@33 | 1009 | def formatText(text, request, fmt, inhibit_p=True, parser_cls=None): |
paul@15 | 1010 | |
paul@15 | 1011 | """ |
paul@15 | 1012 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@15 | 1013 | Suppress line anchors in the output, and fix lists by indicating that a |
paul@15 | 1014 | paragraph has already been started. |
paul@15 | 1015 | """ |
paul@15 | 1016 | |
paul@15 | 1017 | if not parser_cls: |
paul@15 | 1018 | parser_cls = getPageParserClass(request) |
paul@15 | 1019 | parser = parser_cls(text, request, line_anchors=False) |
paul@15 | 1020 | |
paul@15 | 1021 | old_fmt = request.formatter |
paul@15 | 1022 | request.formatter = fmt |
paul@15 | 1023 | try: |
paul@33 | 1024 | return redirectedOutput(request, parser, fmt, inhibit_p=inhibit_p) |
paul@15 | 1025 | finally: |
paul@15 | 1026 | request.formatter = old_fmt |
paul@15 | 1027 | |
paul@11 | 1028 | def redirectedOutput(request, parser, fmt, **kw): |
paul@11 | 1029 | |
paul@11 | 1030 | "A fixed version of the request method of the same name." |
paul@11 | 1031 | |
paul@11 | 1032 | buf = StringIO() |
paul@11 | 1033 | request.redirect(buf) |
paul@11 | 1034 | try: |
paul@11 | 1035 | parser.format(fmt, **kw) |
paul@11 | 1036 | if hasattr(fmt, "flush"): |
paul@11 | 1037 | buf.write(fmt.flush(True)) |
paul@11 | 1038 | finally: |
paul@11 | 1039 | request.redirect() |
paul@11 | 1040 | text = buf.getvalue() |
paul@11 | 1041 | buf.close() |
paul@11 | 1042 | return text |
paul@11 | 1043 | |
paul@25 | 1044 | # Textual representations. |
paul@25 | 1045 | |
paul@25 | 1046 | def getSimpleWikiText(text): |
paul@25 | 1047 | |
paul@25 | 1048 | """ |
paul@25 | 1049 | Return the plain text representation of the given 'text' which may employ |
paul@25 | 1050 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@25 | 1051 | text. |
paul@25 | 1052 | """ |
paul@25 | 1053 | |
paul@25 | 1054 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@25 | 1055 | |
paul@25 | 1056 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@25 | 1057 | |
paul@25 | 1058 | def getEncodedWikiText(text): |
paul@25 | 1059 | |
paul@25 | 1060 | "Encode the given 'text' in a verbatim representation." |
paul@25 | 1061 | |
paul@25 | 1062 | return "<<Verbatim(%s)>>" % text |
paul@25 | 1063 | |
paul@25 | 1064 | def getPrettyTitle(title): |
paul@25 | 1065 | |
paul@25 | 1066 | "Return a nicely formatted version of the given 'title'." |
paul@25 | 1067 | |
paul@25 | 1068 | return title.replace("_", " ").replace("/", u" ? ") |
paul@25 | 1069 | |
paul@0 | 1070 | # User interface functions. |
paul@0 | 1071 | |
paul@0 | 1072 | def getParameter(request, name, default=None): |
paul@0 | 1073 | |
paul@0 | 1074 | """ |
paul@0 | 1075 | Using the given 'request', return the value of the parameter with the given |
paul@0 | 1076 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@0 | 1077 | in the 'request'. |
paul@0 | 1078 | """ |
paul@0 | 1079 | |
paul@0 | 1080 | return get_form(request).get(name, [default])[0] |
paul@0 | 1081 | |
paul@0 | 1082 | def getQualifiedParameter(request, prefix, argname, default=None): |
paul@0 | 1083 | |
paul@0 | 1084 | """ |
paul@0 | 1085 | Using the given 'request', 'prefix' and 'argname', retrieve the value of the |
paul@0 | 1086 | qualified parameter, returning the optional 'default' (or None) if no value |
paul@0 | 1087 | was supplied in the 'request'. |
paul@0 | 1088 | """ |
paul@0 | 1089 | |
paul@0 | 1090 | argname = getQualifiedParameterName(prefix, argname) |
paul@0 | 1091 | return getParameter(request, argname, default) |
paul@0 | 1092 | |
paul@0 | 1093 | def getQualifiedParameterName(prefix, argname): |
paul@0 | 1094 | |
paul@0 | 1095 | """ |
paul@0 | 1096 | Return the qualified parameter name using the given 'prefix' and 'argname'. |
paul@0 | 1097 | """ |
paul@0 | 1098 | |
paul@27 | 1099 | if not prefix: |
paul@0 | 1100 | return argname |
paul@0 | 1101 | else: |
paul@0 | 1102 | return "%s-%s" % (prefix, argname) |
paul@0 | 1103 | |
paul@0 | 1104 | # Page-related functions. |
paul@0 | 1105 | |
paul@0 | 1106 | def getPrettyPageName(page): |
paul@0 | 1107 | |
paul@0 | 1108 | "Return a nicely formatted title/name for the given 'page'." |
paul@0 | 1109 | |
paul@0 | 1110 | title = page.split_title(force=1) |
paul@0 | 1111 | return getPrettyTitle(title) |
paul@0 | 1112 | |
paul@25 | 1113 | def linkToPage(request, page, text, query_string=None, anchor=None, **kw): |
paul@0 | 1114 | |
paul@0 | 1115 | """ |
paul@0 | 1116 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@25 | 1117 | optional 'query_string' and 'anchor'. |
paul@0 | 1118 | """ |
paul@0 | 1119 | |
paul@0 | 1120 | text = wikiutil.escape(text) |
paul@25 | 1121 | return page.link_to_raw(request, text, query_string, anchor, **kw) |
paul@0 | 1122 | |
paul@25 | 1123 | def linkToResource(url, request, text, query_string=None, anchor=None): |
paul@0 | 1124 | |
paul@0 | 1125 | """ |
paul@0 | 1126 | Using 'request', return a link to 'url' with the given link 'text' and |
paul@25 | 1127 | optional 'query_string' and 'anchor'. |
paul@0 | 1128 | """ |
paul@0 | 1129 | |
paul@25 | 1130 | if anchor: |
paul@25 | 1131 | url += "#%s" % anchor |
paul@25 | 1132 | |
paul@0 | 1133 | if query_string: |
paul@0 | 1134 | query_string = wikiutil.makeQueryString(query_string) |
paul@25 | 1135 | url += "?%s" % query_string |
paul@0 | 1136 | |
paul@0 | 1137 | formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter |
paul@0 | 1138 | |
paul@0 | 1139 | output = [] |
paul@0 | 1140 | output.append(formatter.url(1, url)) |
paul@0 | 1141 | output.append(formatter.text(text)) |
paul@0 | 1142 | output.append(formatter.url(0)) |
paul@0 | 1143 | return "".join(output) |
paul@0 | 1144 | |
paul@0 | 1145 | def getFullPageName(parent, title): |
paul@0 | 1146 | |
paul@0 | 1147 | """ |
paul@0 | 1148 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@0 | 1149 | and 'title' (a simple page name). |
paul@0 | 1150 | """ |
paul@0 | 1151 | |
paul@0 | 1152 | if parent: |
paul@0 | 1153 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@0 | 1154 | else: |
paul@0 | 1155 | return title |
paul@0 | 1156 | |
paul@0 | 1157 | # vim: tabstop=4 expandtab shiftwidth=4 |