From 8a3bbbcc95c7d6d113b914ad8f3b61ac6e949db6 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Tue, 9 Nov 2021 18:24:50 +0800 Subject: [PATCH] fixed odd orders --- src/tree.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tree.py b/src/tree.py index 9e6a5cf..b770efc 100644 --- a/src/tree.py +++ b/src/tree.py @@ -119,7 +119,8 @@ class TreeWalker: level_path = os.path.join(self.root, level) num, config = self.load_level_node(level_path) levels.append((num, config)) - levels.sort(key=lambda item: item[0]) + + levels = self.resort_children(self.root, levels) root_node["children"] = [item[1] for item in levels] return root_node @@ -145,7 +146,7 @@ class TreeWalker: num, chapter = self.load_chapter_node(full_name) chapters.append((num, chapter)) - chapters.sort(key=lambda item: item[0]) + chapters = self.resort_children(base, chapters) level_node["children"] = [item[1] for item in chapters] return level_node @@ -157,10 +158,21 @@ class TreeWalker: num, section = self.load_section_node(full_name) sections.append((num, section)) - sections.sort(key=lambda item: item[0]) + sections = self.resort_children(base, sections) chapter_node["children"] = [item[1] for item in sections] return chapter_node + def resort_children(self, base, children): + children.sort(key=lambda item: item[0]) + for index, [number, element] in enumerate(children): + title = list(element.keys())[0] + origin = os.path.join(base, f"{number}.{title}") + posted = os.path.join(base, f"{index+1}.{title}") + if origin != posted: + self.logger.info(f"rename [{origin}] to [{posted}]") + os.rename(origin, posted) + return children + def ensure_chapters(self): for subdir in os.listdir(self.root): self.ensure_level_config(subdir) -- GitLab