diff --git a/zooms_generator.py b/zooms_generator.py index 73a95cc..33f8523 100644 --- a/zooms_generator.py +++ b/zooms_generator.py @@ -5,45 +5,133 @@ import sys import re import xml.etree.ElementTree as ET +import argparse from pathlib import Path -def extract_zooms(src_folder, dest_file): +def extract_zooms(src_folder): folder = Path(src_folder) zooms = {} + + max_width = 0 + max_height = 0 + + idx = 0 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 + + print(f"page {idx} : {svg_path.name}") + + zooms[idx] = { + "name": svg_path.stem, + "zooms": [], + } + + tree = ET.parse(svg_path) + root = tree.getroot() - zooms[page_idx] = [] + for svg in root.findall('.//{*}svg'): + if area.get("width") > max_width: + max_width = area.get("width") + if area.get("height") > max_width: + max_width = area.get("height") - tree = ET.parse(svg_path) - root = tree.getroot() - - for area in root.findall('.//{*}rect'): - zooms[page_idx].append([ - float(area.get("width")), - float(area.get("height")), - float(area.get("x")), - float(area.get("y")), - ]) + 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: - 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 zoom in zooms[page_idx]: + for zoom in zooms[page_idx]["zooms"]: 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?)") - data_file.write(f" {[page_idx] + zoom},\n") - data_file.write("]\n") + if first_coma_skiped: + 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'