| 1 |
|
|---|
| 2 |
""" |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
import re |
|---|
| 82 |
import sys |
|---|
| 83 |
|
|---|
| 84 |
listRule = re.compile(r"""^(?P<indent> *)\* +(?:(?P<wikilink>\[wiki:(?P<link>("([^"]*)"|'([^']*)')|([^ \]]+)) +(?P<label>[^\]]*)\])|(?P<text>.*))""", re.M) |
|---|
| 85 |
|
|---|
| 86 |
def getToc(hdf, env, db, curpage, name): |
|---|
| 87 |
preview = hdf.getValue('args.preview', "") |
|---|
| 88 |
|
|---|
| 89 |
tocText = "* Table of contents" |
|---|
| 90 |
if preview and (name == curpage): |
|---|
| 91 |
tocText = hdf.getValue('wiki.page_source', tocText); |
|---|
| 92 |
else: |
|---|
| 93 |
cursor = db.cursor() |
|---|
| 94 |
cursor.execute('SELECT text FROM wiki WHERE name=%s ORDER BY version DESC LIMIT 1', name) |
|---|
| 95 |
row = cursor.fetchone() |
|---|
| 96 |
if row: |
|---|
| 97 |
tocText = row[0] |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
return tocText |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
def parseToc(tocText): |
|---|
| 104 |
stack = [] |
|---|
| 105 |
nextPos = 0 |
|---|
| 106 |
while 1: |
|---|
| 107 |
match = listRule.search(tocText, nextPos) |
|---|
| 108 |
if not match: |
|---|
| 109 |
|
|---|
| 110 |
break |
|---|
| 111 |
|
|---|
| 112 |
indent = len(match.group('indent')) |
|---|
| 113 |
if match.group('wikilink'): |
|---|
| 114 |
link = match.group('link') |
|---|
| 115 |
label = match.group('label') |
|---|
| 116 |
else: |
|---|
| 117 |
link = None |
|---|
| 118 |
label = match.group('text') |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
if len(stack) == 0: |
|---|
| 126 |
stack.append((indent, [])) |
|---|
| 127 |
|
|---|
| 128 |
(lastIndent, list) = stack[len(stack) - 1] |
|---|
| 129 |
|
|---|
| 130 |
if indent > lastIndent: |
|---|
| 131 |
stack.append((indent, [(link, label, None, False)])) |
|---|
| 132 |
elif indent == lastIndent: |
|---|
| 133 |
list.append((link, label, None, False)) |
|---|
| 134 |
else: |
|---|
| 135 |
while indent < lastIndent: |
|---|
| 136 |
(_, list) = stack.pop() |
|---|
| 137 |
(lastIndent, topList) = stack[len(stack) - 1] |
|---|
| 138 |
(topLink, topLabel, _, __) = topList[len(topList) - 1] |
|---|
| 139 |
topList[len(topList) - 1] = (topLink, topLabel, list, False) |
|---|
| 140 |
|
|---|
| 141 |
(lastIndent, list) = stack[len(stack) - 1] |
|---|
| 142 |
list.append((link, label, None, False)) |
|---|
| 143 |
|
|---|
| 144 |
nextPos = match.end() |
|---|
| 145 |
|
|---|
| 146 |
while len(stack) > 1: |
|---|
| 147 |
(_, list) = stack.pop() |
|---|
| 148 |
(_, topList) = stack[len(stack) - 1] |
|---|
| 149 |
(topLink, topLabel, _, __) = topList[len(topList) - 1] |
|---|
| 150 |
topList[len(topList) - 1] = (topLink, topLabel, list, False) |
|---|
| 151 |
|
|---|
| 152 |
(_, list) = stack.pop() |
|---|
| 153 |
return list |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
def execute(hdf, args, env): |
|---|
| 157 |
preview = hdf.getValue('args.preview', "") |
|---|
| 158 |
curpage = hdf.getValue('args.page', "") |
|---|
| 159 |
name = args |
|---|
| 160 |
if not name: |
|---|
| 161 |
name = 'TOC' |
|---|
| 162 |
|
|---|
| 163 |
db = env.get_db_cnx() |
|---|
| 164 |
toc = parseToc(getToc(hdf, env, db, curpage, name)) |
|---|
| 165 |
if not toc: |
|---|
| 166 |
msg = '' |
|---|
| 167 |
msg += '<div class="system-message"><strong>Error: Table of contents does not exist.' |
|---|
| 168 |
if (not preview) and (hdf.getValue('trac.acl.WIKI_MODIFY', '')): |
|---|
| 169 |
msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % env.href.wiki(name) |
|---|
| 170 |
msg += '</strong></div>\n' |
|---|
| 171 |
return msg |
|---|
| 172 |
|
|---|
| 173 |
(found, filtered) = filter(curpage, toc, 0) |
|---|
| 174 |
if found: |
|---|
| 175 |
return displayAll(hdf, env, name, curpage, filtered, 0) |
|---|
| 176 |
else: |
|---|
| 177 |
return displayAll(hdf, env, name, curpage, toc, 0) |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
def filter(curpage, toc, level): |
|---|
| 181 |
found = 0 |
|---|
| 182 |
result = [] |
|---|
| 183 |
for name, title, sub, hidden in toc: |
|---|
| 184 |
if sub == None: |
|---|
| 185 |
if name == curpage: |
|---|
| 186 |
found = 1 |
|---|
| 187 |
result.append((name, title, None, hidden)) |
|---|
| 188 |
else: |
|---|
| 189 |
if name == curpage: |
|---|
| 190 |
found = 1 |
|---|
| 191 |
(subfound, subtoc) = filter(curpage, sub, level + 1) |
|---|
| 192 |
if subfound or (name == None): |
|---|
| 193 |
found = 1 |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
result.append((name, title, subtoc, hidden)) |
|---|
| 201 |
else: |
|---|
| 202 |
if level == 0 and name != curpage: |
|---|
| 203 |
hidden = True |
|---|
| 204 |
result.append((name, title, subtoc, hidden)) |
|---|
| 205 |
|
|---|
| 206 |
return (found, result) |
|---|
| 207 |
|
|---|
| 208 |
def indentation(col): |
|---|
| 209 |
return ' ' * col |
|---|
| 210 |
|
|---|
| 211 |
def displayAll(hdf, env, name, curpage, toc, col): |
|---|
| 212 |
preview = hdf.getValue('args.preview', "") |
|---|
| 213 |
html = '' |
|---|
| 214 |
html += '%s<div id="PropelSiteNavigation" class="wiki-toc trac-nav">\n' % indentation(col) |
|---|
| 215 |
col += 1 |
|---|
| 216 |
if (not preview) and hdf.getValue('trac.acl.WIKI_MODIFY', ''): |
|---|
| 217 |
html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % (indentation(col), env.href.wiki(name)) |
|---|
| 218 |
html += '%s<h3>Website Navigation</h3>\n' % indentation(col) |
|---|
| 219 |
col += 1 |
|---|
| 220 |
html += display(env, curpage, toc, 0, col) |
|---|
| 221 |
col -= 1 |
|---|
| 222 |
col -= 1 |
|---|
| 223 |
html += '%s</div>\n' % indentation(col) |
|---|
| 224 |
return html |
|---|
| 225 |
|
|---|
| 226 |
def display(env, curpage, toc, depth, col): |
|---|
| 227 |
html = '' |
|---|
| 228 |
html+= '<ul>\n'; |
|---|
| 229 |
for name, title, sub, hidden in toc: |
|---|
| 230 |
if name == curpage: |
|---|
| 231 |
cls = ' class="active"' |
|---|
| 232 |
else: |
|---|
| 233 |
cls = '' |
|---|
| 234 |
if sub == None: |
|---|
| 235 |
html += '%s<li%s>' % (indentation(col), cls) |
|---|
| 236 |
if name == None: |
|---|
| 237 |
html += title |
|---|
| 238 |
else: |
|---|
| 239 |
html += '<a href="%s">%s</a>' % (env.href.wiki(name), title) |
|---|
| 240 |
html += '</li>\n' |
|---|
| 241 |
else: |
|---|
| 242 |
if hidden: |
|---|
| 243 |
hide = ' class="hidden"' |
|---|
| 244 |
else: |
|---|
| 245 |
hide = '' |
|---|
| 246 |
html += '%s<li%s%s>' % (indentation(col), hide, cls) |
|---|
| 247 |
col += 1 |
|---|
| 248 |
|
|---|
| 249 |
if name == None: |
|---|
| 250 |
html += '%s%s\n' % (indentation(col), title) |
|---|
| 251 |
else: |
|---|
| 252 |
html += '%s<a href="%s">%s...</a>\n' % (indentation(col), env.href.wiki(name), title) |
|---|
| 253 |
col -= 1 |
|---|
| 254 |
if len(sub) > 0: |
|---|
| 255 |
html += display(env, curpage, sub, depth + 1, col) |
|---|
| 256 |
html += '%s</li>\n' % indentation(col) |
|---|
| 257 |
html+= '</ul>\n' |
|---|
| 258 |
return html |
|---|
| 259 |
|
|---|