From 7b8dc6aed51854f8f614bad3350109a2e6a965f7 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Tue, 2 Nov 2021 14:36:51 +0800 Subject: [PATCH] add update md code --- .gitignore | 1 + leetcode_helper.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 778cafa1..df302c7f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ auto_gen_leetcode_tem.py leetcode_helper.py lanqiao_helper.py lanqiao_template.md +test.html ./src/__pycache__/*.pyc \ No newline at end of file diff --git a/leetcode_helper.py b/leetcode_helper.py index 063afa0e..e4726eb3 100644 --- a/leetcode_helper.py +++ b/leetcode_helper.py @@ -1,4 +1,5 @@ import os +import re import shutil def leetcode_helper(): data_dir = 'data/3.算法高阶/1.leetcode' @@ -75,4 +76,32 @@ def leetcode_helper_delete_md(): if leetcode_solution_md_data == template: os.remove(solution_md_path) -leetcode_helper_delete_md() \ No newline at end of file +def leetcode_helper_update_md(): + data_dir = 'data/3.算法高阶/1.leetcode' + dirs_ = os.listdir(data_dir) + dirs = [] + for dir in dirs_: + dir = os.path.join(data_dir, dir) + if os.path.isdir(dir): + dirs.append(dir) + for dir in dirs: + assert os.path.isdir(dir) + exercises_id = dir.split('/')[-1].split('_')[0] + title = dir.split('/')[-1].split('_')[1] + if 0 <= int(exercises_id) and int(exercises_id) < 52: + solution_md_path = os.path.join(dir, 'solution.md') + desc_html_path = os.path.join(dir, 'desc.html') + with open(solution_md_path, 'r', encoding='utf-8') as f: + solution_md_data = f.read() + with open(desc_html_path, 'r', encoding='utf-8') as f: + desc_html_data = f.read() + content = re.findall('# .*?\n(.*?)\n## aop'.format(title), solution_md_data, re.DOTALL)[0] + new_content = desc_html_data + "\n

{}

".format(content) + # print(solution_md_path) + solution_md_data = solution_md_data.replace(content, new_content) + # print(solution_md_data) + with open(solution_md_path, 'w', encoding='utf-8') as f: + f.write(solution_md_data) + + +leetcode_helper_update_md() \ No newline at end of file -- GitLab