Skip to content

Commit

Permalink
script
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh4464 committed Jun 20, 2024
1 parent aeaa157 commit 5f30035
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions add_ctexart_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
# -----
# HISTORY:
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 5f30035

Please sign in to comment.