| 1 | """ |
|---|
| 2 | (C) 2005 Carlo C8E Miron; this program is free software, see http://gnu.org/licenses/gpl.html |
|---|
| 3 | $Id: MimeInclude.py 11 2005-04-13 14:15:00Z C8E $ |
|---|
| 4 | |
|---|
| 5 | = MimeInclude HTML Test = |
|---|
| 6 | |
|---|
| 7 | == No MIME type (text/plain) == |
|---|
| 8 | MimeInclude(http://www.python.org/~guido/) |
|---|
| 9 | ---- |
|---|
| 10 | [[MimeInclude(http://www.python.org/~guido/)]] |
|---|
| 11 | ---- |
|---|
| 12 | == HTML == |
|---|
| 13 | MimeInclude(http://www.python.org/~guido/, html) |
|---|
| 14 | ---- |
|---|
| 15 | [[MimeInclude(http://www.python.org/~guido/, html)]] |
|---|
| 16 | ---- |
|---|
| 17 | == HTML source == |
|---|
| 18 | MimeInclude(http://www.python.org/~guido/, text/html) |
|---|
| 19 | ---- |
|---|
| 20 | [[MimeInclude(http://www.python.org/~guido/, text/html)]] |
|---|
| 21 | ---- |
|---|
| 22 | |
|---|
| 23 | = MimeInclude WIKI Test = |
|---|
| 24 | |
|---|
| 25 | == No MIME type (text/plain) == |
|---|
| 26 | MimeInclude(http://projects.edgewall.com/trac/wiki/TracGuide?format=txt) |
|---|
| 27 | ---- |
|---|
| 28 | [[MimeInclude(http://projects.edgewall.com/trac/wiki/TracGuide?format=txt)]] |
|---|
| 29 | ---- |
|---|
| 30 | == Empty MIME type (wiki rendering) == |
|---|
| 31 | MimeInclude(http://projects.edgewall.com/trac/wiki/TracGuide?format=txt,) |
|---|
| 32 | ---- |
|---|
| 33 | [[MimeInclude(http://projects.edgewall.com/trac/wiki/TracGuide?format=txt,)]] |
|---|
| 34 | ---- |
|---|
| 35 | |
|---|
| 36 | = Include Python Test = |
|---|
| 37 | |
|---|
| 38 | == No MIME type (text/plain) == |
|---|
| 39 | MimeInclude(http://projects.edgewall.com/trac/file/trunk/trac/wikimacros/HelloWorld.py?format=txt) |
|---|
| 40 | ---- |
|---|
| 41 | [[MimeInclude(http://projects.edgewall.com/trac/file/trunk/trac/wikimacros/HelloWorld.py?format=txt)]] |
|---|
| 42 | ---- |
|---|
| 43 | == Python source == |
|---|
| 44 | MimeInclude(http://projects.edgewall.com/trac/file/trunk/trac/wikimacros/HelloWorld.py?format=txt, python) |
|---|
| 45 | ---- |
|---|
| 46 | [[MimeInclude(http://projects.edgewall.com/trac/file/trunk/trac/wikimacros/HelloWorld.py?format=txt, python)]] |
|---|
| 47 | --- |
|---|
| 48 | """ |
|---|
| 49 | from trac.util import TracError |
|---|
| 50 | from trac.WikiFormatter import wiki_to_html |
|---|
| 51 | from StringIO import StringIO |
|---|
| 52 | from urllib import urlopen |
|---|
| 53 | |
|---|
| 54 | def execute(hdf, text, env): |
|---|
| 55 | args = [x.strip() for x in (text or "").split(",")] |
|---|
| 56 | if len(args) == 1: |
|---|
| 57 | url, mime = args[0], "default" |
|---|
| 58 | elif len(args) == 2: |
|---|
| 59 | url, mime = args |
|---|
| 60 | else: |
|---|
| 61 | raise TracError("MimeInclude requires an URL and an optional mime type as arguments") |
|---|
| 62 | try: |
|---|
| 63 | stream = urlopen(url) |
|---|
| 64 | except IOError, err: |
|---|
| 65 | raise TracError('"%(url)s" is not a valid URL\nError: %(err)s' % vars()) |
|---|
| 66 | data = stream.read().strip() |
|---|
| 67 | if mime: |
|---|
| 68 | data = "{{{\n#!%(mime)s\n%(data)s\n}}}" % vars() |
|---|
| 69 | html = wiki_to_html(data, hdf, env, env.get_db_cnx()) |
|---|
| 70 | buffer = StringIO(html) |
|---|
| 71 | return buffer.getvalue() |
|---|