"""
        IncludeUrlContentWiki macro for MoinMoin 1.3
        
        Get the content from an URL, process it as wiki markup, and return those
        formatted results. 
        
        I personally use this in cases where I have a web server
        process to dynamically generate content that relies on wiki
        features like macros, smileys, table layout rules, etc.
        
        Usage: [[IncludeUrlContentWiki( http://foo.com/wikimarkup )]]
        
        2005-03-31 Steve Poole, stevep@wrq.com        
        
"""

import urllib, StringIO
from MoinMoin.parser import wiki

def execute( macro, url ):

    try:
        out = StringIO.StringIO()
        macro.request.redirect( out )
        try:
            wikitext = urllib.urlopen( url ).read()
        except IOError:
            wikitext = "{{{ IncludeUrlContentWiki failed on " + url + " }}}"
        wikifier = wiki.Parser( wikitext, macro.request )
        wikifier.format( macro.formatter )
        result = out.getvalue()
    finally:
        macro.request.redirect()
        del out

    return result
