def diff(title, from_rev, to_rev): """Show the differences between specified revisions.""" _ = current_app.gettext p = page.get_page(request, title) build = current_app.get_url from_url = build(view='revision', title=title, rev=from_rev) to_url = build(view='revision', title=title, rev=to_rev) a = werkzeug.html.a links = { 'link1': a(str(from_rev), href=from_url), 'link2': a(str(to_rev), href=to_url), 'link': a(werkzeug.html(title), href=current_app.get_url(title)), } message = werkzeug.html(_( 'Differences between revisions %(link1)s and %(link2)s ' 'of page %(link)s.')) % links diff_content = getattr(p, 'diff_content', None) if diff_content: from_text = current_app.storage.revision_text(p.title, from_rev) to_text = current_app.storage.revision_text(p.title, to_rev) content = diff_content(from_text, to_text, message) else: content = [werkzeug.html.p(werkzeug.html( _("Diff not available for this kind of pages.")))] special_title = _('Diff for "%(title)s"') % {'title': title} html = p.template('page_special.html', content=content, special_title=special_title) resp = response.WikiResponse(html, content_type='text/html') return resp
def edit(title, preview=None, captcha_error=None): page.check_lock(current_app, title) exists = title in current_app.storage p = page.get_page(request, title, wiki=current_app) html = p.render_editor(preview, captcha_error) if not exists: resp = response.WikiResponse(html, content_type="text/html", status=404) elif preview: resp = response.WikiResponse(html, content_type="text/html") else: resp = response.response(request, title, html, '/edit') resp.headers['Cache-Control'] = 'no-cache' return resp
def version(title=None): if title is None: version = current_app.storage.repo_revision() else: try: version, x, x, x = next(current_app.storage.page_history(title)) except StopIteration: version = 0 return response.WikiResponse('%d' % version, content_type="text/plain")
def search(): """Serve the search results page.""" _ = current_app.gettext def highlight_html(m): return werkzeug.html.b(m.group(0), class_="highlight") def search_snippet(title, words): """Extract a snippet of text for search results.""" try: text = current_app.storage.page_text(title) except error.NotFoundErr: return '' regexp = re.compile("|".join(re.escape(w) for w in words), re.U | re.I) match = regexp.search(text) if match is None: return "" position = match.start() min_pos = max(position - 60, 0) max_pos = min(position + 60, len(text)) snippet = werkzeug.escape(text[min_pos:max_pos]) html = regexp.sub(highlight_html, snippet) return html def page_search(words, page, request): """Display the search results.""" h = werkzeug.html current_app.index.update(current_app) result = sorted(current_app.index.find(words), key=lambda x: -x[0]) yield werkzeug.html.p(h(_('%d page(s) containing all words:') % len(result))) yield '<ol id="hatta-search-results">' for number, (score, title) in enumerate(result): yield h.li(h.b(page.wiki_link(title)), ' ', h.i(str(score)), h.div(search_snippet(title, words), class_="hatta-snippet"), id_="search-%d" % (number + 1)) yield '</ol>' query = request.values.get('q', '').strip() p = page.get_page(request, '') if not query: url = current_app.get_url(view='all_pages', external=True) return response.redirect(url, code=303) words = tuple(current_app.index.split_text(query)) if not words: words = (query,) title = _('Searching for "%s"') % " ".join(words) content = page_search(words, p, request) html = p.template('page_special.html', content=content, special_title=title) return response.WikiResponse(html, content_type='text/html')
def backlinks(title): """Serve the page with backlinks.""" current_app.index.update(current_app) p = page.get_page(request, title) html = p.template('backlinks.html', pages=current_app.index.page_backlinks(title)) resp = response.WikiResponse(html, content_type='text/html') resp.set_etag('/+search/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp
def sister_pages(): """Show index of all pages in a format suitable for SisterPages.""" text = [ '%s%s %s\n' % (request.base_url, current_app.get_url(title), title) for title in current_app.storage.all_pages() ] text.sort() resp = response.WikiResponse(text, content_type='text/plain') resp.set_etag('/+sister-index/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp
def _serve_default(request, title, content=None, mime=None): """Some pages have their default content.""" if title in current_app.storage: return download(title) if content is None: content = pkgutil.get_data('datta.wiki', os.path.join('static', title)) mime = mime or 'application/octet-stream' resp = response.WikiResponse( content, content_type=mime, ) resp.set_etag('/%s/-1' % title) resp.make_conditional(request) return resp
def all_pages(): """Show index of all pages in the current_app.""" _ = current_app.gettext p = page.get_page(request, '') html = p.template('list.html', pages=sorted(current_app.storage.all_pages()), class_='index', message=_('Index of all pages'), special_title=_('Page Index')) resp = response.WikiResponse(html, content_type='text/html') resp.set_etag('/+index/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp
def wanted(): """Show all pages that don't exist yet, but are linked.""" def _wanted_pages_list(): for refs, title in current_app.index.wanted_pages(): if not (parser.external_link(title) or title.startswith('+') or title.startswith(':')): yield refs, title p = page.get_page(request, '') html = p.template('wanted.html', pages=_wanted_pages_list()) resp = response.WikiResponse(html, content_type='text/html') resp.set_etag('/+wanted/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp
def orphaned(): """Show all pages that don't have backlinks.""" _ = current_app.gettext p = page.get_page(request, '') orphaned = [ title for title in current_app.index.orphaned_pages() if title in current_app.storage ] html = p.template('list.html', pages=orphaned, class_='orphaned', message=_('List of pages with no links to them'), special_title=_('Orphaned pages')) resp = response.WikiResponse(html, content_type='text/html') resp.set_etag('/+orphaned/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp
def recent_changes(): """Serve the recent changes page.""" def _changes_list(): last = {} lastrev = {} count = 0 make_url = current_app.get_url for title, rev, date, author, comment in current_app.storage.history(): if (author, comment) == last.get(title, (None, None)): continue count += 1 if count > 100: break if rev > 0: date_url = make_url(view='diff', title=title, from_rev=rev - 1, to_rev=lastrev.get(title, rev), ) elif rev == 0: date_url = make_url(view='revision', title=title, rev=rev, ) else: date_url = make_url(view='history', title=title) last[title] = author, comment lastrev[title] = rev yield date, date_url, title, author, comment p = page.get_page(request, '') html = p.template('changes.html', changes=_changes_list(), date_html=page.date_html) resp = response.WikiResponse(html, content_type='text/html') resp.set_etag('/history/%d' % current_app.storage.repo_revision()) resp.make_conditional(request) return resp