From 4d4f7570e00960bfb6b47b8c7d3b736c58fae750 Mon Sep 17 00:00:00 2001 From: Hengyu Ai Date: Thu, 4 Jul 2024 02:13:24 +0800 Subject: [PATCH] feat: list guides by time --- gen_index.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gen_index.py b/gen_index.py index 58a97dc..3f909a2 100644 --- a/gen_index.py +++ b/gen_index.py @@ -62,8 +62,19 @@ def parse(tokens): def get_markdown_files(directory): """Get a list of all markdown files in the specified directory.""" if __debug__: - return [f for f in os.listdir(directory) if f.endswith('.md')] - return [f for f in os.listdir(directory) if f.endswith('.md') and f != 'guide_template.md'] + file_names = [f for f in os.listdir(directory) if f.endswith('.md')] + else: + file_names = [f for f in os.listdir(directory) if f.endswith('.md') and f != 'guide_template.md'] + # sort by time in discription + names_with_time = [] + for file in file_names: + with open(directory + file, 'r', encoding='utf-8') as f: + content = f.read() + time = re.search(r'date: (.+)', content).group(1) + names_with_time.append((file, time)) + names_with_time.sort(key=lambda x: x[1]) + file_names = [name for name, _ in names_with_time] + return file_names def match_guide_info(content):