-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_static.py
81 lines (63 loc) · 2.04 KB
/
merge_static.py
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
# This script merges a template HTML file (floor_plan.html), the
# cleaned up SVG file (floor_plan_cleanup/cleaned_up.svg), javascript
# code to place things onto the floor plan (placement.js) and the data
# file (furnashings/things.json) into a single HTML file which shows
# and lists the things.
# This script is made obsolete by a working version of placement.js.
import json
import os.path
import sys
import xml.dom
import xml.dom.minidom
import cssutils # pip install cssutils
import cssutils.css
# What's the right way to load these?
sys.path.insert(
0, os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"lib"))
from transform import *
from xml_utils import *
from stylesheet import *
# Input files:
HTML_TEMPLATE = "floor_plan.html"
JAVASCRIPT = "placement.js"
FLOOR_PLAN = "floor_plan_cleanup/cleaned_up.svg"
THINGS = "furnashings/things.json"
#Output file
OUTPUT = "merged_floor_plan.html"
THING_STYLES= '''
.thing {
stroke: red;
fill: lightgray;
stroke-width: 1;
vector-effect: non-scaling-stroke;
}
'''
def merge_svg(doc):
svgdoc = xml.dom.minidom.parse(FLOOR_PLAN)
for object in doc.getElementsByTagName("object"):
if object.getAttribute("data") == "floor_plan.svg":
object.parentNode.insertBefore(svgdoc.documentElement, object)
object.parentNode.removeChild(object)
break
def merge_javascript(doc):
with open(JAVASCRIPT, "r") as f:
js = f.read()
with open(THINGS, "r") as f:
things = f.read()
for script in doc.getElementsByTagName("script"):
if script.getAttribute("src") == "placement.js":
script.removeAttribute("src");
script.appendChild(doc.createTextNode("//"))
script.appendChild(doc.createCDATASection(
"\nTHINGS = " + things + ";\n\n" + js + "\n//"))
break
def main():
doc = xml.dom.minidom.parse(HTML_TEMPLATE)
merge_svg(doc)
merge_javascript(doc)
ensure_stylesheet(doc, "thing-styles").appendChild(doc.createTextNode(THING_STYLES))
write_pretty(doc, OUTPUT)
if __name__ == "__main__":
main()