Files
sunday/scripts/add_comic.py
2025-11-15 19:20:21 +10:00

135 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python3
# Sunday Comics - Add comic script
# Copyright (c) 2025 Tomasita Cabrera
# Licensed under the MIT License - see LICENSE file for details
"""
Script to add a new comic entry as a YAML file with reasonable defaults
"""
import sys
import os
import argparse
from datetime import datetime
# Add parent directory to path so we can import comics_data
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from comics_data import COMICS
def create_markdown_file(date_str, parent_dir):
"""Create a markdown file for author notes"""
author_notes_dir = os.path.join(parent_dir, 'content', 'author_notes')
# Create directory if it doesn't exist
os.makedirs(author_notes_dir, exist_ok=True)
markdown_file = os.path.join(author_notes_dir, f'{date_str}.md')
# Check if file already exists
if os.path.exists(markdown_file):
print(f"Warning: Markdown file already exists: {markdown_file}")
return markdown_file
# Create file with template content
template = f"""# Author Note
Write your author note here using markdown formatting.
**Example formatting:**
- *Italic text*
- **Bold text**
- [Links](https://example.com)
- `Code snippets`
"""
with open(markdown_file, 'w') as f:
f.write(template)
print(f"Created markdown file: {markdown_file}")
return markdown_file
def main():
"""Add a new comic entry with defaults"""
parser = argparse.ArgumentParser(description='Add a new comic entry as a YAML file')
parser.add_argument('-m', '--markdown', action='store_true',
help='Generate a markdown file for author notes and add author_note_md field to comic entry')
args = parser.parse_args()
# Get next number
number = max(comic['number'] for comic in COMICS) + 1 if COMICS else 1
# Get today's date
date_str = datetime.now().strftime('%Y-%m-%d')
# Create entry with defaults
comic_data = {
'number': number,
'filename': f'comic-{number:03d}.png',
'date': date_str,
'alt_text': f'Comic #{number}',
}
# Add markdown reference if requested
if args.markdown:
comic_data['author_note_md'] = f'{date_str}.md'
# Get paths
script_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(script_dir)
comics_dir = os.path.join(parent_dir, 'data', 'comics')
yaml_file = os.path.join(comics_dir, f'{number:03d}.yaml')
# Create comics directory if it doesn't exist
os.makedirs(comics_dir, exist_ok=True)
# Check if file already exists
if os.path.exists(yaml_file):
print(f"Error: Comic file already exists: {yaml_file}")
sys.exit(1)
# Create YAML file with comments
yaml_content = f"""# Comic #{number}
number: {number}
filename: {comic_data['filename']}
date: "{date_str}"
alt_text: "{comic_data['alt_text']}"
"""
if args.markdown:
yaml_content += f'\n# Markdown author note (overrides author_note if present)\nauthor_note_md: "{date_str}.md"\n'
else:
yaml_content += '\n# Optional: Add author note\n# author_note: "Your thoughts about this comic."\n'
yaml_content += """
# Optional: Add a title
# title: "Title of Your Comic"
# Optional: Override global settings
# full_width: true
# plain: true
# Optional: Start a new section (only add to first comic of section)
# section: "Chapter X: Title"
"""
# Write YAML file
with open(yaml_file, 'w') as f:
f.write(yaml_content)
print(f"Created comic #{number}: {yaml_file}")
# Create markdown file if requested
if args.markdown:
create_markdown_file(date_str, parent_dir)
print(f"\nNext steps:")
print(f"1. Add your comic image as: static/images/comics/{comic_data['filename']}")
print(f"2. Edit {yaml_file} to customize the comic metadata")
if args.markdown:
print(f"3. Edit content/author_notes/{date_str}.md to write your author note")
if __name__ == '__main__':
main()