proxy: Make sure root pages get the right theme.
[matthijs/projects/wipi.git] / plugin / theme / proxy.py
1 # -*- coding: iso-8859-1 -*-
2 """
3     MoinMoin - Generic website theme.
4
5     @copyright: 2009+ Matthijs Kooijman
6     @license: GNU GPL, see COPYING for details.
7
8     This theme simply loads other themes, depending on the url requested.
9
10     To use this theme, set the default theme to 'proxy' and set the
11     configuration value 'proxy_theme_mapping' to a list of (prefix, themename)
12     tuples (which will be tested in order, so be sure to include a theme with
13     an empty prefix at the end). For example:
14
15         proxy_theme_mapping = [
16             ('/Site/', 'deadfoxjunction'),
17             ('', 'modernized'),
18         ]
19
20     The prefixes are matched against the page requested, so urls within
21     MoinMoin (and a trailing slash is included).
22
23     This theme does not currently play well with user-selectable themes, since
24     that would completely bypass the proxy plugin. For now it is probably best
25     to set the theme_force configuration variable.
26 """
27
28 from MoinMoin.theme import load_theme, ThemeNotFound
29
30 def Theme(context):
31     """
32     Usually, a theme plugin defines a Theme class, whose constructor is
33     called.  Instead, we define a Theme function that calls the proper
34     constructor.
35     """
36     path = context.request.path
37     # Always add a trailing space, so that a /Foo/ also matches the /Foo
38     # path (without needing to remove the trailing space from the
39     # prefix, which would make also match /FooBar).
40     if path[-1] != '/':
41         path += '/'
42     for (prefix, theme) in context.cfg.proxy_theme_mapping:
43         if path.startswith(prefix):
44             return load_theme(context, theme)
45
46     raise ThemeNotFound("proxy_theme_mapping defines no theme for url: %s" % (context.request.path))
47
48 # vim: set sw=4 sts=4 expandtab: