Adding format options and help for generator
This commit is contained in:
parent
a8c1a86592
commit
9f52233405
|
@ -5,45 +5,133 @@
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
import argparse
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def extract_zooms(src_folder, dest_file):
|
def extract_zooms(src_folder):
|
||||||
folder = Path(src_folder)
|
folder = Path(src_folder)
|
||||||
|
|
||||||
zooms = {}
|
zooms = {}
|
||||||
|
|
||||||
|
max_width = 0
|
||||||
|
max_height = 0
|
||||||
|
|
||||||
|
idx = 0
|
||||||
|
|
||||||
for svg_path in folder.glob("*.svg"):
|
for svg_path in folder.glob("*.svg"):
|
||||||
print(svg_path.name)
|
|
||||||
match = re.search("P(\d+)", svg_path.name)
|
|
||||||
if match:
|
|
||||||
|
|
||||||
page_idx = int(match.group(1))
|
idx += 1
|
||||||
|
|
||||||
zooms[page_idx] = []
|
print(f"page {idx} : {svg_path.name}")
|
||||||
|
|
||||||
tree = ET.parse(svg_path)
|
zooms[idx] = {
|
||||||
root = tree.getroot()
|
"name": svg_path.stem,
|
||||||
|
"zooms": [],
|
||||||
|
}
|
||||||
|
|
||||||
for area in root.findall('.//{*}rect'):
|
tree = ET.parse(svg_path)
|
||||||
zooms[page_idx].append([
|
root = tree.getroot()
|
||||||
float(area.get("width")),
|
|
||||||
float(area.get("height")),
|
for svg in root.findall('.//{*}svg'):
|
||||||
float(area.get("x")),
|
if area.get("width") > max_width:
|
||||||
float(area.get("y")),
|
max_width = area.get("width")
|
||||||
])
|
if area.get("height") > max_width:
|
||||||
|
max_width = area.get("height")
|
||||||
|
|
||||||
|
for area in root.findall('.//{*}rect'):
|
||||||
|
zooms[idx]["zooms"].append([
|
||||||
|
float(area.get("width")),
|
||||||
|
float(area.get("height")),
|
||||||
|
float(area.get("x")),
|
||||||
|
float(area.get("y")),
|
||||||
|
])
|
||||||
|
|
||||||
|
return zooms, max_width, max_height
|
||||||
|
|
||||||
|
|
||||||
|
def write_json_or_js(zooms, dest_file, is_js):
|
||||||
|
|
||||||
with open(dest_file, "w") as data_file:
|
with open(dest_file, "w") as data_file:
|
||||||
data_file.write("zooms = [\n")
|
|
||||||
|
if is_js:
|
||||||
|
data_file.write("PAGES_ZOOMS = ")
|
||||||
|
data_file.write("[\n")
|
||||||
|
first_coma_skiped = False
|
||||||
for page_idx in sorted(zooms.keys()):
|
for page_idx in sorted(zooms.keys()):
|
||||||
for zoom in zooms[page_idx]:
|
for zoom in zooms[page_idx]["zooms"]:
|
||||||
|
|
||||||
if zoom[2] < 0 or zoom[3] < 0 :
|
if zoom[2] < 0 or zoom[3] < 0 :
|
||||||
print(f"WARNING: negative pos x / pos y in page {page_idx} for zoom {zoom} (is the rectangle flipped?)")
|
print(f"WARNING: negative pos x / pos y in page {page_idx} for zoom {zoom} (is the rectangle flipped?)")
|
||||||
|
|
||||||
data_file.write(f" {[page_idx] + zoom},\n")
|
if first_coma_skiped:
|
||||||
data_file.write("]\n")
|
data_file.write(",\n")
|
||||||
|
else:
|
||||||
|
first_coma_skiped = True
|
||||||
|
data_file.write(f" {[page_idx] + zoom}")
|
||||||
|
data_file.write("\n]\n")
|
||||||
|
|
||||||
|
|
||||||
|
def write_html(zooms, dest_file, pages_width, pages_height, prefix, extention):
|
||||||
|
|
||||||
|
with open(dest_file, "w") as data_file:
|
||||||
|
|
||||||
|
data_file.write(f'<div id="reader-pages" class="animated" data-pages-width="{pages_width}" data-pages-height="{pages_height}" hidden>\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' <img loading="lazy" src="{img_url}" data-zooms="{zoom_html_str}"/>\n')
|
||||||
|
|
||||||
|
data_file.write(f'</div>\n')
|
||||||
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
extract_zooms(sys.argv[1], sys.argv[2])
|
|
||||||
|
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)
|
Loading…
Reference in New Issue