TOCs
Description
A half-implemented macro to create a table of contents of a reference page. Only half-implemented as I wrote it for myself and my project, discarding entirely all rules for multiple languages and clean coding style.
What it does though will give you a list of section headings and heading level.
Download & Release Notes
Download |
Release Version |
Moin Version |
Release Notes |
No Download (see the Source) |
|
1.9.0 |
<<TOCs( PageName [, Depth=None] )>> Not fully implemented. |
Usage
<<TOCs(PageName [, Depth=None])>>
PageName is the referenced page. Depth will limit page parsing to requested section heading depth.
Source
1 from MoinMoin.Page import Page
2 from MoinMoin import wikiutil
3 import re
4
5 # do not cache
6 Dependencies = ["time"]
7
8 def macro_TOCs(macro, pageName, depth=None):
9 '''
10 macro_TOCs takes page WikiName and processes text to get headings.
11 Depth parameter allows limits on heading depth.
12 '''
13 request = macro.request
14 _ = request.getText
15 response = ""
16 lines = []
17 results = []
18 if pageName is not None:
19 # make page request through moin processor
20 inc_page = Page(request, pageName)
21
22 #gets lines of body
23 lines = inc_page.getlines()
24
25 # headings will be in this format
26 headings = re.compile('^(=+)\s(.*?)\s=+$')
27
28 for line in lines:
29 if line.startswith("="):
30 # stripping ending whitespace
31 m = headings.match(line.rstrip())
32
33 if m:
34 headingNumber = m.group(1)
35 headingTitle = m.group(2)
36
37 # depth 1-5 possible values.
38 # depth of 1 will not provide any results as the pages
39 # are using heading 1 only as the page title
40 if depth is not None:
41 if len(headingNumber) <= int(depth):
42 # append new list onto results list and increment count
43 results.append([len(headingNumber),headingTitle])
44 else:
45 continue
46 else:
47 # append new list onto results list and increment count
48 results.append([len(headingNumber),headingTitle])
49
50
51 #
52 # Do Stuff With Your List Here
53 #
54
55 #
56 # my implementation needed to output raw HTML so below is a little n.b.
57 #
58 # this demonstrates how to create an Link with Anchor (#Section_Heading),
59 baseURL ="http://localhost/" # Needed since we are outputting raw HTML. But should be changed appropriately
60 for item in results:
61 response += "\n\t<li> <a href=\"%s%s#%s\">%s</a> </li>" % (baseURL, wikiutil.url_quote_plus(pageName), wikiutil.anchor_name_from_text(item[1]), wikiutil.escape(item[1]))
Discussion
Using macro.formatter.number_list(1) etc. to create nested lists causes problems, which in turn causes problems to use return macro.formatter.text('Macro output') and the recommended way of outputting raw HTML. My macro response has return macro.formatter.rawHTML(response).