Edgewall Software

MacroBazaar: Include.2.py

File Include.2.py, 2.0 kB (added by csawyer@…, 4 years ago)

This incorporates WikiFile? and Include together, and doesn't require patching on 0.8

Line 
1"""
2We take an argument which is a file to go get.  The second argument is going to be None, or wiki at this point.
3
4do we include it, and then parse it thru the wiki formatter or not?
5
6url, can be a special case 'trunk' and it will grab from the local svn repository.
7
8a couple of examples:
9
10This will do no formatting on the url.
11[[Include(http://www.yuma.ca/tech/license.html,None)]]
12
13This will get the url, and do the Trac wiki formatting on it.
14[[Include(http://www.yuma.ca/tech/license.html,wiki)]]
15
16This will get the file out of the local SVN Repository(latest revision only).
17[[Include(trunk/needs_document/Employee/employee_file.txt,wiki)]]
18
19Theoretically you can modify this to handle more formatters, but I haven't the need so I stopped.
20
21If you include Arguments, you MUST include both.  Sorry I'm lazy.
22
23"""
24
25from trac import util
26from trac.WikiFormatter import wiki_to_html
27from StringIO import StringIO
28import os
29import urllib
30
31def execute(hdf, args, env):
32        # Args seperated by commas:
33        # url, formatter
34        #
35        # url is the url to go get.
36        # Formatter is which formatter if any to parse.  Default: None
37        _href = env.abs_href or env.href
38        formatter = None
39        url = None
40        db = env.get_db_cnx()
41        cursor = db.cursor()
42        cs = db.cursor()
43        buf = StringIO()
44
45        currentpage =  hdf.getValue('wiki.page_name', '') + '/'
46        if args:
47                args = args.replace('\'', '\'\'')
48        args = args.split(',')
49        if args[0] != 'None':
50                url = args[0]
51        if args[1] != 'None':
52                formatter = args[1]
53        if url[:5] == 'trunk':
54                # we need to get the file from the svn repo.
55                #http://financial.trac.yumaed.org/trac.cgi/file/trunk/needs_document/Employee/employee_file.txt?format=raw
56                url = 'http://' + os.getenv('HTTP_HOST') + env.href.base + '/file/' +  url + '?format=raw'
57        try:
58            f = urllib.urlopen(url)
59        except:
60            raise util.TracError('The "%s" argument doesnt seem to be a valid link' % (args))
61                        #buf.write('<P>bad url: ')
62                        #buf.write(url)
63        txt = f.read()
64        if formatter == 'wiki':
65                txt = wiki_to_html(txt,hdf,env,db,0)
66        buf.write(txt)
67        return buf.getvalue()