'Post-processing of html content to replace local links to absolute
links, using web.base.url as base url.'

@api.model
def _replace_local_links(self, html):

    if (not html):
        return html
    root = lxml.html.fromstring(html)
    if ((not len(root)) and (root.text is None) and (root.tail is None)):
        html = ('<div>%s</div>' % html)
        root = lxml.html.fromstring(html)
    base_url = self.env['ir.config_parameter'].get_param('web.base.url')
    (base_scheme, base_netloc, bpath, bparams, bquery, bfragment) = urlparse.urlparse(base_url)
    def _process_link(url):
        new_url = url
        (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
        if ((not scheme) and (not netloc)):
            new_url = urlparse.urlunparse((base_scheme, base_netloc, path, params, query, fragment))
        return new_url
    for node in root.iter():
        if ((node.tag == 'a') and node.get('href')):
            node.set('href', _process_link(node.get('href')))
        elif ((node.tag == 'img') and (not node.get('src', 'data').startswith('data'))):
            node.set('src', _process_link(node.get('src')))
    html = lxml.html.tostring(root, pretty_print=False, method='html')
    if (html.startswith('<div>') and html.endswith('</div>')):
        html = html[5:(-6)]
    return html
