# -*- coding: iso-8859-1 -*- # Dinko Korunic 'kreator', 2006. # hybservtoc-trac-0.11.py # new-style Macro TOC plugin for Trac 0.11 from genshi.builder import tag from trac.core import * from trac.wiki.api import parse_args from trac.wiki.macros import WikiMacroBase from trac.util.compat import sorted, groupby, any from StringIO import StringIO __all__ = ['TOCMacro'] class TOCMacro(WikiMacroBase): """Shows a table of contents for a given set of Wiki pages. Accepts a prefix string as parameter: if provided, only pages with names that start with the prefix are included in the resulting list. If this parameter is omitted, all pages are listed. All the other pages given as arguments are added to the TOC, too. Examples: {{{ [TOC()]] # all Wiki pages [TOC(WikiStart, prefix=Hybserv)]] # only WikiStart and Hybserv* }}} """ def render_macro(self, formatter, name, content): curpage = formatter.context.id args, kw = parse_args(content) prefix = kw.get('prefix', '') wiki = formatter.wiki pages = sorted(wiki.get_pages(prefix)) if len(args) == 1: pages.extend(args) else: pages.append(args) return tag.div(tag.h4('Table of Contents'), tag.ul([tag.li(tag.a(title, href=formatter.href.wiki(title)), class_=(title == curpage and "active")) for title in pages]), class_="wiki-toc")