diff options
Diffstat (limited to 'site/app/twirl/twirl_compiler.py')
-rw-r--r-- | site/app/twirl/twirl_compiler.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/site/app/twirl/twirl_compiler.py b/site/app/twirl/twirl_compiler.py new file mode 100644 index 0000000..cc2ba3e --- /dev/null +++ b/site/app/twirl/twirl_compiler.py @@ -0,0 +1,92 @@ +import lxml.html as lh
+import lxml.etree as et
+import os
+import shutil
+
+base_path = "/mnt/hd/web/1bpm.net"
+top_path = os.path.join(base_path, "apps.csound")
+apps_top_path = os.path.join(top_path, "app")
+twirl_path = os.path.join(apps_top_path, "twirl")
+
+apps = ["twist", "twigs", "twine"]
+
+
+def compile_apps():
+ for app in apps:
+ compile(app)
+
+def compile(app):
+ print "Compiling {}".format(app)
+ app_path = os.path.join(apps_top_path, app)
+ target_dir = "{}/{}".format(base_path, app)
+
+ doc = lh.parse(os.path.join(app_path, "index.html"))
+ root = doc.getroot()
+ head = root.xpath("//head")[0]
+
+ script_data = ""
+ css_data = ""
+ post_scripts = []
+
+ for script in root.xpath("//script"):
+ src = script.attrib.get("src")
+ if src:
+ src = src.replace("https://apps.csound.1bpm.net/", "../../")
+ path = os.path.join(app_path, src)
+ with open(path, "r") as f:
+ script_data += f.read() + "\n"
+ else:
+ post_scripts.append(script)
+ script.getparent().remove(script)
+
+ new_script = et.fromstring("<script type=\"text/javascript\" src=\"{}.js\"></script>".format(app))
+ head.append(new_script)
+ for ps in post_scripts:
+ head.append(ps)
+
+ for css in root.xpath("//link"):
+ href = css.attrib.get("href")
+ if href:
+ href = href.replace("https://apps.csound.1bpm.net/", "../../")
+ path = os.path.join(app_path, href)
+ with open(path, "r") as f:
+ css_data += f.read() + "\n"
+ css.getparent().remove(css)
+
+ new_css = et.fromstring("<link rel=\"stylesheet\" href=\"{}.css\" />".format(app))
+ head.append(new_css)
+
+ doc.write(os.path.join(target_dir, "index.html"), method="html", encoding="UTF-8")
+
+ with open(os.path.join(target_dir, "{}.js".format(app)), "w") as f:
+ f.write(script_data)
+
+ with open(os.path.join(target_dir, "{}.css".format(app)), "w") as f:
+ f.write(css_data)
+
+ links = [
+ [twirl_path, "twirl"],
+ [os.path.join(twirl_path, "font"), "font"],
+ [os.path.join(top_path, "udo"), "udo"],
+ [os.path.join(top_path, "code"), "code"],
+ [os.path.join(app_path, "{}.csd".format(app)), "{}.csd".format(app)]
+ ]
+
+ for l in links:
+ target = os.path.join(target_dir, l[1])
+ if os.path.islink(target):
+ os.unlink(target)
+ os.symlink(l[0], target)
+
+ copies = ["documentation.html", "developer_documentation.html"]
+ for c in copies:
+ item = os.path.join(app_path, c)
+ if os.path.exists(item):
+ target = os.path.join(target_dir, c)
+ if os.path.exists(target):
+ os.unlink(target)
+ shutil.copy(item, target)
+
+if __name__ == "__main__":
+ compile_apps()
+
|