def get_chapters(top, type_filter=None, in_compile_only=True):
    """
    Returns a tuple `(chapters, count)`. `chapters` is a list of dicts with
    keys 'scrivID', 'scrivTitle', 'scrivType', 'IncludeInCompile' based on the
    'BinderItem' elements under `top`. If the respective element has children
    these will be captured in a list, mapped against 'children'.  If
    `type_filter` is not `None` only elements with 'Type' tag text equal to
    `type_filter` will be considered (children will be considered in any case).
    Similarly, if `in_compile_only` is `True` only items for which the
    'IncludeInCompile' value is 'Yes' will be considered. `count` is the total
    number of items in `chapters` across potential nestings.
    """
    def get_children(top, chapters):
        count = 0
        for e in top.iterfind('BinderItem'):
            rec = {}
            rec['scrivID'] = e.get('ID')
            rec['scrivType'] = e.get('Type')
            rec['scrivTitle'] = e.findtext('Title')
            in_compile = e.find('MetaData').findtext(
                    'IncludeInCompile')
            if ((type_filter is None or rec['scrivType'] == type_filter)
                    and (in_compile is None or in_compile.lower() == 'yes'
                         or not in_compile_only)):
                incl_item = True
                rec['children'] = []
                ccollect = rec['children']
            else:
                # we're not interested in the current item but want to pull
                # up its children one level
                incl_item = False
                ccollect = chapters
            children = e.find('Children')
            if children is not None:
                    count += get_children(children, ccollect)
            if incl_item:
                if len(rec['children']) == 0:
                    rec.pop('children', None)
                chapters.append(rec)
                count += 1
        return count

    children = top.find('Children')

    chapters = []
    count = 0
    if children is not None:
        count = get_children(top=children, chapters=chapters)

    return (chapters, count)