forked from onepercentclub/bluebottle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_templates.py
65 lines (55 loc) · 2.13 KB
/
parse_templates.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
#!/usr/bin/python
import os, glob, sys, getopt
from django.conf import settings
from django.template import Context, Template, loader
from django.utils.translation import ugettext
from django.contrib.auth.models import AbstractBaseUser
import templatetag_handlebars
import re
from BeautifulSoup import BeautifulSoup
PROJECT_ROOT = os.path.dirname(os.path.normpath(os.path.join(__file__, '..', '..')))
USE_EMBER_STYLE_ATTRS=True
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates')
)
settings.configure(
INSTALLED_APPS=('bluebottle.common','templatetag_handlebars','django.contrib.staticfiles','statici18n',),
STATIC_URL='',
ROOT_URLCONF='bluebottle.urls',
AUTH_USER_MODEL='models.AbstractBaseUser'
)
def main(argv):
destdir = './test/js/templates'
try:
opts, args = getopt.getopt(argv,"hd:",["destdir="])
except getopt.GetoptError:
print 'parse_templates.py -d <destdirectory>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'parse_templates.py -d <destdirectory>'
sys.exit()
elif opt in ("-d", "--destdir"):
destdir = arg
# Template method was returning an empty string if there was a comment (<!--..-->)
# => strip them out before passing to Template below
pattern = '\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>'
htmlcomments = re.compile(pattern, re.DOTALL)
munged_templates = ''
for root, dirs, files in os.walk("bluebottle"):
for file in files:
if file.endswith(".hbs"):
template_file = open(os.path.join(root, file), 'r')
template = Template(htmlcomments.sub('', template_file.read()))
munged_templates += template.render(Context({}))
template_file.close()
soup = BeautifulSoup(munged_templates)
print('Templates written:')
for tag in soup('script'):
file = '%s/%s.handlebars' % (destdir, tag["id"].replace('/', '_'))
f = open(file, 'w')
f.write(tag.string.encode('utf-8').strip())
f.close()
print(' %s' % (file))
if __name__ == "__main__":
main(sys.argv[1:])