#!/usr/bin/env python3 """Sync the published canvas file with the artboards on disk. build.py writes the .dc.html artboards and canvas.json is edited by hand; this folds both back into afrix-mobile-concept.html, which carries the editor and the design content in one file. Images already embedded are left alone. python3 build.py && python3 reseed.py """ import io, json, os, re HERE = os.path.dirname(os.path.abspath(__file__)) DOC = os.path.join(HERE, "afrix-mobile-concept.html") src = io.open(DOC, encoding="utf-8").read() m = re.search(r'(]*id="appifact-doc"[^>]*>)(.*?)()', src, re.S) if not m: raise SystemExit("no appifact-doc block in %s" % DOC) doc = json.loads(m.group(2)) files = doc["content"]["files"] order = list(files) # keep the artboard order the editor stored added, changed = [], [] for name in sorted(os.listdir(HERE)): if not name.endswith(".dc.html"): continue body = io.open(os.path.join(HERE, name), encoding="utf-8").read() if name not in files: added.append(name) elif files[name] != body: changed.append(name) files[name] = body canvas = io.open(os.path.join(HERE, "canvas.json"), encoding="utf-8").read() if files.get("canvas.json") != canvas: changed.append("canvas.json") files["canvas.json"] = canvas # new artboards go in before the images and canvas.json, so the editor lists them # beside their siblings rather than after the assets tail = [k for k in order if not k.endswith(".dc.html")] doc["content"]["files"] = {k: files[k] for k in [k for k in order if k.endswith(".dc.html")] + added + tail} payload = json.dumps(doc, ensure_ascii=False, separators=(",", ":")) if "