# Melpomene webcomic reader JSON/JS/HTML generator
# Version 1.0.0 - UNSTABLE
# CC-BY-NC-SA https://git.aribaud.net/caribaud/melpomene/
import sys
import re
import xml.etree.ElementTree as ET
import argparse
from pathlib import Path
HTML_START_CONSTANT = """\
\n')
for page_idx in sorted(zooms.keys()):
img_url = f"{prefix}{zooms[page_idx]['name']}.{extention}"
zoom_html_data = [','.join([str(zoom) for zoom in page_zooms]) for page_zooms in zooms[page_idx]["zooms"]]
zoom_html_str = ';'.join(zoom_html_data)
data_file.write(f'
\n')
data_file.write(f'
\n')
data_file.write(HTML_END_CONSTANT)
def generate_argparse():
""" Generate Melpomene's generator input parser"""
parser = argparse.ArgumentParser(
description="Helper that can generate JSON / JS / HTML files for Melpomene webcomic reader"
)
parser.add_argument("output_format", choices=["html", "json", "js"], help="The type of output to generate")
parser.add_argument("svg_folders", help="Path of the folder containing the SVGs")
parser.add_argument("-o", metavar="dest_file", help="Where to write the generator output to")
parser.add_argument("-p", default="", metavar="img_url_prefix", help="What to prefix the URL of the images when using HTML format.")
parser.add_argument("-e", default="png", metavar="img_ext", help="What extention to use in the URL of the images when using HTML format.")
return parser
if __name__ == "__main__":
args = generate_argparse().parse_args()
# Get the final outout name
output = None
if not args.o:
output = "melpomene_data"
else:
output = args.o
if args.output_format == "html" and not output.endswith(".html"):
output += ".html"
elif args.output_format == "json" and not output.endswith(".json"):
output += ".json"
elif args.output_format == "js" and not output.endswith(".js"):
output += ".js"
zooms, max_width, max_height = extract_zooms(args.svg_folders)
if args.output_format == "html":
write_html(zooms, output, max_width, max_height, args.p, args.e)
elif args.output_format == "json":
write_json_or_js(zooms, output, False)
elif args.output_format == "js":
write_json_or_js(zooms, output, True)