-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexport.py
executable file
·85 lines (71 loc) · 2.54 KB
/
export.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
80
81
82
83
84
85
import sys
from bs4 import BeautifulSoup
# convert a file from Bootstrap Studio to Django Template
infile = sys.argv[-1]
with open(infile, "r") as fp:
soup = BeautifulSoup(fp, "lxml")
# handle load
for div in soup.find_all(attrs={"dj-load": True}):
if div:
forline = "{% load " + div.get('dj-load') + " %}"
div.insert_before(forline)
del div
# handle for
for div in soup.find_all(attrs={"dj-for": True}):
if div:
forline = "{% for " + div.get('dj-for') + " %}"
div.insert_before(forline)
if 'dj-for' in div.attrs:
del div.attrs['dj-for']
div.insert_after('{% endfor %}')
# handle refs
for ref in soup.find_all(attrs={"dj-ref": True}):
if ref:
if 'dj-ref' in ref.attrs:
refattr = ref.get('dj-ref')
if ref.string:
ref.string.replace_with('{{ ' + refattr + ' }}')
del ref.attrs['dj-ref']
# handle if
# eg: <div dj-if="form.errors" > will get translated to:
# {% if form.errors %}
# ...
# {% endif %}
for ifs in soup.find_all(attrs={"dj-if": True}):
if ifs:
ifline = "{% if " + ifs.get("dj-if") + " %}"
ifs.insert_before(ifline)
if "dj-if" in ifs.attrs:
del ifs.attrs["dj-if"]
ifs.insert_after("{% endif %}")
# handle block
for div in soup.find_all(attrs={"dj-block": True}):
if div:
blockline = "{% block " + div.get('dj-block') + " %}"
div.insert_before(blockline)
if 'dj-block' in div.attrs:
del div.attrs['dj-block']
div.insert_after('{% endblock %}')
# handle scripts,
# eg: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.js">
# eg <script src="{% static "assets/js/theme.js" %}">
for div in soup.find_all("script"):
if div:
if not div.get("src").startswith("http"):
div.attrs["src"] = "{% static \"" + div.attrs["src"] + "\" %}"""
for div in soup.find_all("link"):
if div:
if not div.get("href").startswith("http"):
div.attrs["href"] = "{% static \"" + div.attrs["href"] + "\" %}"""
for div in soup.find_all("img"):
if div:
div.attrs["src"] = "{% static \"" + div.attrs["src"] + "\" %}"""
for csrf in soup.find_all(attrs={"dj-csrf": True}):
if csrf:
csrf.insert(0, "{% csrf_token %}")
if "dj-csrf" in csrf.attrs:
del csrf.attrs["dj-csrf"]
#print(soup.prettify())
with open(infile, "w") as outfp:
outfp.write("{% load static %}")
outfp.write(soup.prettify())