Skip to content

Commit 62ba55b

Browse files
committed
Add argparse support to utilities
1 parent 58ba601 commit 62ba55b

13 files changed

+87
-45
lines changed

capitalized_keywords.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22
import yaml
33
import re
4+
import argparse
45

5-
# Define the folder where the markdown files are stored
6-
folder_path = './_posts' # Change this to your folder path
6+
# Default folder containing the markdown files
7+
DEFAULT_FOLDER = "./_posts"
78

89
# List of stop words to exclude from capitalization
910
stop_words = {'at', 'vs', 'and', 'or', 'the', 'of', 'in', 'on', 'for', 'to', 'a'}
@@ -71,11 +72,14 @@ def process_markdown_file(file_path):
7172
print(f"No 'keywords' found in {file_path}")
7273

7374
# Function to process all markdown files in the folder
74-
def process_all_markdown_files(folder_path):
75+
def process_all_markdown_files(folder_path: str) -> None:
7576
for filename in os.listdir(folder_path):
7677
if filename.endswith(".md"): # Check if it's a markdown file
7778
file_path = os.path.join(folder_path, filename)
7879
process_markdown_file(file_path)
7980

80-
# Run the function for the specified folder
81-
process_all_markdown_files(folder_path)
81+
if __name__ == "__main__":
82+
parser = argparse.ArgumentParser(description="Capitalize keywords in markdown files")
83+
parser.add_argument("--path", default=DEFAULT_FOLDER, help="Target folder")
84+
args = parser.parse_args()
85+
process_all_markdown_files(args.path)

check_summary.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import yaml # to parse YAML front matter
3+
import argparse
34

45
def extract_front_matter(md_file_path: str) -> dict:
56
"""
@@ -52,7 +53,9 @@ def check_front_matter(folder_path: str, output_file: str):
5253
out_file.write(f" - Keywords present: {has_keywords}\n")
5354
out_file.write("\n")
5455

55-
# Example usage
56-
folder_path = './_posts' # Replace with the actual folder path
57-
output_file = "front_matter_report.txt" # Replace with the desired output file path
58-
check_front_matter(folder_path, output_file)
56+
if __name__ == "__main__":
57+
parser = argparse.ArgumentParser(description="Check markdown files for summary and keywords")
58+
parser.add_argument("--path", default="./_posts", help="Target folder")
59+
args = parser.parse_args()
60+
output_file = "front_matter_report.txt"
61+
check_front_matter(args.path, output_file)

extract_front_matter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import yaml
2+
import argparse
23

34
def extract_and_print_front_matter(folder: str, file_name: str):
45
"""
@@ -31,6 +32,9 @@ def extract_and_print_front_matter(folder: str, file_name: str):
3132
print(f"An error occurred: {e}")
3233

3334
# Example usage:
34-
folder = './_posts/'
35-
file_name = '2023-01-01-error_coefficientes.md' # Replace with your file name
36-
extract_and_print_front_matter(folder, file_name)
35+
if __name__ == "__main__":
36+
parser = argparse.ArgumentParser(description="Extract and print front matter from a markdown file")
37+
parser.add_argument("--path", default="./_posts/", help="Folder containing the markdown file")
38+
parser.add_argument("--file", default="2023-01-01-error_coefficientes.md", help="Markdown file name")
39+
args = parser.parse_args()
40+
extract_and_print_front_matter(args.path, args.file)

fix_date.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import frontmatter
4+
import argparse
45

56
def extract_date_from_filename(filename):
67
# Assuming the filename format is 'YYYY-MM-DD-some-title.md'
@@ -46,6 +47,8 @@ def process_markdown_files_in_directory(directory):
4647
filepath = os.path.join(directory, filename)
4748
process_markdown_file(filepath)
4849

49-
# Example usage:
50-
directory_path = './_posts' # Change to your directory path
51-
process_markdown_files_in_directory(directory_path)
50+
if __name__ == "__main__":
51+
parser = argparse.ArgumentParser(description="Fix dates in markdown front matter")
52+
parser.add_argument("--path", default="./_posts", help="Target folder")
53+
args = parser.parse_args()
54+
process_markdown_files_in_directory(args.path)

fix_frontmatter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import frontmatter
44
import random
5+
import argparse
56

67
TOTAL_FILES = 20
78

@@ -93,6 +94,8 @@ def process_markdown_files_in_directory(directory):
9394
filepath = os.path.join(directory, filename)
9495
process_markdown_file(filepath)
9596

96-
# Example usage:
97-
directory_path = './_posts' # Change to your directory path
98-
process_markdown_files_in_directory(directory_path)
97+
if __name__ == "__main__":
98+
parser = argparse.ArgumentParser(description="Fix front matter in markdown files")
99+
parser.add_argument("--path", default="./_posts", help="Target folder")
100+
args = parser.parse_args()
101+
process_markdown_files_in_directory(args.path)

markdown_category_checker.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import yaml
4+
import argparse
45
from typing import List
56

67
def read_markdown_files_from_folder(folder_path: str) -> List[str]:
@@ -41,7 +42,10 @@ def process_markdown_files(folder_path: str, output_txt_file: str):
4142
output_file.write(f'{filename}\n')
4243

4344

44-
folder_path = './_posts' # Change this to your folder path
45-
output_txt_file = 'files_with_multiple_categories.txt'
46-
process_markdown_files(folder_path, output_txt_file)
47-
print(f'Processing complete. Files with multiple categories saved to {output_txt_file}')
45+
if __name__ == "__main__":
46+
parser = argparse.ArgumentParser(description="Check categories in markdown files")
47+
parser.add_argument("--path", default="./_posts", help="Target folder")
48+
args = parser.parse_args()
49+
output_txt_file = 'files_with_multiple_categories.txt'
50+
process_markdown_files(args.path, output_txt_file)
51+
print(f'Processing complete. Files with multiple categories saved to {output_txt_file}')

markdown_file_processor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import string
4+
import argparse
45

56
# List of stop words to remove from file names
67
STOP_WORDS = {
@@ -122,6 +123,8 @@ def process_markdown_files_in_folder(folder_path: str):
122123
print(f"Finished processing file: {new_file_path}")
123124

124125

125-
# Path to the folder containing markdown files
126-
folder_path = "./_posts"
127-
process_markdown_files_in_folder(folder_path)
126+
if __name__ == "__main__":
127+
parser = argparse.ArgumentParser(description="Process markdown files in a folder")
128+
parser.add_argument("--path", default="./_posts", help="Target folder")
129+
args = parser.parse_args()
130+
process_markdown_files_in_folder(args.path)

markdown_frontmatter_cleanup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import yaml
4+
import argparse
45
from typing import List
56

67
def read_markdown_files_from_folder(folder_path: str) -> List[str]:
@@ -81,6 +82,9 @@ def process_markdown_files(folder_path: str):
8182
except Exception as e:
8283
print(f"Error processing file {md_file}: {e}")
8384

84-
folder_path = './_posts' # Change this to your folder path
85-
process_markdown_files(folder_path)
86-
print(f"Processing complete.")
85+
if __name__ == "__main__":
86+
parser = argparse.ArgumentParser(description="Clean up markdown front matter")
87+
parser.add_argument("--path", default="./_posts", help="Target folder")
88+
args = parser.parse_args()
89+
process_markdown_files(args.path)
90+
print("Processing complete.")

process_markdown_frontmatter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import yaml # You might need to install PyYAML (pip install pyyaml)
4+
import argparse
45

56
def process_frontmatter(frontmatter: dict):
67
"""
@@ -64,6 +65,8 @@ def process_folder(folder_path: str):
6465
process_markdown_file(filepath)
6566

6667

67-
# Specify the folder path containing markdown files
68-
folder_path = './_posts' # Replace with the actual folder path
69-
process_folder(folder_path)
68+
if __name__ == "__main__":
69+
parser = argparse.ArgumentParser(description="Process markdown front matter")
70+
parser.add_argument("--path", default="./_posts", help="Target folder")
71+
args = parser.parse_args()
72+
process_folder(args.path)

rename_files_spaces.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import argparse
23

34

45
def rename_files_in_folder(directory: str) -> None:
@@ -30,6 +31,7 @@ def rename_files_in_folder(directory: str) -> None:
3031

3132

3233
if __name__ == "__main__":
33-
# You can change the path below to point to your folder
34-
folder_path: str = './_posts'
35-
rename_files_in_folder(folder_path)
34+
parser = argparse.ArgumentParser(description="Rename files replacing spaces with underscores")
35+
parser.add_argument("--path", default="./_posts", help="Target folder")
36+
args = parser.parse_args()
37+
rename_files_in_folder(args.path)

0 commit comments

Comments
 (0)