|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +import re |
| 4 | + |
| 5 | +# Define the folder where the markdown files are stored |
| 6 | +folder_path = './_posts' # Change this to your folder path |
| 7 | + |
| 8 | +# List of stop words to exclude from capitalization |
| 9 | +stop_words = {'at', 'vs', 'and', 'or', 'the', 'of', 'in', 'on', 'for', 'to', 'a'} |
| 10 | + |
| 11 | +# Function to capitalize keywords based on your rules |
| 12 | +def capitalize_keywords(keywords): |
| 13 | + def capitalize_word(word, first_word=False): |
| 14 | + # Only capitalize if it's not a stop word or it's the first word |
| 15 | + if word in stop_words and not first_word: |
| 16 | + return word |
| 17 | + else: |
| 18 | + return word.capitalize() |
| 19 | + |
| 20 | + def process_phrase(phrase): |
| 21 | + words = phrase.split() |
| 22 | + # Capitalize each word as per rules, first word always capitalized |
| 23 | + return ' '.join(capitalize_word(word, i == 0) for i, word in enumerate(words)) |
| 24 | + |
| 25 | + return [process_phrase(phrase) for phrase in keywords] |
| 26 | + |
| 27 | +# Function to process each markdown file |
| 28 | +def process_markdown_file(file_path): |
| 29 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 30 | + content = file.read() |
| 31 | + |
| 32 | + # Use regex to extract the front matter (between '---' lines) |
| 33 | + front_matter_match = re.match(r'---(.*?)---', content, re.DOTALL) |
| 34 | + if not front_matter_match: |
| 35 | + print(f"No front matter found in {file_path}") |
| 36 | + return |
| 37 | + |
| 38 | + front_matter = front_matter_match.group(1) |
| 39 | + |
| 40 | + # Parse the front matter using YAML |
| 41 | + try: |
| 42 | + front_matter_dict = yaml.safe_load(front_matter) |
| 43 | + except yaml.YAMLError as exc: |
| 44 | + print(f"Error parsing YAML in {file_path}: {exc}") |
| 45 | + return |
| 46 | + |
| 47 | + # If 'keywords' exists in front matter, process it |
| 48 | + if 'keywords' in front_matter_dict: |
| 49 | + original_keywords = front_matter_dict['keywords'] |
| 50 | + updated_keywords = capitalize_keywords(original_keywords) |
| 51 | + front_matter_dict['keywords'] = updated_keywords |
| 52 | + |
| 53 | + # Replace the front matter in the content |
| 54 | + updated_front_matter = yaml.dump(front_matter_dict, default_flow_style=False) |
| 55 | + updated_content = re.sub(r'---(.*?)---', f'---\n{updated_front_matter}---', content, flags=re.DOTALL) |
| 56 | + |
| 57 | + # Save the updated content back to the file |
| 58 | + with open(file_path, 'w', encoding='utf-8') as file: |
| 59 | + file.write(updated_content) |
| 60 | + |
| 61 | + print(f"Updated keywords in {file_path}") |
| 62 | + else: |
| 63 | + print(f"No 'keywords' found in {file_path}") |
| 64 | + |
| 65 | +# Function to process all markdown files in the folder |
| 66 | +def process_all_markdown_files(folder_path): |
| 67 | + for filename in os.listdir(folder_path): |
| 68 | + if filename.endswith(".md"): # Check if it's a markdown file |
| 69 | + file_path = os.path.join(folder_path, filename) |
| 70 | + process_markdown_file(file_path) |
| 71 | + |
| 72 | +# Run the function for the specified folder |
| 73 | +process_all_markdown_files(folder_path) |
0 commit comments