for image_name in images: image_path = os.path.join(source_folder, image_name) if os.path.isfile(image_path): print(f"正在处理图片:\033[31m{image_name}\033[0m") target_path = os.path.join(target_folder, image_name)
if os.path.exists(target_path): print(f"目标文件夹中已存在同名图片文件:{target_path}") print("跳过当前图片。") print() continue
withopen(index_file_path, 'r', encoding='utf-8') as file: lines = file.readlines()
# Find the line number containing {% gallery %} gallery_line_index = -1 for i, line inenumerate(lines): if'{% gallery %}'in line: gallery_line_index = i break
# Insert the new content after {% gallery %}, ensuring a newline new_content = f"\n" lines.insert(gallery_line_index + 1, new_content)
withopen(index_file_path, 'w', encoding='utf-8') as file: file.writelines(lines)
print(f"已更新 {index_file_path}") print()
defprocess_markdown_file(file_path, wakawaka_images_folder): file_name = os.path.basename(file_path) title = os.path.splitext(file_name)[0] print(f"正在处理Markdown文档:\033[31m{file_name}\033[0m") print("当前文章的categories是?") categories = input().encode(sys.stdin.encoding).decode(sys.stdin.encoding).replace(",", ",").split(",") categories = [category.strip() for category in categories]
print("当前文章的tags是?") tags = input().encode(sys.stdin.encoding).decode(sys.stdin.encoding).replace(",", ",").split(",") tags = [tag.strip() for tag in tags]
cover_image = get_random_image(wakawaka_images_folder) info = f"---\n" info += f"title: [{title}]\n" info += f"categories: {categories}\n" info += f"tags: {tags}\n" info += f"background: url(/img/wakawaka/{cover_image})\n" info += f"cover: /img/wakawaka/{cover_image}\n" info += f"---\n"
for file_name in files: file_path = os.path.join(drafts_folder, file_name) if os.path.isfile(file_path) and file_name.lower().endswith(".md"): process_markdown_file(file_path, wakawaka_target_folder)