3 # Script to convert Friendica internal template files into Smarty template files
4 # Copyright 2013 Zach Prezkuta
5 # Licensed under GPL v3
13 def fToSmarty(matches):
14 match = matches.group(0)
17 match = string.replace(match, '[', '')
18 match = string.replace(match, ']', '')
22 if match.find("'") > -1:
23 match = string.replace(match, "'", '')
26 elif match.find('"') > -1:
27 match = string.replace(match, '"', '')
31 return ldel + match + rdel
34 def fix_element(element):
35 # Much of the positioning here is important, e.g. if you do element.find('if ') before you do
36 # element.find('endif'), then you may get some multiply-replaced delimiters
38 if element.find('endif') > -1:
39 element = ldelim + '/if' + rdelim
42 if element.find('if ') > -1:
43 element = string.replace(element, '{{ if', ldelim + 'if')
44 element = string.replace(element, '{{if', ldelim + 'if')
45 element = string.replace(element, ' }}', rdelim)
46 element = string.replace(element, '}}', rdelim)
49 if element.find('else') > -1:
50 element = ldelim + 'else' + rdelim
53 if element.find('endfor') > -1:
54 element = ldelim + '/foreach' + rdelim
57 if element.find('for ') > -1:
58 element = string.replace(element, '{{ for ', ldelim + 'foreach ')
59 element = string.replace(element, '{{for ', ldelim + 'foreach ')
60 element = string.replace(element, ' }}', rdelim)
61 element = string.replace(element, '}}', rdelim)
64 if element.find('endinc') > -1:
68 if element.find('inc ') > -1:
69 parts = element.split(' ')
70 element = ldelim + 'include file="'
72 # We need to find the file name. It'll either be in parts[1] if the element was written as {{ inc file.tpl }}
73 # or it'll be in parts[2] if the element was written as {{inc file.tpl}}
74 if parts[0].find('inc') > -1:
79 if parts[first+1][0] == '$':
80 # This takes care of elements where the filename is a variable, e.g. {{ inc $file }}
81 element += ldelim + parts[first+1].rstrip('}') + rdelim
83 # This takes care of elements where the filename is a path, e.g. {{ inc file.tpl }}
84 element += parts[first+1].rstrip('}')
88 if len(parts) > first + 1 and parts[first+2] == 'with':
89 # Take care of variable substitutions, e.g. {{ inc file.tpl with $var=this_var }}
90 element += ' ' + parts[first+3].rstrip('}')[1:]
96 def convert(filename, tofilename, php_tpl):
97 header = ldelim + "*\n *\tAUTOMATICALLY GENERATED TEMPLATE\n *\tDO NOT EDIT THIS FILE, CHANGES WILL BE OVERWRITTEN\n *\n *" + rdelim + "\n"
98 tofilename.write(header)
100 for line in filename:
103 brack_pos = line.find('{{')
106 # If php_tpl is True, this script will only convert variables in quotes, like '$variable'
107 # or "$variable". This is for .tpl files that produce PHP scripts, where you don't want
108 # all the PHP variables converted into Smarty variables
109 pattern1 = re.compile(r"""
110 ([\'\"]\$\[[a-zA-Z]\w*
117 pattern2 = re.compile(r"""
126 # Compile the pattern for bracket-style variables, e.g. $[variable.key|filter:arg1:arg2|filter2:arg1:arg2]
127 # Note that dashes are only allowed in array keys if the key doesn't start
128 # with a number, e.g. $[variable.key-id] is ok but $[variable.12-id] isn't
130 # Doesn't currently process the argument position key 'x', i.e. filter:arg1:x:arg2 doesn't get
131 # changed to arg1|filter:variable:arg2 like Smarty requires
133 # Filter arguments can be variables, e.g. $variable, but currently can't have array keys with dashes
134 # like filter:$variable.key-name
135 pattern1 = re.compile(r"""
144 # Compile the pattern for normal style variables, e.g. $variable.key
145 pattern2 = re.compile(r"""
154 while brack_pos > -1:
155 if brack_pos > st_pos:
156 line_segment = line[st_pos:brack_pos]
157 line_segment = pattern2.sub(fToSmarty, line_segment)
158 newline += pattern1.sub(fToSmarty, line_segment)
160 end_brack_pos = line.find('}}', brack_pos)
161 if end_brack_pos < 0:
162 print "Error: no matching bracket found"
164 newline += fix_element(line[brack_pos:end_brack_pos + 2])
165 st_pos = end_brack_pos + 2
167 brack_pos = line.find('{{', st_pos)
169 line_segment = line[st_pos:]
170 line_segment = pattern2.sub(fToSmarty, line_segment)
171 newline += pattern1.sub(fToSmarty, line_segment)
172 newline = newline.replace("{#", ldelim + "*")
173 newline = newline.replace("#}", "*" + rdelim)
174 tofilename.write(newline)
179 print "\t" + pname + " -h\n\n\t\t\tShow this help screen\n"
180 print "\t" + pname + " -p directory\n\n\t\t\tConvert all .tpl files in directory to\n\t\t\tSmarty templates in directory/smarty3/\n"
181 print "\t" + pname + "\n\n\t\t\tInteractive mode\n"
193 opts, args = getopt.getopt(sys.argv[1:], "hp:")
194 for opt, arg in opts:
200 except getopt.GetoptError:
206 path = raw_input('Path to template folder to convert: ')
211 outpath = path + 'smarty3/'
213 if not os.path.exists(outpath):
216 files = os.listdir(path)
218 if a_file == 'htconfig.tpl':
223 filename = os.path.join(path,a_file)
224 ext = a_file.split('.')[-1]
225 if os.path.isfile(filename) and ext == 'tpl':
226 f = open(filename, 'r')
228 newfilename = os.path.join(outpath,a_file)
229 outf = open(newfilename, 'w')
231 print "Converting " + filename + " to " + newfilename
232 convert(f, outf, php_tpl)