Ticket #662: initial-migration-r1202.diff
| File initial-migration-r1202.diff, 14.0 kB (added by cboos, 2 years ago) |
|---|
-
a/doxygentrac/doxygentrac.py
old new 11 11 import re 12 12 import mimetypes 13 13 14 from trac.config import Option 14 15 from trac.core import * 15 16 from trac.web import IRequestHandler 16 17 from trac.perm import IPermissionRequestor 17 from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet 18 from trac.web.chrome import INavigationContributor, ITemplateProvider, \ 19 add_stylesheet 18 20 from trac.Search import ISearchSource 19 21 from trac.wiki import IWikiSyntaxProvider, wiki_to_html 20 22 from trac.wiki.formatter import system_message 21 from trac.util import Markup23 from trac.util.html import Markup, html 22 24 23 25 def compare_rank(x, y): 24 26 if x['rank'] == y['rank']: … … 31 33 implements(IPermissionRequestor, INavigationContributor, IRequestHandler, 32 34 ITemplateProvider, ISearchSource, IWikiSyntaxProvider) 33 35 36 base_path = Option('doxygen', 'path', '/var/lib/trac/doxygen', 37 """Directory containing doxygen generated files.""") 38 39 default_doc = Option('doxygen', 'default_documentation', '', 40 """Default path relative to `base_path` in which to look for 41 documentation files.""") 42 43 title = Option('doxygen', 'title', 'Doxygen', 44 """Title to use for the main navigation tab.""") 45 46 ext = Option('doxygen', 'ext', 'htm html png', 47 """Space separated list of extensions for doxygen managed files.""") 48 49 source_ext = Option('doxygen', 'source_ext', 50 'idl odl java cs py php php4 inc phtml m ' 51 'cpp cxx c hpp hxx h', 52 """Space separated list of source files extensions""") 53 54 index = Option('doxygen', 'index', 'main.html', 55 """Default index page to pick in the generated documentation.""") 56 57 wiki_index = Option('doxygen', 'wiki_index', None, 58 """Wiki page to use as the default page for the Doxygen main page.""") 59 60 encoding = Option('doxygen', 'encoding', 'iso-8859-1', 61 """Default encoding used by the generated documentation files.""") 62 34 63 # IPermissionRequestor methods 35 64 36 65 def get_permission_actions(self): … … 40 69 41 70 def get_active_navigation_item(self, req): 42 71 return 'doxygen' 72 43 73 def get_navigation_items(self, req): 44 74 if req.perm.has_permission('DOXYGEN_VIEW'): 45 # Get config variables.46 title = self.env.config.get('doxygen', 'title', 'Doxygen')47 48 75 # Return mainnav buttons. 49 yield 'mainnav', 'doxygen', Markup('<a href="%s">%s</a>' %\50 (self.env.href.doxygen() + '/', title))76 yield 'mainnav', 'doxygen', \ 77 html.a(self.title, href=req.href.doxygen()) 51 78 52 79 # IRequestHandler methods 53 80 54 81 def match_request(self, req): 55 # Get config variables. 56 base_path = self.config.get('doxygen', 'path', '/var/lib/trac/doxygen') 57 default_doc = self.config.get('doxygen', 'default_documentation', '') 58 ext = self.config.get('doxygen', 'ext', 'htm html png') 59 ext = '|'.join(ext.split(' ')) 60 source_ext = self.config.get('doxygen', 'source_ext', 'idl odl java' \ 61 ' cs py php php4 inc phtml m cpp cxx c hpp hxx h') 62 source_ext = '|'.join(source_ext.split(' ')) 82 ext = '|'.join(self.ext.split(' ')) 83 source_ext = '|'.join(self.source_ext.split(' ')) 63 84 64 85 # Match documentation request. 65 86 self.log.debug(req.path_info) 66 87 match = re.match('^/doxygen(?:/?$|/([^/]*)(?:/?$|/(.*)$))', 67 req.path_info)88 req.path_info) 68 89 if match: 69 90 self.log.debug('matched group 1: %s' % (match.group(1),)) 70 91 self.log.debug('matched group 2: %s' % (match.group(2),)) 71 92 72 93 if not match.group(1) and not match.group(2): 73 94 # Request for documentation index. 74 req.args['path'] = os.path.join(base_path, default_doc) 95 req.args['path'] = os.path.join(self.base_path, 96 self.default_doc) 75 97 req.args['action'] = 'index' 76 98 else: 77 99 # Get doc and file from request. 78 100 if not match.group(2): 79 doc = default_doc101 doc = self.default_doc 80 102 file = match.group(1) 81 103 else: 82 104 doc = match.group(1) … … 91 113 92 114 elif re.match(r'''^(.*)[.](%s)''' % (ext,), file): 93 115 # Request for documentation file. 94 path = os.path.join( base_path, doc, file)116 path = os.path.join(self.base_path, doc, file) 95 117 self.log.debug('path: %s' % (path,)) 96 118 if os.path.exists(path): 97 119 req.args['path'] = path … … 104 126 match = re.match(r'''^(.*)[.](%s)''' % (source_ext,), file) 105 127 if match: 106 128 # Request for source file documentation. 107 path = os.path.join( base_path, doc, '%s_8%s.html'129 path = os.path.join(self.base_path, doc, '%s_8%s.html' 108 130 % (match.group(1), match.group(2))) 109 131 self.log.debug('path: %s' % (path,)) 110 132 if os.path.exists(path): … … 115 137 req.args['query'] = file 116 138 117 139 else: 118 path = os.path.join( base_path, doc, 'class%s.html'140 path = os.path.join(self.base_path, doc, 'class%s.html' 119 141 % (file,)) 120 142 if os.path.exists(path): 121 143 req.args['path'] = path 122 144 req.args['action'] = 'file' 123 145 else: 124 path = os.path.join( base_path, doc,146 path = os.path.join(self.base_path, doc, 125 147 'struct%s.html' % (file,)) 126 148 if os.path.exists(path): 127 149 req.args['path'] = path … … 132 154 for result in results: 133 155 self.log.debug(result) 134 156 if result['name'] == file: 135 req.redirect( self.env.href.doxygen(doc)157 req.redirect(req.href.doxygen(doc) 136 158 + '/' + result['url']) 137 159 req.args['action'] = 'search' 138 160 req.args['query'] = file 139 140 161 # Request matched. 141 162 return True 142 163 … … 151 172 path = req.args.get('path') 152 173 action = req.args.get('action') 153 174 154 # Get config variables 155 index = self.config.get('doxygen', 'index', 'main.html') 156 wiki_index = self.config.get('doxygen', 'wiki_index', None) 175 print path, action 157 176 158 177 # Redirect search requests. 159 178 if action == 'search': 160 req.redirect( '%s?q=%s&doxygen=on' % (self.env.href.search(),161 req.args.get('query')))179 req.redirect(req.href.search(q=req.args.get('query'), 180 doxygen='on')) 162 181 163 182 # Retrun apropriate content to type or search request 164 183 elif action == 'index': 165 if wiki_index:184 if self.wiki_index: 166 185 # Get access to database 167 186 db = self.env.get_db_cnx() 168 187 cursor = db.cursor() 169 188 170 # Get wiki index 189 # Get wiki index # FIXME: use WikiPage() instead 171 190 sql = "SELECT text FROM wiki WHERE name = %s" 172 cursor.execute(sql, ( wiki_index,))173 text = Markup(system_message('Error', 'Wiki page %s does not' \174 ' exists' % (wiki_index)))191 cursor.execute(sql, (self.wiki_index,)) 192 text = system_message('Error', 'Wiki page %s does not exists' % 193 self.wiki_index) 175 194 for row in cursor: 176 195 text = wiki_to_html(row[0], self.env, req) 177 196 … … 180 199 return 'doxygen.cs', 'text/html' 181 200 else: 182 201 add_stylesheet(req, 'doxygen/css/doxygen.css') 183 req.hdf['doxygen.path'] = path + '/' + index202 req.hdf['doxygen.path'] = path + '/' + self.index 184 203 return 'doxygen.cs', 'text/html' 185 204 186 205 elif action == 'file': … … 207 226 208 227 def get_search_filters(self, req): 209 228 if req.perm.has_permission('DOXYGEN_VIEW'): 210 # Get config variables 211 title = self.env.config.get('doxygen', 'title', 'Doxygen') 212 213 yield('doxygen', title) 214 215 def get_search_results(self, req, query, filters): 229 yield('doxygen', self.title) 230 231 def get_search_results(self, req, keywords, filters): 216 232 if not 'doxygen' in filters: 217 233 return 218 if query[0] == query[-1] == "'" or query[0] == query[-1] == '"': 219 keywords = [query[1:-1]] 220 else: 221 keywords = query.split(' ') 222 223 base_path = self.config.get('doxygen', 'path') 224 225 for doc in os.listdir(base_path): 234 235 # We have to search for the raw bytes... 236 keywords = [k.encode(self.encoding) for k in keywords] 237 238 for doc in os.listdir(self.base_path): 226 239 # Search in documentation directories 227 path = os.path.join( base_path, doc)240 path = os.path.join(self.base_path, doc) 228 241 if os.path.isdir(path): 229 242 index = os.path.join(path, 'search.idx') 230 243 if os.path.exists(index): 231 244 creation = os.path.getctime(index) 232 245 for result in self._search_in_documentation(doc, keywords): 233 result['url'] = self.env.href.doxygen(doc) + '/' \246 result['url'] = req.href.doxygen(doc) + '/' \ 234 247 + result['url'] 235 248 yield result['url'], result['name'], creation, \ 236 249 'doxygen', None 237 250 238 251 # Search in common documentation directory 239 index = os.path.join( base_path, 'search.idx')252 index = os.path.join(self.base_path, 'search.idx') 240 253 if os.path.exists(index): 241 254 creation = os.path.getctime(index) 242 255 for result in self._search_in_documentation('', keywords): 243 result['url'] = self.env.href.doxygen() + '/' + \256 result['url'] = req.href.doxygen() + '/' + \ 244 257 result['url'] 245 258 yield result['url'], result['name'], creation, 'doxygen', \ 246 259 None … … 255 268 # internal methods 256 269 def _search_in_documentation(self, doc, keywords): 257 270 # Open index file for documentation 258 base_path = self.config.get('doxygen', 'path') 259 index = os.path.join(base_path, doc, 'search.idx') 271 index = os.path.join(self.base_path, doc, 'search.idx') 260 272 if os.path.exists(index): 261 273 fd = open(index) 262 274 … … 283 295 statIdx = self._readInt(fd) 284 296 low = word.lower() 285 297 if w.find(low) != -1: 286 matches.append({'word' : word, 'match' : w, 'index' : statIdx, 'full' : len(low) == len(w)}) 298 matches.append({'word': word, 'match': w, 299 'index': statIdx, 300 'full': len(low) == len(w)}) 287 301 w = self._readString(fd) 288 302 289 303 count = 0 … … 302 316 for i in range(numDocs): 303 317 idx = self._readInt(fd) 304 318 freq = self._readInt(fd) 305 results.append({'idx' : idx, 'freq' : freq >> 1, 'hi' : freq & 1, 'multi' : multiplier}) 319 results.append({'idx': idx, 'freq': freq >> 1, 320 'hi': freq & 1, 'multi': multiplier}) 306 321 if freq & 1: 307 322 totalHi += 1 308 323 totalFreqHi += freq * multiplier … … 322 337 freq = results[i]['freq'] 323 338 multi = results[i]['multi'] 324 339 if results[i]['hi']: 325 results[i]['rank'] = float((freq * multi + totalFreqLo)) / float(totalFreq) 340 results[i]['rank'] = float(freq*multi + totalFreqLo) \ 341 / float(totalFreq) 326 342 else: 327 results[i]['rank'] = float((freq * multi)) / float(totalFreq) 343 results[i]['rank'] = float(freq*multi) \ 344 / float(totalFreq) 328 345 329 346 return results 330 347 … … 360 377 361 378 def _doxygen_link(self, formatter, ns, params, label): 362 379 if ns == 'doxygen': 363 return '<a href="%s" title="%s">%s</a>' % \364 (self.env.href.doxygen(params), params, label)380 return html.a(label, href=formatter.href.doxygen(params), 381 title=params) 365 382 else: 366 return '<a href="%s" class="missing">%s?</a>' % \367 (self.env.href.doxygen(), label)383 return html.a(label, href=formatter.href.doxygen(), 384 title=params, class_='missing')
