Edgewall Software

MacroBazaar: SubWiki.4.py

File SubWiki.4.py, 3.9 KB (added by swl181 (a) gmail.com, 4 years ago)

SubWiki for trac 0.11b

Line 
1# Macros for the HierWiki plugin
2
3from trac.wiki.api import parse_args
4from trac.wiki.macros import WikiMacroBase
5from trac.wiki.model import WikiPage
6
7from genshi.builder import tag
8
9from StringIO import StringIO
10import re, string, inspect
11
12__all__ = ['AutoNavMacro']
13
14class SubWikiMacro(WikiMacroBase):
15    """
16    Inserts an alphabetic list of sub-wiki pages into the output.
17    A sub-wiki page is a page that is is deeper in the hierachy than the current page.  e.g. if the current page is People, the this will return a list of all wiki entries that start with "People/"
18   
19    Accepts a prefix string as parameter: if provided, only pages with names that
20    start with the prefix are included in the resulting list. If this parameter is
21    omitted, all pages are listed.
22   
23    This now takes the text of the first heading and displays it as the link name.
24    """
25
26    # TODO: Everything until render_macro can be removed once switched to be based on WikiMacroBase
27       
28    def expand_macro(self, formatter, name, content):
29        # Args seperated by commas:
30        # prefix,level
31        #
32        # Page Name prefix to search for.
33        # how many 'levels' in the hierarchy to go down.
34        #db = self.env.get_db_cnx()
35        args, kw = parse_args(content)
36
37        req = formatter.req
38        context = formatter.context
39        resource = context.resource
40
41        if not resource.realm == 'wiki':
42            raise TracError('\'SubWikiMacro\' macro can only be used in Wiki pages.')
43
44        cursor = formatter.db.cursor()
45        cs = formatter.db.cursor()
46
47        page = WikiPage(self.env, resource)
48        page_url = req.href.wiki(resource.id)
49        prefix = page.name + '/'
50
51
52        level = None
53        if args:
54            if args[0] != 'None':
55                prefix = args[0]
56            if len(args) > 1 and args[1] != 'None':
57                level = args[1]
58       
59        sql = 'SELECT DISTINCT name FROM wiki '
60        if prefix:
61            sql += 'WHERE name LIKE \'%s%%\' ' % prefix
62        sql += 'ORDER BY name'
63        cursor.execute(sql)
64
65        buf = StringIO()
66        buf.write('<ul>')
67
68        while 1:
69            row = cursor.fetchone()
70            if row == None:
71                break
72            name = row[0]
73            if level:
74                len_name = name.split('/')
75                #buf.write(len_name)
76                if len(len_name) > int(level)+1:
77                    continue
78            # Get the latest revision only.
79           
80            sql = 'SELECT name,text from wiki where name = \'%s\' order by version desc limit 1' % name
81            cs.execute(sql)
82            while 1:
83                csrow = cs.fetchone()
84                if csrow == None:
85                    break
86                name = csrow[0]
87                text = csrow[1]
88                (linktext,title,desc) = self._getinfo(formatter.db,name)
89
90                l = formatter.href.wiki(name)
91
92                buf.write('<li><a title="%s" href="%s">' % (title,l))
93                buf.write(linktext)
94                buf.write('</a> %s</li>\n' % desc)
95           
96        buf.write('</ul>')
97
98        return buf.getvalue()
99
100    def _getinfo(self, db, name):
101        cs = db.cursor()
102        desc = name
103        # Get the latest revision only.
104        cs.execute('SELECT text from wiki where name = \'%s\' order by version desc limit 1' % name)
105        csrow = cs.fetchone()
106        prefix = ''
107
108        if csrow != None:
109            text = csrow[0]
110            m  = re.search('=+\s([^=]*)=+',text)
111            if m != None:
112                desc = string.strip(m.group(1))
113        else:
114            prefix = "Create "
115
116        title = StringIO()
117        title.write("%s%s"%(prefix, desc))
118        if prefix != '' or desc == name:
119            desc = ''
120
121        return (name,title.getvalue(),desc)