From 81fe7cd47bbe041978dd5c6cc29c128405246408 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 3 Feb 2020 23:11:22 -0800 Subject: [PATCH] Use enumerate instead of manual counting, now that headers is exact. --- epubfile.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/epubfile.py b/epubfile.py index 5401d24..23c968f 100644 --- a/epubfile.py +++ b/epubfile.py @@ -1027,7 +1027,6 @@ class Epub: toc = new_list(root=True) current_level = None current_list = toc.ol - toc_line_index = 1 spine = self.get_spine_order(linear_only=linear_only) spine = [s for s in spine if s != nav_id] @@ -1036,12 +1035,12 @@ class Epub: file_path = self.get_filepath(file_id) soup = self.read_file(file_id, soup=True) - for header in soup.find_all(header_pattern): + headers = soup.find_all(header_pattern) + for (toc_line_index, header) in enumerate(headers, start=1): # 'hX' -> X level = int(header.name[1]) header['id'] = f'toc_{toc_line_index}' - toc_line_index += 1 toc_line = toc.new_tag('li') toc_line['text'] = header.text