Add Wiki syntax for page links, style updates

This commit is contained in:
Noah 2015-01-25 02:05:14 -08:00
parent 13b5c18062
commit 8a7cf66248
4 changed files with 58 additions and 6 deletions

View File

@ -2,13 +2,45 @@
"""Wiki models."""
from flask import url_for
import time
import re
import hashlib
import rophako.jsondb as JsonDB
from rophako.utils import render_markdown
from rophako.log import logger
def render_page(content):
"""Render the Markdown content of a Wiki page, and support inter-page
linking with [[double braces]].
For simple links, just use the [[Page Name]]. To have a different link text
than the page name, use [[Link Text|Page Name]]."""
html = render_markdown(content)
# Look for [[double brackets]]
links = re.findall(r'\[\[(.+?)\]\]', html)
for match in links:
label = page = match
if "|" in match:
label, page = match.split("|", 2)
# Does the page exist?
output = '''<a href="{url}">{label}</a>'''
if not JsonDB.exists("wiki/pages/{}".format(page)):
output = '''<a href="{url}" class="wiki-broken">{label}</a>'''
html = html.replace("[[{}]]".format(match),
output.format(
url=url_for("wiki.view_page", name=name_to_url(page)),
label=label,
)
)
return html
def get_page(name):
"""Get a Wiki page. Returns `None` if the page isn't found."""
name = name.strip("/") # Remove any surrounding slashes.

View File

@ -7,8 +7,7 @@ from flask import Blueprint, g, request, redirect, url_for, flash
import rophako.model.user as User
import rophako.model.wiki as Wiki
import rophako.model.emoticons as Emoticons
from rophako.utils import (template, render_markdown, pretty_time,
login_required)
from rophako.utils import template, pretty_time, render_markdown, login_required
from rophako.settings import Config
mod = Blueprint("wiki", __name__, url_prefix="/wiki")
@ -33,8 +32,12 @@ def list_pages():
@mod.route("/<path:name>")
def view_page(name):
"""Show a specific wiki page."""
link = name
name = Wiki.url_to_name(name)
g.info["link"] = link
g.info["title"] = name
# Look up the page.
page = Wiki.get_page(name)
if not page:
@ -59,10 +62,19 @@ def view_page(name):
# Show the latest one.
rev = page["revisions"][0]
# Getting the plain text source?
if request.args.get("source", None):
g.info["markdown"] = render_markdown("\n".join([
"# Source: {}".format(name),
"",
"```markdown",
rev["body"],
"```"
]))
return template("markdown.inc.html")
# Render it!
g.info["link"] = Wiki.name_to_url(name)
g.info["title"] = name
g.info["rendered_body"] = render_markdown(rev["body"])
g.info["rendered_body"] = Wiki.render_page(rev["body"])
g.info["rendered_body"] = Emoticons.render(g.info["rendered_body"])
g.info["pretty_time"] = pretty_time(Config.wiki.time_format, rev["time"])
@ -131,7 +143,7 @@ def edit():
g.info["preview"] = True
# Render markdown
g.info["rendered_body"] = render_markdown(body)
g.info["rendered_body"] = Wiki.render_page(body)
# Render emoticons.
g.info["rendered_body"] = Emoticons.render(g.info["rendered_body"])

View File

@ -7,6 +7,7 @@
[ <a href="{{ url_for('wiki.history', name=link) }}">History</a>
| <a href="{{ url_for('wiki.list_pages') }}">Index</a>
| <a href="{{ url_for('wiki.view_page', name=link, source='1') }}">Source</a>
{% if session["login"] %}
| <a href="{{ url_for('wiki.edit', name=title) }}">Edit</a>
| <a href="{{ url_for('wiki.edit') }}">New Page</a>
@ -16,6 +17,8 @@
<h1>{{ title }}</h1>
<hr>
{{ rendered_body|safe }}
{% endblock %}

View File

@ -314,6 +314,11 @@ ul.blog-categories li:last-child:after {
font-size: 9pt;
}
/* Wiki styles */
a.wiki-broken {
text-decoration: line-through;
}
/* Visitor history page */
.visitor-graph {
height: 16px;