1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import os, inspect
import genshi
import genshi.template
import ivle.conf
from ivle.webapp.base.plugins import ViewPlugin
from ivle.webapp.base.xhtml import XHTMLView
def help_url(plugin, path):
'''Generates a URL to a media file.
Plugin must be a string, which is put into the path literally.'''
return os.path.join(ivle.conf.root_dir, plugin, path)
def generate_toc(plugins, req):
toc = {}
for plugin in plugins:
if hasattr(plugin, 'help'):
# Get the dir the plugin resides in
plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
add_dict(toc, plugin.help, plugindir)
return toc
def add_dict(newdict, curdict, plugin):
for key in curdict:
if key not in newdict:
newdict[key] = {}
if isinstance(curdict[key], dict):
add_dict(newdict[key], curdict[key], plugin)
else:
newdict[key] = help_url(plugin, curdict[key])
return newdict
class HelpView(XHTMLView):
"""Shows the help file for the specified path."""
template = 'helpview.html'
def __init__(self, req, path):
self.paths = path.split('/')
def populate(self, req, ctx):
helpfile = generate_toc(req.plugin_index[ViewPlugin], req)
try:
for path in self.paths:
helpfile = helpfile[path]
ctx['helpfile'] = helpfile
except KeyError:
pass
class HelpToc(XHTMLView):
"""Displays the help Table of Contents."""
appname = 'help'
template = 'toc.html'
def populate(self, req, ctx):
ctx['toc'] = generate_toc(req.plugin_index[ViewPlugin], req)
class Plugin(ViewPlugin):
"""The plugin for viewing help files."""
urls = [
('+help', HelpToc),
('+help/*path', HelpView)
]
|