3 # Script to convert Friendica internal template files into Smarty template files
4 # Copyright 2013 Zach Prezkuta
5 # Licensed under GPL v3
15 def fToSmarty(matches):
16 match = matches.group(0)
19 match = string.replace(match, '[', '')
20 match = string.replace(match, ']', '')
24 if match.find("'") > -1:
25 match = string.replace(match, "'", '')
28 elif match.find('"') > -1:
29 match = string.replace(match, '"', '')
33 return ldel + match + rdel
36 def fix_element(element):
37 # Much of the positioning here is important, e.g. if you do element.find('if ') before you do
38 # element.find('endif'), then you may get some multiply-replaced delimiters
40 if element.find('endif') > -1:
41 element = ldelim + '/if' + rdelim
44 if element.find('if ') > -1:
45 element = string.replace(element, '{{ if', ldelim + 'if')
46 element = string.replace(element, '{{if', ldelim + 'if')
47 element = string.replace(element, ' }}', rdelim)
48 element = string.replace(element, '}}', rdelim)
51 if element.find('else') > -1:
52 element = ldelim + 'else' + rdelim
55 if element.find('endfor') > -1:
56 element = ldelim + '/foreach' + rdelim
59 if element.find('for ') > -1:
60 element = string.replace(element, '{{ for ', ldelim + 'foreach ')
61 element = string.replace(element, '{{for ', ldelim + 'foreach ')
62 element = string.replace(element, ' }}', rdelim)
63 element = string.replace(element, '}}', rdelim)
66 if element.find('endinc') > -1:
70 if element.find('inc ') > -1:
71 parts = element.split(' ')
72 element = ldelim + 'include file="'
74 # We need to find the file name. It'll either be in parts[1] if the element was written as {{ inc file.tpl }}
75 # or it'll be in parts[2] if the element was written as {{inc file.tpl}}
76 if parts[0].find('inc') > -1:
81 if parts[first+1][0] == '$':
82 # This takes care of elements where the filename is a variable, e.g. {{ inc $file }}
83 element += ldelim + parts[first+1].rstrip('}') + rdelim
85 # This takes care of elements where the filename is a path, e.g. {{ inc file.tpl }}
86 element += parts[first+1].rstrip('}')
90 if len(parts) > first + 1 and parts[first+2] == 'with':
91 # Take care of variable substitutions, e.g. {{ inc file.tpl with $var=this_var }}
92 element += ' ' + parts[first+3].rstrip('}')[1:]
98 def convert(filename, tofilename, php_tpl):
100 header = ldelim + "*\n *\tAUTOMATICALLY GENERATED TEMPLATE\n *\tDO NOT EDIT THIS FILE, CHANGES WILL BE OVERWRITTEN\n *\n *" + rdelim + "\n"
101 tofilename.write(header)
103 for line in filename:
106 brack_pos = line.find('{{')
109 # If php_tpl is True, this script will only convert variables in quotes, like '$variable'
110 # or "$variable". This is for .tpl files that produce PHP scripts, where you don't want
111 # all the PHP variables converted into Smarty variables
112 pattern1 = re.compile(r"""
113 ([\'\"]\$\[[a-zA-Z]\w*
120 pattern2 = re.compile(r"""
129 # Compile the pattern for bracket-style variables, e.g. $[variable.key|filter:arg1:arg2|filter2:arg1:arg2]
130 # Note that dashes are only allowed in array keys if the key doesn't start
131 # with a number, e.g. $[variable.key-id] is ok but $[variable.12-id] isn't
133 # Doesn't currently process the argument position key 'x', i.e. filter:arg1:x:arg2 doesn't get
134 # changed to arg1|filter:variable:arg2 like Smarty requires
136 # Filter arguments can be variables, e.g. $variable, but currently can't have array keys with dashes
137 # like filter:$variable.key-name
138 pattern1 = re.compile(r"""
147 # Compile the pattern for normal style variables, e.g. $variable.key
148 pattern2 = re.compile(r"""
157 while brack_pos > -1:
158 if brack_pos > st_pos:
159 line_segment = line[st_pos:brack_pos]
160 line_segment = pattern2.sub(fToSmarty, line_segment)
161 newline += pattern1.sub(fToSmarty, line_segment)
163 end_brack_pos = line.find('}}', brack_pos)
164 if end_brack_pos < 0:
165 print "Error: no matching bracket found"
167 newline += fix_element(line[brack_pos:end_brack_pos + 2])
168 st_pos = end_brack_pos + 2
170 brack_pos = line.find('{{', st_pos)
172 line_segment = line[st_pos:]
173 line_segment = pattern2.sub(fToSmarty, line_segment)
174 newline += pattern1.sub(fToSmarty, line_segment)
175 newline = newline.replace("{#", ldelim + "*")
176 newline = newline.replace("#}", "*" + rdelim)
177 tofilename.write(newline)
182 print "\t" + pname + " -h\n\n\t\t\tShow this help screen\n"
183 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"
184 print "\t" + pname + "\n\n\t\t\tInteractive mode\n"
196 opts, args = getopt.getopt(sys.argv[1:], "hp:", ['no-header'])
197 for opt, arg in opts:
203 elif opt == '--no-header':
205 except getopt.GetoptError:
211 path = raw_input('Path to template folder to convert: ')
216 outpath = path + 'smarty3/'
218 if not os.path.exists(outpath):
221 files = os.listdir(path)
223 if a_file == 'htconfig.tpl':
228 filename = os.path.join(path,a_file)
229 ext = a_file.split('.')[-1]
230 if os.path.isfile(filename) and ext == 'tpl':
231 f = open(filename, 'r')
233 newfilename = os.path.join(outpath,a_file)
234 outf = open(newfilename, 'w')
236 print "Converting " + filename + " to " + newfilename
237 convert(f, outf, php_tpl)