Changeset 3057
- Timestamp:
- 11/24/05 11:13:47 (8 years ago)
- File:
-
- 1 edited
-
trac/trunk/wiki-macros/TracNav.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trac/trunk/wiki-macros/TracNav.py
r3056 r3057 73 73 from trac.wiki.api import WikiSystem 74 74 from trac.wiki.model import WikiPage 75 76 LISTRULE = re.compile(r"""^(?P<indent> *)\* +""" 77 r"""(?:(?P<wikilink>\[wiki:""" 78 r"""(?P<link>("([^"]*)"|""" 79 r"""'([^']*)')|([^ \]]+))""" 80 r"""+(?P<label>[^\]]*)\])|(?P<text>.*))""", 81 re.M) 75 from trac.wiki.formatter import OneLinerFormatter 76 from StringIO import StringIO 82 77 83 78 TRACNAVHOME = "http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav" 79 LISTRULE = re.compile(r"^(?P<indent> +)\* +(?P<rest>.*)$", re.M) 84 80 85 81 def get_toc(hdf, env, curpage, name): 86 """Fetch the wiki page containing the toc, if available.""" 87 88 toc_text = "* Table of contents" 82 """ 83 Fetch the wiki page containing the toc, if available. 84 """ 85 toc_text = " * Table of contents" 89 86 90 87 preview = hdf.getValue('args.preview', "") 91 88 if preview and (name == curpage): 92 89 toc_text = hdf.getValue('wiki.page_source', toc_text); 93 else: 94 if WikiSystem(env).has_page(name): 95 toc_text = WikiPage(env, name).text 96 97 # env.log.debug(tocText) 90 elif WikiSystem(env).has_page(name): 91 toc_text = WikiPage(env, name).text 98 92 return toc_text 99 93 100 94 101 def get_toc_entry(toc_text): 102 """Generator: returns the next toc entry. Each toc entry consists 103 of it's indentation level, label and link.""" 104 next_pos = 0 105 while 1: 106 match = LISTRULE.search(toc_text, next_pos) 107 if not match: 108 # env.log.debug("No more matches") 109 return 110 95 class TocFormatter(OneLinerFormatter): 96 """ 97 Basically the OneLinerFormatter, but additionally remembers the 98 last wiki link. 99 """ 100 101 def format_toc(self, wikitext, out): 102 OneLinerFormatter.format(self, wikitext, out) 103 return self.link 104 105 def __init__(self, env): 106 OneLinerFormatter.__init__(self, env) 107 self.link = None 108 self.myenv = env 109 110 def _make_link(self, namespace, target, match, label): 111 if namespace == 'wiki': 112 self.link = target 113 return OneLinerFormatter._make_link( 114 self, namespace, target, match, label) 115 116 # FIXME: what about _make_relative_link() ? 117 # FIXME: CamelCase links are special and not handled by the Formatter... 118 119 def format_toc_entry(wikitext, env): 120 out = StringIO() 121 link = TocFormatter(env).format_toc(wikitext, out) 122 return out.getvalue(), link 123 124 125 def get_toc_entry(toc_text, env): 126 """ 127 Filter the toc_text for toc entries. 128 """ 129 for match in LISTRULE.finditer(toc_text): 111 130 indent = len(match.group('indent')) 112 if match.group('wikilink'): 113 link = match.group('link') 114 label = match.group('label') 115 else: 116 link = None 117 label = match.group('text') 118 119 # if link == None: 120 # env.log.debug(label + " ---") 121 # else: 122 # env.log.debug(label + ": " + link) 123 131 label, link = format_toc_entry(match.group('rest'), env) 132 # env.log.debug("link is '%s'" % link) 124 133 yield indent, link, label 125 next_pos = match.end()126 134 127 135 128 136 def get_toc_entry_and_indent(gen): 129 """Generator, to be used as a filter for get_toc_entry(). Returns 130 link and label of the current toc entry and the indentation level 131 of the next entry or -1 if there are no more entries. The first 132 call to next() returns the indentation of the first entry.""" 133 137 """ 138 Filter for get_toc_entry(). Returns link and label of the current 139 toc entry and the indentation level of the next entry or -1 if 140 there are no more entries. The first call to next() returns the 141 indentation of the first entry. 142 """ 134 143 try: 135 144 indent, link, label = gen.next() … … 152 161 if next_indent > level: 153 162 sublist, next_indent = _parse_toc(gen, next_indent, level + 1) 154 if next_indent < level: 155 # level is empty 163 if next_indent < level: # level is empty 156 164 return sublist, next_indent 157 else: 158 # broken indentation structure 165 else: # broken indentation structure 159 166 toclist.append((None, None, sublist)) 160 167 while 1: … … 171 178 172 179 173 def parse_toc(toc_text ):174 gen = get_toc_entry_and_indent(get_toc_entry(toc_text ))180 def parse_toc(toc_text, env): 181 gen = get_toc_entry_and_indent(get_toc_entry(toc_text, env)) 175 182 toclist, _ = _parse_toc(gen, gen.next()) 176 183 return toclist … … 184 191 name = 'TOC' 185 192 186 toc = parse_toc(get_toc(hdf, env, curpage, name) )193 toc = parse_toc(get_toc(hdf, env, curpage, name), env) 187 194 if not toc: 188 195 msg = '<div class="system-message">' \ … … 255 262 else: 256 263 cls = '' 257 html += '%s<li%s%s>' % (indentation(col), li_style, cls) 258 if name == None: 259 html += title 260 else: 261 html += '<a href="%s">%s</a>' % (env.href.wiki(name), title) 262 html += '</li>\n' 264 html += '%s<li%s%s>%s</li>\n' % \ 265 (indentation(col), li_style, cls, title) 263 266 else: 264 267 html += '%s<li%s>\n' % (indentation(col), li_style) 265 268 col += 1 266 if name == None or len(sub) > 0: 267 html += '%s<h4>%s</h4>\n' % (indentation(col), title) 268 else: 269 html += '%s<h4><a href="%s">%s</a>...</h4>\n' % \ 270 (indentation(col), env.href.wiki(name), title) 269 if name == None or sub: 270 html += '%s<h4>%s</h4>\n' % \ 271 (indentation(col), title) 272 else: 273 html += '%s<h4>%s...</h4>\n' % \ 274 (indentation(col), title) 271 275 col -= 1 272 276 html += '%s</li>\n' % indentation(col)
Note: See TracChangeset
for help on using the changeset viewer.
