]> git.mxchange.org Git - friendica.git/blob - mods/friendica-to-smarty-tpl.py
use Python 2.4 compatible script; update Smarty templates
[friendica.git] / mods / friendica-to-smarty-tpl.py
1 #!/usr/bin/python
2 #
3 # Script to convert Friendica internal template files into Smarty template files
4 # Copyright 2012 Zach Prezkuta
5 # Licensed under GPL v3
6
7 import os, re, string
8
9 ldelim = '{{'
10 rdelim = '}}'
11
12 def fToSmarty(matches):
13         match = matches.group(0)
14         if match == '$j':
15                 return match
16         match = string.replace(match, '[', '')
17         match = string.replace(match, ']', '')
18
19         ldel = ldelim
20         rdel = rdelim
21         if match.find("'") > -1:
22                 match = string.replace(match, "'", '')
23                 ldel = "'" + ldel
24                 rdel = rdel + "'"
25         elif match.find('"') > -1:
26                 match = string.replace(match, '"', '')
27                 ldel = '"' + ldel
28                 rdel = rdel + '"'
29
30         return ldel + match + rdel
31
32
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
36
37         if element.find('endif') > -1:
38                 element = ldelim + '/if' + rdelim
39                 return element
40
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)
46                 return element
47
48         if element.find('else') > -1:
49                 element = ldelim + 'else' + rdelim
50                 return element
51
52         if element.find('endfor') > -1:
53                 element = ldelim + '/foreach' + rdelim
54                 return element
55
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)
61                 return element
62
63         if element.find('endinc') > -1:
64                 element = ''
65                 return element
66
67         if element.find('inc ') > -1:
68                 parts = element.split(' ')
69                 element = ldelim + 'include file="'
70
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:
74                         first = 0
75                 else:
76                         first = 1
77
78                 if parts[first+1][0] == '$':
79                         # This takes care of elements where the filename is a variable, e.g. {{ inc $file }}
80                         element += ldelim + parts[first+1].rstrip('}') + rdelim
81                 else:
82                         # This takes care of elements where the filename is a path, e.g. {{ inc file.tpl }}
83                         element += parts[first+1].rstrip('}') 
84
85                 element += '"'
86
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:]
90
91                 element += rdelim
92                 return element
93
94
95 def convert(filename, tofilename, php_tpl):
96         for line in filename:
97                 newline = ''
98                 st_pos = 0
99                 brack_pos = line.find('{{')
100
101                 if php_tpl:
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*
107 (\.
108 (\d+|[a-zA-Z][\w-]*)
109 )*
110 (\|[\w\$:\.]*)*
111 \][\'\"])
112 """, re.VERBOSE)
113                         pattern2 = re.compile(r"""
114 ([\'\"]\$[a-zA-Z]\w*
115 (\.
116 (\d+|[a-zA-Z][\w-]*)
117 )*
118 (\|[\w\$:\.]*)*
119 [\'\"])
120 """, re.VERBOSE)
121                 else:
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
125                         #
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
128                         #
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"""
132 (\$\[[a-zA-Z]\w*
133 (\.
134 (\d+|[a-zA-Z][\w-]*)
135 )*
136 (\|[\w\$:\.]*)*
137 \])
138 """, re.VERBOSE)
139
140                         # Compile the pattern for normal style variables, e.g. $variable.key
141                         pattern2 = re.compile(r"""
142 (\$[a-zA-Z]\w*
143 (\.
144 (\d+|[a-zA-Z][\w-]*)
145 )*
146 (\|[\w\$:\.]*)*
147 )
148 """, re.VERBOSE)
149
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)
155
156                         end_brack_pos = line.find('}}', brack_pos)
157                         if end_brack_pos < 0:
158                                 print "Error: no matching bracket found"
159
160                         newline += fix_element(line[brack_pos:end_brack_pos + 2])
161                         st_pos = end_brack_pos + 2
162
163                         brack_pos = line.find('{{', st_pos)
164
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)
171
172
173 path = raw_input('Path to template folder to convert: ')
174 if path[-1:] != '/':
175         path = path + '/'
176
177 outpath = path + 'smarty3/'
178
179 if not os.path.exists(outpath):
180         os.makedirs(outpath)
181
182 files = os.listdir(path)
183 for a_file in files:
184         if a_file == 'htconfig.tpl':
185                 php_tpl = True
186         else:
187                 php_tpl = False
188
189         filename = os.path.join(path,a_file)
190         ext = a_file.split('.')[-1]
191         if os.path.isfile(filename) and ext == 'tpl':
192                 f = open(filename, 'r')
193
194                 newfilename = os.path.join(outpath,a_file)
195                 outf = open(newfilename, 'w')
196
197                 print "Converting " + filename + " to " + newfilename
198                 convert(f, outf, php_tpl)
199
200                 outf.close()
201                 f.close()
202