Commits

Peter Nixon committed 807bee0

Initial proof of concept

  • Participants

Comments (0)

Files changed (7)

hgwiki/__init__.py

Empty file added.
+from django.core.cache import cache
+from django.db import models

hgwiki/templates/hgwiki/base.html

+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+  <title>{{ site.name }} - {% block title %}main{% endblock %}</title>
+</head>
+<body>
+{% block content %}{% endblock %}
+</body>
+</html>

hgwiki/templates/hgwiki/index.html

+{% extends "base.html" %}
+
+{% block content %}
+<H1>Wiki Index</H1>
+<ul>
+    {% for page in pages %}
+    <li><a href="{{ page }}">{{ page }}</a></li>
+    {% endfor %}
+</ul>
+{% endblock %}

hgwiki/templates/hgwiki/page.html

+{% extends "base.html" %}
+
+{% block content %}
+{{ page }}
+{% endblock %}
+
+from django.conf.urls.defaults import *
+
+
+urlpatterns = patterns('hgwiki.views',
+    url(r'^$', 'wiki_index', name="hgwiki_index"),
+    url(r'^(?P<slug>[-\w]+\.rst)$', 'wiki_page', name="hgwiki_page"),
+)
+# -*- coding: utf-8 -*-
+from django.http import Http404, HttpResponse
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.utils.safestring import mark_safe
+
+from django.conf import settings
+
+from docutils.core import publish_parts
+import os
+
+def listDirectory(directory, fileExtList=['.rst', '.wiki']):
+    "get list of file info objects for files of particular extensions"
+    fileList = [os.path.normcase(f)
+                for f in os.listdir(directory)
+                if os.path.splitext(f)[1] in fileExtList]
+    return fileList
+
+
+def wiki_index(request):
+    """
+    Renders a list of available wiki pages
+    """
+    # FIXME: Handle missing HGWIKI_DIRECTORY_PATH
+    pages = listDirectory(settings.HGWIKI_DIRECTORY_PATH)
+
+    return render_to_response("hgwiki/index.html", {
+        "pages": pages,
+    }, context_instance=RequestContext(request))
+
+
+def wiki_page(request, slug):
+    """
+    Tries to find a document matching the given slug, throws 404 if it can't
+    """
+    file = os.path.join(settings.HGWIKI_DIRECTORY_PATH, slug)
+    if os.path.isfile(file) == False:
+      raise Http404
+
+    page = open(file, 'r')
+
+    SAFE_DOCUTILS = dict(file_insertion_enabled=False, raw_enabled=False)
+    content = publish_parts(page.read(), writer_name='html', settings_overrides=SAFE_DOCUTILS)['html_body']
+
+    return render_to_response("hgwiki/page.html", {
+        "page": mark_safe(content)
+    }, context_instance=RequestContext(request))
+