All pastes #1708840 Raw Edit

Someone

public text v1 · immutable
#1708840 ·published 2009-12-09 22:19 UTC
rendered paste body
### deliverance.xml
<proxy path="/">
  <dest href="http://www.example.com/foo"/>
  <response rewrite-links="1"/>
  <response pyref="my_custom_deliverance_xforms:strip_empty_blockquotes"/>
  <response pyref="my_custom_deliverance_xforms:reinstate_images"/>
</proxy>

### my_custom_deliverance_xforms/__init__.py
from lxml.html import document_fromstring, tostring
from lxml.etree import XMLSyntaxError

def strip_empty_blockquotes(request, response, *args):
    """ these empty blockquotes are nicely invisible in the source content,
    but the theme makes them visible and ugly. let's just get rid of them. """
    try:
        body = document_fromstring(response.body)
    except XMLSyntaxError:
        return

    to_remove = [el.drop_tree() for el in body.cssselect("blockquote")
                 if not el.text_content().replace('\r\n ', '').replace(' ', '')]
            
    response.body = tostring(body)
    
def reinstate_images(request, response,
                     deliverated_base_url,
                     proxied_base_url,
                     proxied_url,
                     log):
    """ images are hosted directly on the source; when their src hrefs from the content
    are swapped to use the deliverated_base_url, the img tags break. so let's just reverse
    that in postprocessing.

    not sure if this is a deliverance bug or not. """

    try:
        body = document_fromstring(response.body)
    except XMLSyntaxError:
        return

    for el in body.cssselect("img"):
        img_src = el.attrib['src']
        if img_src.startswith(deliverated_base_url):
            el.attrib['src'] = img_src.replace(
                deliverated_base_url, proxied_base_url)

    response.body = tostring(body)