7
7
from ivle.webapp.base.xhtml import XHTMLView
8
8
from ivle.webapp.errors import NotFound, Forbidden
9
9
from ivle.webapp.publisher.decorators import forward_route, reverse_route
10
from ivle.webapp.publisher import ROOT
10
11
from ivle.webapp import ApplicationRoot
12
def generate_toc(plugins, req):
13
def generate_toc(plugins):
14
toc = HelpTree(None, None, {})
14
15
for plugin in plugins:
15
16
if hasattr(plugin, 'help'):
16
17
# Get the dir the plugin resides in
21
22
def add_dict(newdict, curdict, plugin):
22
23
for key in curdict:
23
if key not in newdict:
25
24
if isinstance(curdict[key], dict):
25
if key not in newdict:
26
newdict[key] = HelpTree(newdict, key, {})
26
27
add_dict(newdict[key], curdict[key], plugin)
28
newdict[key] = os.path.join(plugin, curdict[key])
29
newdict[key] = HelpEntry(newdict, key, os.path.join(plugin, curdict[key]))
31
32
class HelpTreeView(XHTMLView):
39
40
def populate(self, req, ctx):
40
41
self.plugin_styles[Plugin] = ['help.css']
42
ctx['toc'] = self.context.tree
44
ctx['context'] = self.context
45
ctx['HelpTree'] = HelpTree
46
ctx['HelpEntry'] = HelpEntry
44
48
class HelpEntryView(XHTMLView):
52
56
def populate(self, req, ctx):
53
57
self.plugin_styles[Plugin] = ['help.css']
55
59
ctx['helpfile'] = self.context.file
57
class HelpTree(object):
58
def __init__(self, tree):
62
def __init__(self, parent, name, tree):
63
super(HelpTree, self).__init__(tree)
61
67
class HelpEntry(object):
62
def __init__(self, file):
68
def __init__(self, parent, name, file):
65
73
@forward_route(ApplicationRoot, '+help')
66
74
def root_to_helptree(root):
67
return HelpTree(generate_toc(root.config.plugin_index[ViewPlugin], None))
75
return HelpTree(None, None, generate_toc(root.config.plugin_index[ViewPlugin]))
69
77
@forward_route(HelpTree, argc=1)
70
78
def helptree_to_help(help, subhelp_name):
72
sub = help.tree[subhelp_name]
80
return help[subhelp_name]
78
82
# No help entry of that name
85
@reverse_route(HelpTree)
86
def helptree_url(helptree):
87
if helptree.parent is None:
88
return (ROOT, '+help')
89
return (helptree.parent, helptree.name)
91
@reverse_route(HelpEntry)
92
def helpentry_url(helpentry):
93
return (helpentry.parent, helpentry.name)
81
95
class Plugin(ViewPlugin, MediaPlugin):
82
96
"""The plugin for viewing help files."""
83
97
forward_routes = (root_to_helptree, helptree_to_help)
98
reverse_routes = (helptree_url, helpentry_url)
84
100
views = [(HelpTree, '+index', HelpTreeView),
85
101
(HelpEntry, '+index', HelpEntryView)]