-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patherb2jinja.py
69 lines (57 loc) · 1.91 KB
/
erb2jinja.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
#!/usr/bin/env python2
"""
This script takes one or more files as input and converts them to jinja2
templates.
Usage
python erb2jinja.py <file> [<file2>...]
"""
def main(filename):
""" Main method doing the conversion from erb to jinja2. """
space_dict = {
"<%if": "<% if",
"<%for": "<% for",
"<%else":"<% else",
"else%>":"else %>",
"<%end":"<% end",
"end%>":"end %>",
}
output = []
with open(filename) as stream:
IF = False
FOR = False
for row in stream.readlines():
# Add spaces in case they are not there
for key in space_dict:
row = row.replace(key, space_dict[key])
if "<%=" in row:
row = row.replace("<%= @", "{{ ")
row = row.replace("<%=", "{{")
row = row.replace("%>", "}}")
elif "<% if" in row:
row = row.replace("<% if", "{% if ")
row = row.replace("%>", "%}")
IF = True
elif "<% for " in row:
row = row.replace("<% for", "{% for ")
row = row.replace("%>", "%}")
FOR = True
elif "<% else %>" in row:
row = row.replace("<% else %>", "{% else %}")
elif "<% end %>" in row:
if IF is True:
row = row.replace("<% end %>", "{% endif %}")
IF = False
elif FOR is True:
row = row.replace("<% end %>", "{% endfor %}")
FOR = False
output.append(row)
print "".join(output)
with open(filename + ".j2", "w") as stream:
stream.write("".join(output))
if __name__ == '__main__':
import sys
if len(sys.argv) <= 1:
print 'Usage: python erb2jinja.py <file> [<file2>...]'
sys.exit(1)
for filename in sys.argv[1:]:
main(filename)