Changeset 3056
- Timestamp:
- 11/16/05 16:37:58 (8 years ago)
- File:
-
- 1 edited
-
trac/trunk/wiki-macros/TracNav.py (modified) (10 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
trac/trunk/wiki-macros/TracNav.py
- Property svn:keywords set to Id
r3054 r3056 68 68 """ 69 69 70 __revision__ = "$Id$" 71 70 72 import re 71 import sys72 73 from trac.wiki.api import WikiSystem 73 74 from trac.wiki.model import WikiPage 74 75 75 listRule = re.compile(r"""^(?P<indent> *)\* +(?:(?P<wikilink>\[wiki:(?P<link>("([^"]*)"|'([^']*)')|([^ \]]+)) +(?P<label>[^\]]*)\])|(?P<text>.*))""", re.M) 76 77 def getToc(hdf, env, curpage, name): 78 tocText = "* Table of contents" 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) 82 83 TRACNAVHOME = "http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav" 84 85 def get_toc(hdf, env, curpage, name): 86 """Fetch the wiki page containing the toc, if available.""" 87 88 toc_text = "* Table of contents" 79 89 80 90 preview = hdf.getValue('args.preview', "") 81 91 if preview and (name == curpage): 82 toc Text = hdf.getValue('wiki.page_source', tocText);92 toc_text = hdf.getValue('wiki.page_source', toc_text); 83 93 else: 84 94 if WikiSystem(env).has_page(name): 85 toc Text = WikiPage(env, name).text95 toc_text = WikiPage(env, name).text 86 96 87 97 # env.log.debug(tocText) 88 return tocText 89 90 91 def getTocEntry(tocText): 92 nextPos = 0 98 return toc_text 99 100 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 93 105 while 1: 94 match = listRule.search(tocText, nextPos)106 match = LISTRULE.search(toc_text, next_pos) 95 107 if not match: 96 108 # env.log.debug("No more matches") … … 111 123 112 124 yield indent, link, label 113 nextPos = match.end() 114 115 116 def getTocEntryAndNextIndent(g): 117 indent, link, label = g.next() 125 next_pos = match.end() 126 127 128 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 134 try: 135 indent, link, label = gen.next() 136 except StopIteration: 137 indent = -1 118 138 yield indent 119 139 … … 122 142 yield link, label 123 143 try: 124 indent, link, label = g .next()144 indent, link, label = gen.next() 125 145 except StopIteration: 126 146 indent, ready = -1, True … … 128 148 129 149 130 def _parse Toc(g, nextIndent, level = 0):131 list = []132 if next Indent > level:133 sub List, nextIndent = _parseToc(g, nextIndent, level + 1)134 if next Indent < level:150 def _parse_toc(gen, next_indent, level = 0): 151 toclist = [] 152 if next_indent > level: 153 sublist, next_indent = _parse_toc(gen, next_indent, level + 1) 154 if next_indent < level: 135 155 # level is empty 136 return sub List, nextIndent156 return sublist, next_indent 137 157 else: 138 158 # broken indentation structure 139 list.append((None, None, subList))159 toclist.append((None, None, sublist)) 140 160 while 1: 141 if next Indent == level:142 (link, label), next Indent = g.next(), g.next()143 if next Indent > level:144 sub List, nextIndent = _parseToc(g, nextIndent, level + 1)145 list.append((link, label, subList))146 else: 147 list.append((link, label, None))148 else: 149 assert next Indent < level150 return list, nextIndent151 152 153 def parse Toc(tocText):154 g = getTocEntryAndNextIndent(getTocEntry(tocText))155 list, _ = _parseToc(g, g.next())156 return list161 if next_indent == level: 162 (link, label), next_indent = gen.next(), gen.next() 163 if next_indent > level: 164 sublist, next_indent = _parse_toc(gen, next_indent, level + 1) 165 toclist.append((link, label, sublist)) 166 else: 167 toclist.append((link, label, None)) 168 else: 169 assert next_indent < level 170 return toclist, next_indent 171 172 173 def parse_toc(toc_text): 174 gen = get_toc_entry_and_indent(get_toc_entry(toc_text)) 175 toclist, _ = _parse_toc(gen, gen.next()) 176 return toclist 157 177 158 178 … … 164 184 name = 'TOC' 165 185 166 db = env.get_db_cnx() 167 toc = parseToc(getToc(hdf, env, curpage, name)) 186 toc = parse_toc(get_toc(hdf, env, curpage, name)) 168 187 if not toc: 169 msg = ' '170 msg += '<div class="system-message"><strong>Error: Table of contents does not exist.'188 msg = '<div class="system-message">' \ 189 "<strong>Error: Table of contents does not exist or is empty." 171 190 if (not preview) and (hdf.getValue('trac.acl.WIKI_MODIFY', '')): 172 msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % env.href.wiki(name) 191 msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % \ 192 env.href.wiki(name) 173 193 msg += '</strong></div>\n' 174 194 return msg 175 195 176 (found, filtered) = filter (curpage, toc, 0)196 (found, filtered) = filter_toc(curpage, toc, 0) 177 197 if found: 178 return display All(hdf, env, name, curpage, filtered, 0)198 return display_all(hdf, env, name, curpage, filtered, 0) 179 199 else: 180 return display All(hdf, env, name, curpage, toc, 0)181 182 183 def filter (curpage, toc, level):200 return display_all(hdf, env, name, curpage, toc, 0) 201 202 203 def filter_toc(curpage, toc, level): 184 204 found = 0 185 205 result = [] … … 190 210 result.append((name, title, None)) 191 211 else: 192 (subfound, subtoc) = filter (curpage, sub, level + 1)212 (subfound, subtoc) = filter_toc(curpage, sub, level + 1) 193 213 if subfound: 194 214 found = 1 … … 207 227 return ' ' * col 208 228 209 def display All(hdf, env, name, curpage, toc, col):229 def display_all(hdf, env, name, curpage, toc, col): 210 230 preview = hdf.getValue('args.preview', "") 211 html = '' 212 html += '%s<div class="wiki-toc trac-nav">\n' % indentation(col) 231 html = '%s<div class="wiki-toc trac-nav">\n' % indentation(col) 213 232 col += 1 214 215 html += '%s<h2><a href="http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav">TracNav</a> menu</h2>' % indentation(col)233 html += '%s<h2><a href="%s">TracNav</a> menu</h2>' % \ 234 (indentation(col), TRACNAVHOME) 216 235 217 236 if (not preview) and hdf.getValue('trac.acl.WIKI_MODIFY', ''): 218 html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % (indentation(col), env.href.wiki(name)) 237 html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % \ 238 (indentation(col), env.href.wiki(name)) 219 239 html += '%s<ul>\n' % indentation(col) 220 240 col += 1 … … 229 249 html = '' 230 250 for name, title, sub in toc: 231 li Style = ' style="padding-left: %dem;"' % (depth + 1)251 li_style = ' style="padding-left: %dem;"' % (depth + 1) 232 252 if sub == None: 233 253 if name == curpage: … … 235 255 else: 236 256 cls = '' 237 html += '%s<li%s%s>' % (indentation(col), li Style, cls)257 html += '%s<li%s%s>' % (indentation(col), li_style, cls) 238 258 if name == None: 239 259 html += title … … 242 262 html += '</li>\n' 243 263 else: 244 html += '%s<li%s>\n' % (indentation(col), li Style)264 html += '%s<li%s>\n' % (indentation(col), li_style) 245 265 col += 1 246 266 if name == None or len(sub) > 0: 247 267 html += '%s<h4>%s</h4>\n' % (indentation(col), title) 248 268 else: 249 html += '%s<h4><a href="%s">%s</a>...</h4>\n' % (indentation(col), env.href.wiki(name), title) 269 html += '%s<h4><a href="%s">%s</a>...</h4>\n' % \ 270 (indentation(col), env.href.wiki(name), title) 250 271 col -= 1 251 272 html += '%s</li>\n' % indentation(col)
Note: See TracChangeset
for help on using the changeset viewer.
