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