🎨 manage comics via yaml files
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
# Licensed under the MIT License - see LICENSE file for details
|
||||
|
||||
"""
|
||||
Script to add a new comic entry to comics_data.py with reasonable defaults
|
||||
Script to add a new comic entry as a YAML file with reasonable defaults
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
@@ -51,7 +51,7 @@ Write your author note here using markdown formatting.
|
||||
|
||||
def main():
|
||||
"""Add a new comic entry with defaults"""
|
||||
parser = argparse.ArgumentParser(description='Add a new comic entry to comics_data.py')
|
||||
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()
|
||||
@@ -59,53 +59,75 @@ def main():
|
||||
# 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 = {
|
||||
comic_data = {
|
||||
'number': number,
|
||||
'filename': f'comic-{number:03d}.png',
|
||||
'date': datetime.now().strftime('%Y-%m-%d'),
|
||||
'date': date_str,
|
||||
'alt_text': f'Comic #{number}',
|
||||
}
|
||||
|
||||
# Get path to comics_data.py
|
||||
# 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_file = os.path.join(parent_dir, 'comics_data.py')
|
||||
comics_dir = os.path.join(parent_dir, 'data', 'comics')
|
||||
yaml_file = os.path.join(comics_dir, f'{number:03d}.yaml')
|
||||
|
||||
# Read file
|
||||
with open(comics_file, 'r') as f:
|
||||
content = f.read()
|
||||
# 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']}"
|
||||
"""
|
||||
|
||||
# Format new entry
|
||||
if args.markdown:
|
||||
entry_str = f""" {{
|
||||
'number': {comic['number']},
|
||||
'filename': {repr(comic['filename'])},
|
||||
'date': {repr(comic['date'])},
|
||||
'alt_text': {repr(comic['alt_text'])},
|
||||
'author_note_md': {repr(comic['date'] + '.md')}
|
||||
}}"""
|
||||
yaml_content += f'\n# Markdown author note (overrides author_note if present)\nauthor_note_md: "{date_str}.md"\n'
|
||||
else:
|
||||
entry_str = f""" {{
|
||||
'number': {comic['number']},
|
||||
'filename': {repr(comic['filename'])},
|
||||
'date': {repr(comic['date'])},
|
||||
'alt_text': {repr(comic['alt_text'])}
|
||||
}}"""
|
||||
yaml_content += '\n# Optional: Add author note\n# author_note: "Your thoughts about this comic."\n'
|
||||
|
||||
# Insert before closing bracket
|
||||
insert_pos = content.rfind(']')
|
||||
new_content = content[:insert_pos] + entry_str + ",\n" + content[insert_pos:]
|
||||
yaml_content += """
|
||||
# Optional: Add a title
|
||||
# title: "Title of Your Comic"
|
||||
|
||||
# Write back
|
||||
with open(comics_file, 'w') as f:
|
||||
f.write(new_content)
|
||||
# Optional: Override global settings
|
||||
# full_width: true
|
||||
# plain: true
|
||||
|
||||
print(f"Added comic #{number}")
|
||||
# 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(comic['date'], parent_dir)
|
||||
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__':
|
||||
|
||||
Reference in New Issue
Block a user