import re


def parse_details_page_notes(details_page_notes):
    """
    Clean up a details page notes section.

    The purpose of this function is to attempt to extract the sentences about
    the crash with some level of fidelity, but does not always return
    a perfectly parsed sentence as the HTML syntax varies widely.

    :param str details_description: the paragraph after the Deceased information
    :return: A paragraph containing the details of the fatality in sentence form.
    :rtype: str
    """
    # Ideally the Notes will be contained in a paragraph tag.
    start_tag = details_page_notes.find('<p>') + len('<p>')
    end_tag = details_page_notes.find('</p>', start_tag)

    # Here .upper().isupper() tests if the substring of the
    # text passed in contains any letters. If it doesn't,
    # the Notes may be located after a <br \>.
    if not details_page_notes[start_tag:end_tag].upper().isupper():
        start_tag = details_page_notes.find(r'<br \>') + len(r'<br \>')

    snippet = details_page_notes[start_tag:end_tag]

    # Update the snippet if the following tag is an image.
    if snippet[:4] == '<img':
        snippet = details_page_notes[details_page_notes.find(r'<br \>') + len(r'<br \>'):end_tag]

    # Remove the end of line characters.
    squished = snippet.replace('\n', ' ')

    # Look for the first capital letter and start from there.
    first_cap = 0
    for index, c in enumerate(squished):
        if c.isupper():
            first_cap = index
            break

    # Remove HTML tags.
    no_html = re.sub(re.compile('<.*?>'), '', squished[first_cap:])

    # Remove tabs and, if subjects included, remove.
    remove_subjects = re.split(r'\s{2,}', no_html)

    # Demographic info is usually only included in subject description.
    # DOB would be better, but that is sometimes missing.
    final = ' '.join([segment for segment in remove_subjects if 'male' not in segment])

    # This phrase signals the end of a report.
    footer_string = 'Fatality information may change.'
    end_pos = final.find(footer_string)

    if end_pos != -1:
        final = final[:end_pos + len(footer_string)]

    return final