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