Modify

Opened 6 years ago

Last modified 5 years ago

#2861 new enhancement

SubversionFind macro

Reported by: anonymous Owned by: anybody
Priority: normal Component: Request-a-Hack
Severity: blocker Keywords:
Cc: Trac Release: 0.10

Description

To describe project architecture I had to do macros that seek the string in source subversion file and set reference to first finding string on wiki page.

for example, wiki page code:

[[SvnQuery(HelloWorld,.../_trunk/example.c, HelloWorld::HelloWorld)]]

result:
<a href="/trac/browser/.../_trunk/example.c#L480">HelloWorld::HelloWorld</a>

It's comfortable if the HelloWorld method moves or changes in the example.c code.

I wrote the macros:

# -*- coding: utf-8 -*-

import sys 
from trac.core import * 
from trac.wiki.macros import WikiMacroBase
from trac.wiki.macros import IWikiMacroProvider
#from trac.versioncontrol.api import NoSuchChangeset
from trac.versioncontrol.web_ui.util import *

import re
import urllib
import os.path
from fnmatch import fnmatchcase

from trac import util
from trac.config import ListOption, Option
#from trac.core import *
from trac.mimeview import Mimeview, is_binary, get_mimetype
from trac.perm import IPermissionRequestor
from trac.util import sorted, embedded_numbers
from trac.util.datefmt import http_date, format_datetime, pretty_timedelta
from trac.util.html import escape, html, Markup
from trac.util.text import pretty_size
from trac.web import IRequestHandler, RequestDone
from trac.web.chrome import add_link, add_stylesheet, INavigationContributor
from trac.wiki import wiki_to_html, IWikiSyntaxProvider
from trac.versioncontrol.api import NoSuchChangeset
from trac.versioncontrol.web_ui.util import *
from trac.web.href import Href

from urllib import quote, urlencode
from trac.util.text import unicode_quote, unicode_urlencode


from svn import core, fs, ra
import codecs
from StringIO import StringIO


CHUNK_SIZE = 4096

class SvnQuery(WikiMacroBase):

    implements(IPermissionRequestor)

    def get_permission_actions(self):
        return ['BROWSER_VIEW', 'FILE_VIEW']


    """ Usage: `[[SvnQuery]]` """
    def render_macro(self, formatter, name, content):
        if not content:
            return 'Not Argument for SvnQuery' 

        args = content.split(',')
        if len(args) == 0: 
            raise Exception("No argument.") 

#        print 'args:' + args[0].encode('utf-8')
        repos = self.env.get_repository(formatter.authname)

        node = get_existing_node(formatter, repos, args[1].encode('utf-8').strip(), repos.youngest_rev)


        formatter.perm.assert_permission('FILE_VIEW')

        content = node.get_content()
        import StringIO as StringIO
        self.strin = StringIO.StringIO('')   
        chunk = content.read(CHUNK_SIZE)

        res=-1
        i = 0
        self.str = ''
        while 1:
            if not chunk:
                n = self.find_line(args[2].encode('utf-8'), formatter)
                if n != -1:
                    return Markup(html.A(args[0].encode('utf-8'), href=formatter.href.browser(args[1].encode('utf-8').strip()) + '#L%d' % n
, style='padding:0; border:none') ).sanitize()
                return
            self.strin.write(chunk)
            chunk = content.read(CHUNK_SIZE)
        return ''
# return 'SvnQuery: ' + formatter.href.browser(content, format = 'raw')
    def find_line(self, str, fmt):
        res = -1
        i = 1
        self.strin.seek(0)
#        line = self.strin.readline()
        for line in self.strin.readlines():#while 1:
          if line.find(str) != -1:
              res = i
              return res
#line = self.strin.readline()
          i = i + 1

        return -1

Alexander Alyabushev alexhalf NOTDOG gmail.com

Attachments (1)

SvnQuery.py (3.0 KB) - added by anonymous 6 years ago.
macros SvnQuery

Download all attachments as: .zip

Change History (3)

comment:1 Changed 6 years ago by anonymous

  • Type changed from defect to enhancement

Changed 6 years ago by anonymous

macros SvnQuery

comment:2 Changed 5 years ago by rjollos

  • Summary changed from find in subversion to SubversionFind macro

Add Comment

Modify Ticket

Action
as new .
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.