#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # cringe.py # Generate HTML image grids from .cringe files import argparse import os import re import sys def parse_arguments(): parser = argparse.ArgumentParser(description='Generate HTML image grids from .cringe files.') parser.add_argument('cringe_files', nargs='+', help='Path(s) to .cringe file(s)') parser.add_argument('-o', '--output', help='Output HTML file (defaults to stdout)', default=None) return parser.parse_args() def extract_src(cringe_content): """ Extract the src URL from the blurhash shortcode. """ match = re.search(r'src="([^"]+)"', cringe_content) if match: return match.group(1) else: raise ValueError("No src attribute found in the .cringe file.") def generate_html(cringe_contents): """ Generate the HTML structure with image grids from the list of .cringe contents. """ html_lines = [ '
', '
' ] for content in cringe_contents: try: src = extract_src(content) html_lines.append(f' ') # Indent the shortcode content by two spaces shortcode_lines = content.strip().split('\n') indented_shortcode = '\n'.join([' ' + line for line in shortcode_lines]) html_lines.append(indented_shortcode) html_lines.append(' ') except ValueError as ve: print(f"Warning: {ve}", file=sys.stderr) continue html_lines.extend([ '
', '
' ]) return '\n'.join(html_lines) def main(): args = parse_arguments() cringe_contents = [] for file_path in args.cringe_files: if not os.path.isfile(file_path): print(f"Error: File '{file_path}' does not exist.", file=sys.stderr) sys.exit(1) if not file_path.lower().endswith('.cringe'): print(f"Warning: File '{file_path}' does not have a .cringe extension.", file=sys.stderr) try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() cringe_contents.append(content) except Exception as e: print(f"Error reading file '{file_path}': {e}", file=sys.stderr) sys.exit(1) html_output = generate_html(cringe_contents) if args.output: try: with open(args.output, 'w', encoding='utf-8') as out_file: out_file.write(html_output) print(f"HTML content successfully written to '{args.output}'.") except Exception as e: print(f"Error writing to file '{args.output}': {e}", file=sys.stderr) sys.exit(1) else: print(html_output) if __name__ == "__main__": main()