Edgewall Software

MacroBazaar: WikiInclude.py

File WikiInclude.py, 0.6 kB (added by anonymous, 3 years ago)
Line 
1from StringIO import StringIO
2from trac.WikiFormatter import *
3
4def execute(hdf, txt, env):
5        db = env.get_db_cnx()
6        formatter = Formatter(hdf, env, db)
7        out = StringIO()
8       
9        txt = txt or ''
10        args = txt.split('|')
11        name = args.pop(0).replace('\'', '\'\'')
12        sql = "SELECT text from wiki where name = '%s' order by version desc limit 1" % name
13        cs = db.cursor()
14        cs.execute(sql)
15       
16        row = cs.fetchone()
17        if row == None:
18                return ''
19        text = row[0]
20       
21        i = 0
22        for arg in args:
23                text = text.replace('{{%d}}' % (i+1), args[i])
24                i += 1
25       
26        formatter.format(text, out)
27       
28        return out.getvalue()