Ticket #662: initial-migration-r1202.diff

File initial-migration-r1202.diff, 14.0 kB (added by cboos, 2 years ago)

That's a start... Only notable change was the need to specify a new [doxygen] encoding setting, so that we know what are the bytes to look for in the index file.

  • a/doxygentrac/doxygentrac.py

    old new  
    1111import re 
    1212import mimetypes 
    1313 
     14from trac.config import Option 
    1415from trac.core import * 
    1516from trac.web import IRequestHandler 
    1617from trac.perm import IPermissionRequestor 
    17 from trac.web.chrome import INavigationContributor, ITemplateProvider, add_stylesheet 
     18from trac.web.chrome import INavigationContributor, ITemplateProvider, \ 
     19                            add_stylesheet 
    1820from trac.Search import ISearchSource 
    1921from trac.wiki import IWikiSyntaxProvider, wiki_to_html 
    2022from trac.wiki.formatter import system_message 
    21 from trac.util import Markup 
     23from trac.util.html import Markup, html 
    2224 
    2325def compare_rank(x, y): 
    2426    if x['rank'] == y['rank']: 
     
    3133    implements(IPermissionRequestor, INavigationContributor, IRequestHandler, 
    3234      ITemplateProvider, ISearchSource, IWikiSyntaxProvider) 
    3335 
     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 
    3463    # IPermissionRequestor methods 
    3564 
    3665    def get_permission_actions(self): 
     
    4069 
    4170    def get_active_navigation_item(self, req): 
    4271        return 'doxygen' 
     72 
    4373    def get_navigation_items(self, req): 
    4474        if req.perm.has_permission('DOXYGEN_VIEW'): 
    45             # Get config variables. 
    46             title = self.env.config.get('doxygen', 'title', 'Doxygen') 
    47  
    4875            # 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()) 
    5178 
    5279    # IRequestHandler methods 
    5380 
    5481    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(' ')) 
    6384 
    6485        # Match documentation request. 
    6586        self.log.debug(req.path_info) 
    6687        match = re.match('^/doxygen(?:/?$|/([^/]*)(?:/?$|/(.*)$))', 
    67           req.path_info) 
     88                         req.path_info) 
    6889        if match: 
    6990            self.log.debug('matched group 1: %s' % (match.group(1),)) 
    7091            self.log.debug('matched group 2: %s' % (match.group(2),)) 
    7192 
    7293            if not match.group(1) and not match.group(2): 
    7394                # 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) 
    7597                req.args['action'] = 'index' 
    7698            else: 
    7799                # Get doc and file from request. 
    78100                if not match.group(2): 
    79                     doc = default_doc 
     101                    doc = self.default_doc 
    80102                    file = match.group(1) 
    81103                else: 
    82104                    doc = match.group(1) 
     
    91113 
    92114                elif re.match(r'''^(.*)[.](%s)''' % (ext,), file): 
    93115                    # Request for documentation file. 
    94                     path = os.path.join(base_path, doc, file) 
     116                    path = os.path.join(self.base_path, doc, file) 
    95117                    self.log.debug('path: %s' % (path,)) 
    96118                    if os.path.exists(path): 
    97119                        req.args['path'] = path 
     
    104126                    match = re.match(r'''^(.*)[.](%s)''' % (source_ext,), file) 
    105127                    if match: 
    106128                        # 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' 
    108130                          % (match.group(1), match.group(2))) 
    109131                        self.log.debug('path: %s' % (path,)) 
    110132                        if os.path.exists(path): 
     
    115137                            req.args['query'] = file 
    116138 
    117139                    else: 
    118                         path = os.path.join(base_path, doc, 'class%s.html' 
     140                        path = os.path.join(self.base_path, doc, 'class%s.html' 
    119141                          % (file,)) 
    120142                        if os.path.exists(path): 
    121143                            req.args['path'] = path 
    122144                            req.args['action'] = 'file' 
    123145                        else: 
    124                             path = os.path.join(base_path, doc, 
     146                            path = os.path.join(self.base_path, doc, 
    125147                              'struct%s.html' % (file,)) 
    126148                            if os.path.exists(path): 
    127149                                req.args['path'] = path 
     
    132154                                for result in results: 
    133155                                    self.log.debug(result) 
    134156                                    if result['name'] == file: 
    135                                         req.redirect(self.env.href.doxygen(doc) 
     157                                        req.redirect(req.href.doxygen(doc) 
    136158                                          + '/' + result['url']) 
    137159                                req.args['action'] = 'search' 
    138160                                req.args['query'] = file 
    139  
    140161            # Request matched. 
    141162            return True 
    142163 
     
    151172        path = req.args.get('path') 
    152173        action = req.args.get('action') 
    153174 
    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 
    157176 
    158177        # Redirect search requests. 
    159178        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')) 
    162181 
    163182        # Retrun apropriate content to type or search request 
    164183        elif action == 'index': 
    165             if wiki_index: 
     184            if self.wiki_index: 
    166185                # Get access to database 
    167186                db = self.env.get_db_cnx() 
    168187                cursor = db.cursor() 
    169188 
    170                 # Get wiki index 
     189                # Get wiki index  # FIXME: use WikiPage() instead 
    171190                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
    175194                for row in cursor: 
    176195                    text = wiki_to_html(row[0], self.env, req) 
    177196 
     
    180199                return 'doxygen.cs', 'text/html' 
    181200            else: 
    182201                add_stylesheet(req, 'doxygen/css/doxygen.css') 
    183                 req.hdf['doxygen.path'] = path + '/' + index 
     202                req.hdf['doxygen.path'] = path + '/' + self.index 
    184203                return 'doxygen.cs', 'text/html' 
    185204 
    186205        elif action == 'file': 
     
    207226 
    208227    def get_search_filters(self, req): 
    209228        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): 
    216232        if not 'doxygen' in filters: 
    217233            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): 
    226239            # Search in documentation directories 
    227             path = os.path.join(base_path, doc) 
     240            path = os.path.join(self.base_path, doc) 
    228241            if os.path.isdir(path): 
    229242                index = os.path.join(path, 'search.idx') 
    230243                if os.path.exists(index): 
    231244                    creation = os.path.getctime(index) 
    232245                    for result in  self._search_in_documentation(doc, keywords): 
    233                         result['url'] =  self.env.href.doxygen(doc) + '/' \ 
     246                        result['url'] =  req.href.doxygen(doc) + '/' \ 
    234247                          + result['url'] 
    235248                        yield result['url'], result['name'], creation, \ 
    236249                          'doxygen', None 
    237250 
    238251            # Search in common documentation directory 
    239             index = os.path.join(base_path, 'search.idx') 
     252            index = os.path.join(self.base_path, 'search.idx') 
    240253            if os.path.exists(index): 
    241254                creation = os.path.getctime(index) 
    242255                for result in self._search_in_documentation('', keywords): 
    243                     result['url'] =  self.env.href.doxygen() + '/' + \ 
     256                    result['url'] =  req.href.doxygen() + '/' + \ 
    244257                      result['url'] 
    245258                    yield result['url'], result['name'], creation, 'doxygen', \ 
    246259                      None 
     
    255268    # internal methods 
    256269    def _search_in_documentation(self, doc, keywords): 
    257270        # 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') 
    260272        if os.path.exists(index): 
    261273            fd = open(index) 
    262274 
     
    283295                    statIdx = self._readInt(fd) 
    284296                    low = word.lower() 
    285297                    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)}) 
    287301                    w = self._readString(fd) 
    288302 
    289303                count = 0 
     
    302316                    for i in range(numDocs): 
    303317                        idx = self._readInt(fd) 
    304318                        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}) 
    306321                        if freq & 1: 
    307322                            totalHi += 1 
    308323                            totalFreqHi += freq * multiplier 
     
    322337                    freq = results[i]['freq'] 
    323338                    multi = results[i]['multi'] 
    324339                    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) 
    326342                    else: 
    327                         results[i]['rank'] = float((freq * multi)) / float(totalFreq) 
     343                        results[i]['rank'] = float(freq*multi) \ 
     344                                             / float(totalFreq) 
    328345 
    329346        return results 
    330347 
     
    360377 
    361378    def _doxygen_link(self, formatter, ns, params, label): 
    362379        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
    365382        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'