Attachment 'GetTOC-1.0.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - GetTOC macro Version 1.0, 25.06.2014
4
5 Get the Table of Contents for a page with or without specifying a maxium
6 depth. This macro is used by the 'collect' parser that makes a collection
7 of formatted pages for printing or storing as a PDF and includes the option
8 for a cover page with Tables of Contents from each collected page.
9
10 Syntax:
11 <<GetTOC(pagename)>>
12 <<GetTOC(pagename, maxdepth)>>
13
14 @copyright: 2014 Ian Riley <ian@riley.asia>
15 license: GNU GPL, see COPYING for details.
16
17 """
18
19 from MoinMoin import wikiutil
20 from MoinMoin.Page import Page
21
22 def execute(macro, args, maxdepth=int):
23 request = macro.request
24 _ = request.getText
25 page_name = macro.formatter.page.page_name
26
27 toc_name = page_name
28 maxdepth = 99
29
30 if args:
31 toc_name = args
32 if ',' in args:
33 toc_name, maxdepth = args.split(',',1)
34 maxdepth = int(maxdepth) or 99
35 toc_name = toc_name or page_name
36
37 tocMacro = wikiutil.importPlugin(macro.cfg, u'macro', u'TableOfContents',
38 u'macro_TableOfContents')
39
40 if request.user.may.read(toc_name):
41 saved_raw = macro.parser.raw
42 macro.parser.raw = Page(request, toc_name).get_data()
43 macro.formatter.page.page_name = toc_name
44 result = tocMacro(macro, int(maxdepth))
45 macro.formatter.page.page_name = page_name
46 macro.parser.raw = saved_raw
47 else:
48 return _("You are not allowed to view this page.")
49
50 if 'href' not in result:
51 result = '<div class="table-of-contents">' + \
52 '<p class="table-of-contents-heading">Contents<ol></ol></li></ol></div>'
53
54 anchor = wikiutil.anchor_name_from_text(toc_name)
55 result = result.replace('Contents',
56 '<a href="#%s.top">Contents</a>' % anchor)
57
58 return result
You are not allowed to attach a file to this page.