From 5f3003513092ea578642913d0ca4781ac76318c0 Mon Sep 17 00:00:00 2001 From: Zihan <32484940+wzh4464@users.noreply.github.com> Date: Fri, 21 Jun 2024 00:24:42 +0800 Subject: [PATCH] script --- add_ctexart_comment.py | 47 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/add_ctexart_comment.py b/add_ctexart_comment.py index 60c895f..632594a 100644 --- a/add_ctexart_comment.py +++ b/add_ctexart_comment.py @@ -6,7 +6,7 @@ # Created Date: Monday, June 17th 2024 # Author: Zihan # ----- - # Last Modified: Monday, 17th June 2024 7:59:27 pm + # Last Modified: Friday, 21st June 2024 12:24:20 am # Modified By: the developer formerly known as Zihan at # ----- # HISTORY: @@ -18,19 +18,34 @@ md_files = glob.glob('**/*.md', recursive=True) # Comment to be added -# comment = '''--- -# toc: true -# documentclass: "ctexart" -# classoption: "UTF8" -# --- -# ''' +comment = '''--- +toc: true +documentclass: "ctexart" +classoption: "UTF8" +--- +''' + +comment_2 = '''--- +toc: true +documentclass: "ctexart" +classoption: "UTF8" +''' # Add the comment to each file -# for file in md_files: -# with open(file, 'r+') as f: -# content = f.read() -# f.seek(0, 0) -# f.write(comment + content) +for file in md_files: + with open(file, 'r+') as f: + content = f.read() + # if already has ctexart comment, then skip + if 'documentclass: "ctexart"' in content: + continue + f.seek(0, 0) + # f.write(comment + content) + # if the first line is '---', then remove the first line and add the comment_2 + if content.startswith('---'): + content = content[content.find('\n') + 1:] # remove the first line + f.write(comment_2 + content) + else: + f.write(comment + content) # delete first blank lines # for file in md_files: @@ -43,10 +58,10 @@ # f.truncate() # substitute all space in file name with '_' -for file in md_files: - new_file = file.replace(' ', '_') - print(f'Renaming {file} to {new_file}') - os.rename(file, new_file) +# for file in md_files: +# new_file = file.replace(' ', '_') +# print(f'Renaming {file} to {new_file}') +# os.rename(file, new_file) # also for foler name # folders = glob.glob('**/', recursive=True)