changeset 407:c4b673db5190

web: Add support for custom styles based on page formatter.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 12 Jan 2017 08:43:21 -0800
parents a65996b2e85d
children a2ed337652e7
files wikked/templates/index.html wikked/templates/read-page.html wikked/webimpl/read.py wikked/wiki.py
diffstat 4 files changed, 43 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/templates/index.html	Thu Jan 12 00:45:53 2017 -0800
+++ b/wikked/templates/index.html	Thu Jan 12 08:43:21 2017 -0800
@@ -1,10 +1,11 @@
 <!doctype html>
 <html>
     <head>
-    <title>{%if page_title%}{{page_title}} &ndash; {%endif%}Wikked</title>
+        <title>{%if page_title%}{{page_title}} &ndash; {%endif%}Wikked</title>
         <meta charset="utf-8"></meta>
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <link rel="stylesheet" type="text/css" href="/static/css/wikked.min.css" />
+        {% block head %}{% endblock %}
     </head>
     <body>
         <div id="app">
--- a/wikked/templates/read-page.html	Thu Jan 12 00:45:53 2017 -0800
+++ b/wikked/templates/read-page.html	Thu Jan 12 08:43:21 2017 -0800
@@ -1,4 +1,5 @@
 {% extends 'index.html' %}
+{% block head %}{{custom_head|safe}}{% endblock %}
 {% block content %}
 <article>
     {% if not meta.notitle %}
--- a/wikked/webimpl/read.py	Thu Jan 12 00:45:53 2017 -0800
+++ b/wikked/webimpl/read.py	Thu Jan 12 08:43:21 2017 -0800
@@ -1,3 +1,4 @@
+import os.path
 import urllib.parse
 from wikked.webimpl import (
         CHECK_FOR_READ,
@@ -12,7 +13,7 @@
         # Normal page.
         page, visited_paths = get_redirect_target(
                 wiki, path,
-                fields=['url', 'title', 'text', 'meta'],
+                fields=['url', 'path', 'title', 'text', 'meta'],
                 check_perms=(user, CHECK_FOR_READ),
                 first_only=no_redirect)
         if page is None:
@@ -23,8 +24,11 @@
         elif len(visited_paths) > 1:
             additional_info['redirected_from'] = visited_paths[:-1]
 
+        ext = os.path.splitext(page.path)[1].lstrip('.')
+        custom_head = wiki.custom_heads.get(ext)
+
         result = {'meta': get_page_meta(page), 'text': page.text,
-                  'page_title': page.title}
+                  'page_title': page.title, 'custom_head': custom_head}
         result.update(additional_info)
         return result
 
@@ -33,7 +37,7 @@
     try:
         info_page = get_page_or_raise(
                 wiki, meta_page_url,
-                fields=['url', 'title', 'text', 'meta'],
+                fields=['url', 'path', 'title', 'text', 'meta'],
                 check_perms=(user, CHECK_FOR_READ))
     except PageNotFoundError:
         # Let permissions errors go through, but if the info page is not
@@ -47,19 +51,27 @@
             # Default page text.
             info_page = get_page_or_raise(
                     wiki, endpoint_info.default,
-                    fields=['url', 'title', 'text', 'meta'],
+                    fields=['url', 'path', 'title', 'text', 'meta'],
                     check_perms=(user, CHECK_FOR_READ))
 
-        if not endpoint_info.query:
-            # Not a query-based endpoint (like categories). Let's just
-            # return the text.
-            result = {
-                    'endpoint': endpoint,
-                    'meta': get_page_meta(info_page),
-                    'text': info_page.text,
-                    'page_title': info_page.title}
-            result.update(additional_info)
-            return result
+    custom_head = None
+    if info_page is not None:
+        ext = os.path.splitext(info_page.path)[1].lstrip('.')
+        custom_head = wiki.custom_heads.get(ext)
+
+    if (endpoint_info is not None and
+            not endpoint_info.query
+            and info_page is not None):
+        # Not a query-based endpoint (like categories). Let's just
+        # return the text.
+        result = {
+                'endpoint': endpoint,
+                'meta': get_page_meta(info_page),
+                'text': info_page.text,
+                'page_title': info_page.title,
+                'custom_head': custom_head}
+        result.update(additional_info)
+        return result
 
     # Get the list of pages to show here.
     value = path.lstrip('/')
@@ -92,7 +104,8 @@
                     'url': urllib.parse.quote(meta_page_url.encode('utf-8')),
                     'title': value
                     },
-            'page_title': page_title
+            'page_title': page_title,
+            'custom_head': custom_head
             }
     if info_page:
         result['text'] = info_page.text
--- a/wikked/wiki.py	Thu Jan 12 00:45:53 2017 -0800
+++ b/wikked/wiki.py	Thu Jan 12 08:43:21 2017 -0800
@@ -44,12 +44,15 @@
             root = os.getcwd()
         self.root = root
         self.context = ctx
-        self.formatters = self.getFormatters()
+        self.formatters = {}
+        self.custom_heads = {}
         self.wiki_updater = synchronous_wiki_updater
         self._config = None
         self._index_factory = None
         self._scm_factory = None
 
+        self._build()
+
     @property
     def config(self):
         if self._config is None:
@@ -74,15 +77,14 @@
     def auth_factory(self):
         return UserManager(self.config)
 
-    def getFormatters(self):
-        formatters = {passthrough_formatter: ['txt', 'html']}
-        self.tryAddFormatter(formatters, 'markdown', 'markdown',
+    def _build(self):
+        self.formatters[passthrough_formatter] = ['txt', 'html']
+        self.tryAddFormatter('markdown', 'markdown',
                              ['md', 'mdown', 'markdown'])
-        self.tryAddFormatter(formatters, 'textile', 'textile',
+        self.tryAddFormatter('textile', 'textile',
                              ['tl', 'text', 'textile'])
-        self.tryAddFormatter(formatters, 'creole', 'creole2html',
+        self.tryAddFormatter('creole', 'creole2html',
                              ['cr', 'creole'])
-        return formatters
 
     def getSpecialFilenames(self):
         yield '.wikirc'
@@ -92,12 +94,11 @@
             for name, val in self.config.items('ignore'):
                 yield val
 
-    def tryAddFormatter(self, formatters, module_name, module_func,
-                        extensions):
+    def tryAddFormatter(self, module_name, module_func, extensions):
         try:
             module = importlib.import_module(module_name)
             func = getattr(module, module_func)
-            formatters[func] = extensions
+            self.formatters[func] = extensions
         except ImportError:
             pass
 
@@ -197,6 +198,7 @@
             raise ValueError("No parameters were given to the wiki.")
 
         self.formatters = parameters.formatters
+        self.custom_heads = parameters.custom_heads
         self.special_filenames = parameters.getSpecialFilenames()
 
         self.main_page_url = (