Changeset 2801
- Timestamp:
- 11/19/07 19:21:05 (1 year ago)
- Files:
-
- tocmacro/0.11/tractoc/macro.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
tocmacro/0.11/tractoc/macro.py
r1955 r2801 7 7 8 8 from trac.core import * 9 from trac.resource import get_resource_url 10 from trac.util.compat import sorted 9 11 from trac.wiki.formatter import OutlineFormatter, system_message 10 12 from trac.wiki.api import WikiSystem, parse_args 11 from trac.wiki.macros import WikiMacroBase 13 from trac.wiki.macros import WikiMacroBase 14 from trac.wiki.model import WikiPage 12 15 13 16 … … 18 21 19 22 20 def outline_tree( ol, outline, context, active, min_depth, max_depth):23 def outline_tree(env, ol, outline, context, active, min_depth, max_depth): 21 24 if min_depth > max_depth: 22 25 min_depth, max_depth = max_depth, min_depth … … 40 43 li.append(new_ol) 41 44 stack[d+1] = (None, new_ol) 42 href = context.resource_href()45 href = get_resource_url(env, context.resource, context.href) 43 46 if href.endswith(context.req.path_info): 44 47 href = '' … … 69 72 TracTimeline, TracRss, TracNotification)]] 70 73 }}} 74 A wildcard '*' can be used to fetch a sorted list of all pages starting with 75 the preceding pagename stub: 76 {{{ 77 [[TOC(Trac*, WikiFormatting, WikiMacros)]] 78 }}} 71 79 The following ''control'' arguments change the default behaviour of 72 80 the TOC macro: … … 77 85 || {{{inline}}} || Display TOC inline rather than as a side-bar. || 78 86 || {{{titleindex}}} || Only display the page name and title of each page, similar to TitleIndex. || 79 80 Note that the current page must also be specified if individual wiki 81 pages are given in the argument list. 87 || {{{notitle}}} || Supress display of page title. || 88 For 'titleindex' argument, an empty pagelist will evaluate to all pages: 89 {{{ 90 [[TOC(titleindex, notitle, heading=All pages)]] 91 }}} 82 92 """ 83 93 84 94 def expand_macro(self, formatter, name, args): 85 95 self.formatter = formatter 86 context = formatter.context 96 self.context = formatter.context 97 self.resource = formatter.context.resource 87 98 88 99 # Bail out if we are in a no-float zone … … 91 102 return '' 92 103 93 current_page = context.id104 current_page = self.resource.id 94 105 95 106 # Split the args … … 98 109 inline = False 99 110 pagenames = [] 100 heading = kw.pop('heading', 'Table of Contents')101 111 112 default_heading = 'Table of Contents' 102 113 params = {'min_depth': 1, 'max_depth': 6} 103 114 # Global options … … 107 118 inline = True 108 119 elif arg == 'noheading': 109 heading = ''120 default_heading = '' 110 121 elif arg == 'notitle': 111 122 params['min_depth'] = 2 # Skip page title 112 123 elif arg == 'titleindex': 113 124 params['title_index'] = True 114 heading = ''125 default_heading = default_heading and 'Page Index' 115 126 elif arg == 'nofloat': 116 127 return '' 117 128 elif arg != '': 118 129 pagenames.append(arg) 130 heading = kw.pop('heading', '') or default_heading 119 131 120 132 if 'depth' in kw: … … 122 134 123 135 # Has the user supplied a list of pages? 124 if not pagenames and 'title_index' not in params: 125 pagenames.append(current_page) 126 params['root'] = '' 127 params['min_depth'] = 2 # Skip page title 128 129 base = tag.div(class_=not inline and 'wiki-toc' or '') 136 if not pagenames: 137 if 'title_index' in params: 138 pagenames.append('*') # A marker for 'all' 139 else: 140 pagenames.append(current_page) 141 params['root'] = '' 142 params['min_depth'] = 2 # Skip page title 143 # Check for wildcards and expand lists 144 temp_pagenames = [] 145 for pagename in pagenames: 146 if pagename.endswith('*'): 147 temp_pagenames.extend(sorted( 148 WikiSystem(self.env).get_pages(pagename[:-1]))) 149 else: 150 temp_pagenames.append(pagename) 151 pagenames = temp_pagenames 152 153 base = tag.div(class_=inline and 'wiki-toc-inline' or 'wiki-toc') 130 154 ol = tag.ol() 131 155 base.append([heading and tag.h4(heading), ol]) 132 156 133 157 active = len(pagenames) > 1 134 ifpagenames:135 for pagename in pagenames:136 if 'title_index' in params:137 prefix = pagename.split('/')[0]138 prefix = prefix.replace("'", "''") # FIXME: what's this?139 self._render_title_index(ol, prefix, active and \140 pagename.startswith(current_page))141 else:142 self._render_page_outline(ol, pagename, active, params)143 else:144 self._render_title_index(ol, '', False)158 for pagename in pagenames: 159 page_resource = self.resource(id=pagename) 160 if not 'WIKI_VIEW' in self.context.perm(page_resource): 161 # Not access to the page, so should not be included 162 continue 163 if 'title_index' in params: 164 self._render_title_index(ol, page_resource, active and \ 165 pagename == current_page, 166 params['min_depth'] < 2) 167 else: 168 self._render_page_outline(ol, page_resource, active, params) 145 169 return base 146 170 147 def get_page_text(self, page name):148 """Return a tuple of `(text, exists)` for the given `pagename`."""149 if page name == self.formatter.context.id:171 def get_page_text(self, page_resource): 172 """Return a tuple of `(text, exists)` for the given page (resource).""" 173 if page_resource.id == self.resource.id: 150 174 return (self.formatter.source, True) 151 175 else: 152 # TODO: after sandbox/security merge 153 # page = context(id=pagename).resource 154 from trac.wiki.model import WikiPage 155 page = WikiPage(self.env, pagename, db=self.formatter.db) 176 page = WikiPage(self.env, page_resource) 156 177 return (page.text, page.exists) 157 178 158 def _render_title_index(self, ol, prefix, active): 159 all_pages = list(WikiSystem(self.env).get_pages(prefix)) 160 if all_pages: 161 all_pages.sort() 162 for page in all_pages: 163 ctx = self.formatter.context('wiki', page) 164 fmt = OutlineFormatter(ctx) 165 page_text, _ = self.get_page_text(page) # ctx.resource.text 166 fmt.format(page_text, NullOut()) 167 title = '' 168 if fmt.outline: 169 title = ': ' + fmt.outline[0][2] 170 ol.append((tag.li(tag.a(page, href=ctx.resource_href()), 171 Markup(title), 172 class_= active and 'active' or None))) 173 else: 179 def _render_title_index(self, ol, page_resource, active, show_title): 180 page_text, page_exists = self.get_page_text(page_resource) 181 if not page_exists: 174 182 ol.append(system_message('Error: No page matching %s found' % 175 prefix)) 176 177 def _render_page_outline(self, ol, pagename, active, params): 178 page = params.get('root', '') + pagename 179 page_text, page_exists = self.get_page_text(page) 183 page_resource.id)) 184 return 185 ctx = self.context(page_resource) 186 fmt = OutlineFormatter(self.env, ctx) 187 fmt.format(page_text, NullOut()) 188 title = '' 189 if show_title and fmt.outline: 190 title = ': ' + fmt.outline[0][2] 191 ol.append((tag.li(tag.a(page_resource.id, 192 href=get_resource_url(self.env, page_resource, ctx.href)), 193 Markup(title), 194 class_= active and 'active' or None))) 195 196 def _render_page_outline(self, ol, page_resource, active, params): 197 page = params.get('root', '') + page_resource.id 198 page_text, page_exists = self.get_page_text(page_resource) 180 199 if page_exists: 181 ctx = self. formatter.context('wiki', page)182 fmt = OutlineFormatter( ctx)200 ctx = self.context(page_resource) 201 fmt = OutlineFormatter(self.env, ctx) 183 202 fmt.format(page_text, NullOut()) 184 outline_tree( ol, fmt.outline, ctx,185 active and page == self.formatter.context.id,203 outline_tree(self.env, ol, fmt.outline, ctx, 204 active and page_resource.id == self.resource.id, 186 205 params['min_depth'], params['max_depth']) 187 206 else:
