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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| import os import re import shutil from datetime import datetime import argparse import urllib.parse
def replace_spaces_in_image_paths(markdown_content): image_pattern = r'!\[([^\]]*)\]\(([^)]+)\)' img_html_pattern = r'(<img\s+)(src="[^"]+")([^>]*>)' def replace_image(match): alt_text = match.group(1) image_path = match.group(2) if not image_path.startswith('/'): image_path = '/' + image_path encoded_path = urllib.parse.quote(image_path, safe='/') return f''
def replace_html_img(match): src_part = match.group(2) other_attributes = match.group(3) image_path = src_part.split('=')[1].strip('"') if not image_path.startswith('/'): image_path = '/' + image_path encoded_path = urllib.parse.quote(image_path, safe='/') return f'{match.group(1)}src="{encoded_path}"{other_attributes}'
markdown_content = re.sub(image_pattern, replace_image, markdown_content) markdown_content = re.sub(img_html_pattern, replace_html_img, markdown_content)
front_matter_pattern = r'---\n(.*?)\n---' match = re.search(front_matter_pattern, markdown_content, re.DOTALL)
if match: front_matter = match.group(1)
cover_pattern = r'cover:\s*(\S.*)'
def replace_cover_path(match): cover_path = match.group(1).strip()
if not cover_path.startswith('/'): cover_path = '/' + cover_path
encoded_cover_path = urllib.parse.quote(cover_path, safe='/')
return f'cover: {encoded_cover_path}'
front_matter = re.sub(cover_pattern, replace_cover_path, front_matter)
markdown_content = markdown_content[:match.start(1)] + front_matter + markdown_content[match.end(1):]
return markdown_content def auto_adding_date(markdown_content): front_matter_pattern = r'---\n(.*?)\n---' match = re.search(front_matter_pattern, markdown_content, re.DOTALL)
if match: front_matter = match.group(1)
current_date_time = datetime.now().strftime('%Y/%m/%d/%H:%M:%S')
date_pattern = r'date:\s*(\d{4}/\d{2}/\d{2}/\d{2}:\d{2}:\d{2})' updated_pattern = r'updated:\s*(\d{4}/\d{2}/\d{2}/\d{2}:\d{2}:\d{2})'
has_date = re.search(date_pattern, front_matter) has_updated = re.search(updated_pattern, front_matter)
if has_date: if has_updated: front_matter = re.sub(updated_pattern, f'updated: {current_date_time}', front_matter) else: front_matter += f'\nupdated: {current_date_time}' else: front_matter += f'\ndate: {current_date_time}\nupdated: {current_date_time}'
markdown_content = markdown_content[:match.start(1)] + front_matter + markdown_content[match.end(1):]
return markdown_content
def copy_images_to_blog(org_img_path, BLOG_img_path): if os.path.exists(org_img_path) and os.path.isdir(org_img_path): os.makedirs(BLOG_img_path, exist_ok=True)
for file_name in os.listdir(org_img_path): src_file = os.path.join(org_img_path, file_name) dest_file = os.path.join(BLOG_img_path, file_name)
if os.path.isfile(src_file): shutil.copy2(src_file, dest_file) print(f"Copied: {src_file} -> {dest_file}") else: print(f"Source folder '{org_img_path}' does not exist.")
def parse_args(): parser = argparse.ArgumentParser(description="Process a markdown file and copy images to BLOG.") parser.add_argument("-n", "--name", required=True, help="Markdown file name (e.g., example.md)") return parser.parse_args()
if __name__ == '__main__': args = parse_args() file_name = args.name if not os.path.isabs(file_name): file_path = os.path.abspath(file_name) else: file_path = file_name file_base_name = os.path.splitext(os.path.basename(file_name))[0] org_img_path = os.path.join(os.path.dirname(file_path), "imgs", file_base_name)
BLOG_root = r"path/to/BLOG" BLOG_path = os.path.join(BLOG_root, "source", "_posts", os.path.basename(file_name)) BLOG_img_path = os.path.join(BLOG_root, "source", "imgs", file_base_name)
print(f"Processing file: {file_path}") print(f"Org image path: {org_img_path}") print(f"Blog post path: {BLOG_path}") print(f"Blog image path: {BLOG_img_path}")
with open(file_path, 'r', encoding='utf-8') as file: content = file.read() new_content = auto_adding_date(content) with open(file_path, 'w', encoding='utf-8') as file: file.write(new_content) print(f"Markdown 文件已修改并保存为 '{file_path}'")
new_content = replace_spaces_in_image_paths(new_content) with open(BLOG_path, 'w', encoding='utf-8') as file: file.write(new_content) print(f"Markdown 文件已修改并保存为 '{BLOG_path}'")
copy_images_to_blog(org_img_path, BLOG_img_path)
|