
from trac.WikiFormatter import wiki_to_html, WikiProcessor
from trac import util
import os


def execute(hdf, file, env):
    # Currently hdf is set only when the macro is called
    # From a wiki page
    args = file or ''

    if args:
        import urllib
        try:
            f = urllib.urlopen(args)
        except:
            raise util.TracError('The "%s" argument doesnt seem to be a valid link' % (args))
        txt =  f.read()
        (path, suffix) = os.path.splitext(args)
        suffix = suffix[1:]
        try:
            Format = WikiProcessor(env,suffix)
            html = Format.process(hdf, txt)        
            return html
        except:
            raise util.TracError('The "%s" argument doesnt seem to be a valid link' % (suffix))
            return wiki_to_html(txt, hdf, env, env.get_db_cnx)
    else:
        raise util.TracError('The include macro requires an url as argument')


