Skip to content

Commit 707ec9c

Browse files
committed
feat: new article
1 parent 3905b36 commit 707ec9c

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

markdown_file_processor.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22
import re
33

44
# List of stop words to remove from file names
5-
STOP_WORDS = {"and", "or", "the", "a", "an", "that", "this", "is", "in", "on", "with", "of", "for", "to"}
5+
STOP_WORDS = {
6+
"and",
7+
"or",
8+
"the",
9+
"a",
10+
"an",
11+
"that",
12+
"this",
13+
"is",
14+
"in",
15+
"on",
16+
"with",
17+
"of",
18+
"for",
19+
"to",
20+
}
621

722

823
def rename_markdown_file(file_path: str):
@@ -18,15 +33,15 @@ def rename_markdown_file(file_path: str):
1833
"""
1934
# Extract filename from the path
2035
filename = os.path.basename(file_path)
21-
36+
2237
# Split the filename to get the date and the name parts
23-
date_part, name_part = filename.split('-', 2)[:2], filename.split('-', 2)[2]
38+
date_part, name_part = filename.split("-", 2)[:2], filename.split("-", 2)[2]
2439
name_part = os.path.splitext(name_part)[0] # Remove the .md extension
2540

2641
# Split name into words, remove stop words, and replace spaces with underscores
2742
name_words = name_part.lower().split()
2843
filtered_name = [word for word in name_words if word not in STOP_WORDS]
29-
formatted_name = '_'.join(filtered_name)
44+
formatted_name = "_".join(filtered_name)
3045

3146
# Construct the new filename
3247
new_filename = f"{'-'.join(date_part)}-{formatted_name}.md"
@@ -37,9 +52,10 @@ def rename_markdown_file(file_path: str):
3752
print(f"Renamed '{filename}' to '{new_filename}'")
3853
return new_file_path
3954

55+
4056
def replace_latex_syntax_in_file(file_path: str):
4157
"""
42-
This function reads a markdown file, finds LaTeX delimiters and replaces them
58+
This function reads a markdown file, finds LaTeX delimiters and replaces them
4359
with double dollar signs for compatibility with a different LaTeX rendering system.
4460
4561
Args:
@@ -49,22 +65,23 @@ def replace_latex_syntax_in_file(file_path: str):
4965
None
5066
"""
5167
# Read the content of the file
52-
with open(file_path, 'r', encoding='utf-8') as file:
68+
with open(file_path, "r", encoding="utf-8") as file:
5369
content = file.read()
5470

5571
# Define the patterns to be replaced
56-
content = re.sub(r'\\\[', '$$', content) # Replaces \[ with $$
57-
content = re.sub(r'\\\]', '$$', content) # Replaces \] with $$
58-
content = re.sub(r'\\\(', '$$', content) # Replaces \( with $$
59-
content = re.sub(r'\\\)', '$$', content) # Replaces \) with $$
72+
content = re.sub(r"\\\[", "$$", content) # Replaces \[ with $$
73+
content = re.sub(r"\\\]", "$$", content) # Replaces \] with $$
74+
content = re.sub(r"\\\(", "$$", content) # Replaces \( with $$
75+
content = re.sub(r"\\\)", "$$", content) # Replaces \) with $$
6076

6177
# Write the updated content back to the file
62-
with open(file_path, 'w', encoding='utf-8') as file:
78+
with open(file_path, "w", encoding="utf-8") as file:
6379
file.write(content)
6480

81+
6582
def process_markdown_files_in_folder(folder_path: str):
6683
"""
67-
Processes all markdown files in a given folder, renaming them and
84+
Processes all markdown files in a given folder, renaming them and
6885
replacing LaTeX delimiters according to the replacement rules.
6986
7087
Args:
@@ -76,18 +93,18 @@ def process_markdown_files_in_folder(folder_path: str):
7693
# Iterate over all files in the folder
7794
for filename in os.listdir(folder_path):
7895
# Process only markdown files
79-
if filename.endswith('.md'):
96+
if filename.endswith(".md"):
8097
file_path = os.path.join(folder_path, filename)
81-
print(f'Processing file: {file_path}')
82-
98+
print(f"Processing file: {file_path}")
99+
83100
# Rename the file
84101
new_file_path = rename_markdown_file(file_path)
85-
102+
86103
# Replace LaTeX syntax in the renamed file
87104
replace_latex_syntax_in_file(new_file_path)
88-
89-
print(f'Finished processing file: {new_file_path}')
105+
106+
print(f"Finished processing file: {new_file_path}")
90107

91108

92-
folder_path = './_posts'
109+
folder_path = "./_posts"
93110
process_markdown_files_in_folder(folder_path)

0 commit comments

Comments
 (0)