import re


def get_thesis_description(thesis):
    """
    The description exists in two parts: one that is shown and one
    that is hidden (must be expanded to see). The structure of the second
    part varies, and thus we must make the way of extraction generic.

    Return:
        The entire description of a given master thesis.
    """
    desc_first_part = thesis.find("p").text

    desc_second_part = ""
    # Not every thesis has a hidden description
    try:
        desc_second_part_raw = thesis.find_all("div")[1]
        # Remove the "Skul besrkivelse" anchors
        desc_second_part_raw = desc_second_part_raw.find_all()[:-2]
        desc_second_part = [tag.text.strip()
                            for tag in desc_second_part_raw]
        desc_second_part = [re.sub(r"[\n]", ". ", text)
                            for text in desc_second_part]
    except IndexError:
        pass

    desc = desc_first_part + " " + " ".join(desc_second_part)
    return desc