3 # Script to convert Friendica internal template files into Smarty template files
4 # Copyright 2012 Zach Prezkuta
5 # Licensed under GPL v3
12 def fToSmarty(matches):
13 match = matches.group(0)
16 match = string.replace(match, '[', '')
17 match = string.replace(match, ']', '')
21 if match.find("'") > -1:
22 match = string.replace(match, "'", '')
25 elif match.find('"') > -1:
26 match = string.replace(match, '"', '')
30 return ldel + match + rdel
33 def fix_element(element):
34 # Much of the positioning here is important, e.g. if you do element.find('if ') before you do
35 # element.find('endif'), then you may get some multiply-replaced delimiters
37 if element.find('endif') > -1:
38 element = ldelim + '/if' + rdelim
41 if element.find('if ') > -1:
42 element = string.replace(element, '{{ if', ldelim + 'if')
43 element = string.replace(element, '{{if', ldelim + 'if')
44 element = string.replace(element, ' }}', rdelim)
45 element = string.replace(element, '}}', rdelim)
48 if element.find('else') > -1:
49 element = ldelim + 'else' + rdelim
52 if element.find('endfor') > -1:
53 element = ldelim + '/foreach' + rdelim
56 if element.find('for ') > -1:
57 element = string.replace(element, '{{ for ', ldelim + 'foreach ')
58 element = string.replace(element, '{{for ', ldelim + 'foreach ')
59 element = string.replace(element, ' }}', rdelim)
60 element = string.replace(element, '}}', rdelim)
63 if element.find('endinc') > -1:
67 if element.find('inc ') > -1:
68 parts = element.split(' ')
69 element = ldelim + 'include file="'
71 # We need to find the file name. It'll either be in parts[1] if the element was written as {{ inc file.tpl }}
72 # or it'll be in parts[2] if the element was written as {{inc file.tpl}}
73 if parts[0].find('inc') > -1:
78 if parts[first+1][0] == '$':
79 # This takes care of elements where the filename is a variable, e.g. {{ inc $file }}
80 element += 'file:' + ldelim + parts[first+1].rstrip('}') + rdelim
82 # This takes care of elements where the filename is a path, e.g. {{ inc file.tpl }}
83 element += parts[first+1].rstrip('}')
87 if len(parts) > first + 1 and parts[first+2] == 'with':
88 # Take care of variable substitutions, e.g. {{ inc file.tpl with $var=this_var }}
89 element += ' ' + parts[first+3].rstrip('}')[1:]
95 def convert(filename, tofilename, php_tpl):
99 brack_pos = line.find('{{')
102 # If php_tpl is True, this script will only convert variables in quotes, like '$variable'
103 # or "$variable". This is for .tpl files that produce PHP scripts, where you don't want
104 # all the PHP variables converted into Smarty variables
105 pattern1 = re.compile(r"""
106 ([\'\"]\$\[[a-zA-Z]\w*
113 pattern2 = re.compile(r"""
122 # Compile the pattern for bracket-style variables, e.g. $[variable.key|filter:arg1:arg2|filter2:arg1:arg2]
123 # Note that dashes are only allowed in array keys if the key doesn't start
124 # with a number, e.g. $[variable.key-id] is ok but $[variable.12-id] isn't
126 # Doesn't currently process the argument position key 'x', i.e. filter:arg1:x:arg2 doesn't get
127 # changed to arg1|filter:variable:arg2 like Smarty requires
129 # Filter arguments can be variables, e.g. $variable, but currently can't have array keys with dashes
130 # like filter:$variable.key-name
131 pattern1 = re.compile(r"""
140 # Compile the pattern for normal style variables, e.g. $variable.key
141 pattern2 = re.compile(r"""
150 while brack_pos > -1:
151 if brack_pos > st_pos:
152 line_segment = line[st_pos:brack_pos]
153 line_segment = pattern2.sub(fToSmarty, line_segment)
154 newline += pattern1.sub(fToSmarty, line_segment)
156 end_brack_pos = line.find('}}', brack_pos)
157 if end_brack_pos < 0:
158 print "Error: no matching bracket found"
160 newline += fix_element(line[brack_pos:end_brack_pos + 2])
161 st_pos = end_brack_pos + 2
163 brack_pos = line.find('{{', st_pos)
165 line_segment = line[st_pos:]
166 line_segment = pattern2.sub(fToSmarty, line_segment)
167 newline += pattern1.sub(fToSmarty, line_segment)
168 newline = newline.replace("{#", ldelim + "*")
169 newline = newline.replace("#}", "*" + rdelim)
170 tofilename.write(newline)
173 path = raw_input('Path to template folder to convert: ')
177 outpath = path + 'smarty3/'
179 if not os.path.exists(outpath):
182 files = os.listdir(path)
184 if a_file == 'htconfig.tpl':
189 filename = os.path.join(path,a_file)
190 ext = a_file.split('.')[-1]
191 if os.path.isfile(filename) and ext == 'tpl':
192 with open(filename, 'r') as f:
193 newfilename = os.path.join(outpath,a_file)
194 with open(newfilename, 'w') as outf:
195 print "Converting " + filename + " to " + newfilename
196 convert(f, outf, php_tpl)