import trac.util
import libxml2, libxslt

def execute(hdf, text, env):
    stylesheet = env.get_config('docbook', 'stylesheet')
    if not stylesheet:
        raise trac.util.TracError('DocBook stylesheet not configured.')
    else:
        style = libxslt.parseStylesheetDoc(libxml2.parseFile(stylesheet))
        doc = libxml2.parseDoc(text)
        result = style.applyStylesheet(doc, None)
        html = style.saveResultToString(result)
        style.freeStylesheet()
        doc.freeDoc()
        result.freeDoc()
        return html[html.find('<body>')+6:html.find('</body>')].strip()
