toolkit / utils /shortcode.py
k4d3's picture
chmod
fea0c98
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ファイルからshortcodeを生成する
# Generate shortcode from files
import os
import json
import sys
import argparse
from utils.file_processor import FileProcessor, ProcessorOptions
from pathlib import Path
class JsonShortcodeProcessor(FileProcessor):
def __init__(self, options: ProcessorOptions, base_dir: Path, url_base: str):
super().__init__(options)
self.base_dir = base_dir
self.url_base = url_base
def process_content(self, content: str) -> str:
try:
data = json.loads(content)
# Process JSON content and generate shortcode
# (Implementation depends on specific shortcode requirements)
return self.generate_shortcode(data)
except json.JSONDecodeError as e:
self.logger.error(f"Failed to parse JSON: {e}")
return ""
def generate_shortcode(self, data: dict) -> str:
# Implement shortcode generation logic here
pass
def parse_args():
parser = argparse.ArgumentParser(description='Generate shortcode from files')
parser.add_argument('directory', nargs='?', default=Path.cwd(),
help='Directory to process (default: current directory)')
parser.add_argument('--url', '-u', default='',
help='Base URL for src attribute (e.g., https://example.com)')
return parser.parse_args()
def main():
args = parse_args()
directory = Path(args.directory)
if not directory.is_dir():
print(f"Error: Directory '{directory}' does not exist")
sys.exit(1)
options = ProcessorOptions(
recursive=True,
dry_run=False,
file_extensions={'.json'}
)
processor = JsonShortcodeProcessor(options, directory, args.url)
print(f"Processing directory: {directory}")
print(f"Using URL base: {args.url if args.url else '(none)'}")
processor.process_directory(directory)
if __name__ == "__main__":
main()