1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| ''' Author: Simon Date: 2024-01-26 01:27:57 LastEditors: Simon LastEditTime: 2024-06-05 23:24:57 Description: update blog picture address according to the situation ''' import os import re import sys
def find_and_replace_images(directory, action): for root, dirs, files in os.walk(directory): for file in files: if file.endswith(".md"): file_path = os.path.join(root, file) process_file(file_path, action)
def process_file(file_path, action): with open(file_path, 'r', encoding='utf-8') as file: content = file.readlines()
relative_dir = os.path.dirname(os.path.relpath(file_path, start='./source/_posts')) blog_name = os.path.splitext(os.path.basename(file_path))[0] date_line = next((line for line in content if line.startswith('date: ')), None) if date_line: creation_date = extract_date(date_line) modified_content, changes_made, changes_detail = modify_content(content, action, blog_name, creation_date, relative_dir)
if changes_made: with open(file_path, 'w', encoding='utf-8') as file: file.writelines(modified_content) print(f"Updated file: {file_path}") for detail in changes_detail: print(f" Line {detail['line']}: {detail['original']} -> {detail['modified']}") print( " ...\n") break else: pass else: pass
def extract_date(date_line): match = re.search(r'\d{4}-\d{2}-\d{2}', date_line) if match: return match.group().replace('-', '/') return None
def modify_content(content, action, blog_name, creation_date, relative_dir): changes_made = False changes_detail = []
image_pattern = re.compile(r"!\[\]\(" + re.escape(blog_name) + r"/([^)]+)\)") link_pattern = re.compile(r"\]\(" + re.escape(blog_name) + r"/([^)]+)\)")
for i, line in enumerate(content): original_line = line if action == "--upload" and creation_date: if relative_dir: line = image_pattern.sub(f"", line) line = link_pattern.sub(f"](/{creation_date}/{relative_dir}/{blog_name}/\\1)", line) else: line = image_pattern.sub(f"", line) line = link_pattern.sub(f"](/{creation_date}/{blog_name}/\\1)", line) elif action == "--local": if creation_date: if relative_dir: line = re.sub(r"!\[\]\(/" + re.escape(creation_date) + "/" + re.escape(relative_dir) + "/" + re.escape(blog_name) + r"/([^)]+)\)", f"", line) line = re.sub(r"\]\(/" + re.escape(creation_date) + "/" + re.escape(relative_dir) + "/" + re.escape(blog_name) + r"/([^)]+)\)", f"]({blog_name}/\\1)", line) else: line = re.sub(r"!\[\]\(/" + re.escape(creation_date)+ "/" + re.escape(blog_name) + r"/([^)]+)\)", f"", line) line = re.sub(r"\]\(/" + re.escape(creation_date) + "/" + re.escape(blog_name) + r"/([^)]+)\)", f"]({blog_name}/\\1)", line)
if line != original_line: changes_made = True changes_detail.append({'line': i+1, 'original': original_line.strip(), 'modified': line.strip()}) content[i] = line
return content, changes_made, changes_detail
if __name__ == "__main__": directory = './source/_posts'
if len(sys.argv) != 2 or sys.argv[1] not in ["--local", "--upload"]: print("Usage: python script.py --local | --upload") sys.exit(1)
action = sys.argv[1] find_and_replace_images(directory, action)
|