Edgewall Software

MacroBazaar: WikiInclude.0.11.py

File WikiInclude.0.11.py, 0.8 kB (added by chelah@…, 9 months ago)

Converted for Trac 0.11

Line 
1from StringIO import StringIO
2from trac.util.html import Markup
3from trac.wiki.formatter import Formatter
4from trac.wiki.macros import WikiMacroBase
5
6class WikiIncludeMacro(WikiMacroBase):
7        def expand_macro(self, formatter, name, args):
8               
9                args = args.split('|')
10               
11                if len(args) < 1 :
12                        return '';
13                       
14                target = args[0]
15                db = self.env.get_db_cnx()
16               
17                sql = "SELECT text from wiki where name = '%s' order by version desc limit 1" % target
18                cs = db.cursor()
19                cs.execute(sql)
20               
21                row = cs.fetchone()
22               
23                if row == None:
24                        return ''
25               
26                text = row[0]
27               
28                if len(args) > 1 :
29                        i = 0
30                        for arg in args:
31                                if i > 0 :
32                                        text = text.replace('{{%d}}' % (i-1), unicode(args[i]))
33                                i += 1
34               
35                out = StringIO()
36                Formatter(formatter.env, formatter.context).format(text, out)
37                return Markup(out.getvalue())