aboutsummaryrefslogtreecommitdiff
path: root/site/app/twirl/twirl_compiler.py
blob: 5a72e2d04f890f3b238e4d76106fdf60267090c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)

    dups = [
        [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 dups:
        target = os.path.join(target_dir, l[1])
        if os.path.islink(target) or os.path.isfile(target):
            os.unlink(target)
        elif os.path.isdir(target):
            shutil.rmtree(target)
        
        if os.path.isfile(l[0]):
            shutil.copy2(l[0], target)
        elif os.path.isdir(l[0]):
            shutil.copytree(l[0], 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()