Created
May 25, 2024 22:06
-
-
Save 1206yaya/2d4d458a305e8a634e822e4216c43151 to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import shutil | |
import os | |
import sys | |
import yaml | |
def ensure_directories(zenn_articles_dir, zenn_images_dir): | |
"""必要なディレクトリが存在するか確認し、なければ作成する。""" | |
os.makedirs(zenn_articles_dir, exist_ok=True) | |
os.makedirs(zenn_images_dir, exist_ok=True) | |
def extract_slug(content): | |
"""YAMLブロックからslugを抽出する。""" | |
yaml_match = re.match(r'---\n(.*?)\n---', content, re.DOTALL) | |
if not yaml_match: | |
raise ValueError("No valid YAML block found at the beginning of the file.") | |
yaml_content = yaml_match.group(1) | |
yaml_data = yaml.safe_load(yaml_content) | |
slug = yaml_data.get('slug') | |
if not slug: | |
raise ValueError("No 'slug' key found in the YAML block.") | |
return slug | |
def copy_image_files(image_files, slug, root_vault_images_dir, zenn_images_dir): | |
"""画像ファイルをコピーし、ファイル名を変更する。""" | |
new_image_paths = [] | |
for image_file in image_files: | |
source_image_path = os.path.join(root_vault_images_dir, image_file) | |
print(f"Processing image file: {image_file}") | |
timestamp_match = re.search(r'-(\d{14})', image_file) | |
if not timestamp_match: | |
print(f"Error: No valid timestamp found in the image file name '{image_file}'.") | |
continue | |
timestamp = timestamp_match.group(1) | |
new_image_file_name = f"{slug}-{timestamp}.png" | |
target_image_path = os.path.join(zenn_images_dir, new_image_file_name) | |
if os.path.isfile(source_image_path): | |
print(f"Copying image file '{source_image_path}' to '{target_image_path}'...") | |
shutil.copy2(source_image_path, target_image_path) | |
new_image_paths.append(f"/images/{new_image_file_name}") | |
else: | |
print(f"Warning: Image file '{source_image_path}' does not exist.") | |
return new_image_paths | |
def process_markdown_file(content, slug, root_vault_images_dir, zenn_images_dir): | |
"""Markdownファイルの内容を処理し、画像ファイル名を置換する。""" | |
image_files = re.findall(r'!\[\[(.*?)\]\]', content) | |
new_image_paths = copy_image_files(image_files, slug, root_vault_images_dir, zenn_images_dir) | |
for old_image, new_image in zip(image_files, new_image_paths): | |
content = content.replace(f'![[{old_image}]]', f'![]({new_image})') | |
return content | |
def zennsync(vault_dir, source_path): | |
root_vault_images_dir = os.path.join(vault_dir, "assets/images/") | |
zenn_dir = os.path.join(vault_dir, "sub-vaults/zenn-content") | |
zenn_articles_dir = os.path.join(zenn_dir, "articles/") | |
zenn_images_dir = os.path.join(zenn_dir, "images/") | |
ensure_directories(zenn_articles_dir, zenn_images_dir) | |
# ソースファイルの存在を確認 | |
if not os.path.isfile(source_path): | |
print(f"Error: Source file '{source_path}' does not exist.") | |
return | |
try: | |
# ファイル内容を読み込み | |
with open(source_path, 'r', encoding='utf-8') as file: | |
content = file.read() | |
# slugを抽出 | |
slug = extract_slug(content) | |
# 新しいファイル名を使用 | |
new_file_name = f"{slug}.md" | |
target_path = os.path.join(zenn_articles_dir, new_file_name) | |
# Markdownファイルの内容を処理 | |
processed_content = process_markdown_file(content, slug, root_vault_images_dir, zenn_images_dir) | |
# 変換後の内容を目的のディレクトリに書き込み | |
with open(target_path, 'w', encoding='utf-8') as file: | |
file.write(processed_content) | |
print(f"File '{source_path}' has been copied to '{target_path}' with modifications.") | |
except Exception as e: | |
print(f"Error: {e}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python zennsync <vault_directory> <path_to_markdown_file>") | |
else: | |
vault_directory = sys.argv[1] | |
markdown_file_path = sys.argv[2] | |
zennsync(vault_directory, markdown_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment