Builds a base document with the body as root.

    :param dom: Parsed lxml tree (Document Object Model).
    :param bool return_fragment: If True only <div> fragment is returned.
        Otherwise full HTML document is returned.

def build_base_document(dom, return_fragment=True):
    """
    Builds a base document with the body as root.

    :param dom: Parsed lxml tree (Document Object Model).
    :param bool return_fragment: If True only <div> fragment is returned.
        Otherwise full HTML document is returned.
    """
    body_element = dom.find(".//body")

    if body_element is None:
        fragment = fragment_fromstring('<div id="readabilityBody"/>')
        fragment.append(dom)
    else:
        body_element.tag = "div"
        body_element.set("id", "readabilityBody")
        fragment = body_element

    return document_from_fragment(fragment, return_fragment)