def find_index_section_limits(filename, section_start=".. toctree::\n",
                              section_end="\n"):
    """Helper function for merge_indexes which loops through the
    file and marks the beginning and end of each section.

    Args:
        filename: path to file to be modified
        section_start: string which marks the start of each section
            Default: '.. toctree::\n'
        section_end: string which marks the end of each section
            Default: '\n'

    Returns:
        lines: list of strings of each line in the file
        section_limits: list of the form [[start1, end1], [start2, end2]]
            which marks the separation between sections
    """
    section_limits = []
    start = 0
    first_newline = True
    index_file = open(filename, "r+")
    lines = index_file.readlines()
    index_file.close()

    for i, line in enumerate(lines):
        if line == section_start:
            start = i
        if line == section_end and start != 0:
            if first_newline:
                first_newline = False
            else:
                end = i
                section_limits.append([start, end])
                start = end = 0
                first_newline = True

    return lines, section_limits