def compact(html_tree):
    """Given an HTML tree, compact it.

    This involves:

    - finding all nodes with a single non-text child node
    - checking the pair (parent, child) in following lists,
      and it is in the list, replace the pair with the appropriate
      element of the two:

    Replace with parent:
      *, p

    Replace with child:
      hgroup, h*

    """
    for p in html_tree.xpath('//p'):
        parent = p.getparent()
        if not parent.text and parent.tag in ('li', 'dt', 'dd', 'td', 'th'):
            parent.text = p.text
            index = parent.index(p)
            for c in reversed(p):
                parent.insert(index, c)
            parent.remove(p)
    for header in html_tree.xpath('//header'):
        if len(header) == 0:
            header.getparent().remove(header)
