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