#!/usr/bin/python
#
# Script to convert Friendica internal template files into Smarty template files
-# Copyright 2012 Zach Prezkuta
+# Copyright 2013 Zach Prezkuta
# Licensed under GPL v3
import os, re, string
+import sys, getopt
ldelim = '{{'
rdelim = '}}'
+addheader = True
+
def fToSmarty(matches):
match = matches.group(0)
if match == '$j':
if parts[first+1][0] == '$':
# This takes care of elements where the filename is a variable, e.g. {{ inc $file }}
- element += 'file:' + ldelim + parts[first+1].rstrip('}') + rdelim
+ element += ldelim + parts[first+1].rstrip('}') + rdelim
else:
# This takes care of elements where the filename is a path, e.g. {{ inc file.tpl }}
element += parts[first+1].rstrip('}')
def convert(filename, tofilename, php_tpl):
+ if addheader:
+ header = ldelim + "*\n *\tAUTOMATICALLY GENERATED TEMPLATE\n *\tDO NOT EDIT THIS FILE, CHANGES WILL BE OVERWRITTEN\n *\n *" + rdelim + "\n"
+ tofilename.write(header)
+
for line in filename:
newline = ''
st_pos = 0
tofilename.write(newline)
-path = raw_input('Path to template folder to convert: ')
+def help(pname):
+ print "\nUsage:"
+ print "\t" + pname + " -h\n\n\t\t\tShow this help screen\n"
+ 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"
+ print "\t" + pname + "\n\n\t\t\tInteractive mode\n"
+
+
+
+
+#
+# Main script
+#
+
+path = ''
+
+try:
+ opts, args = getopt.getopt(sys.argv[1:], "hp:", ['no-header'])
+ for opt, arg in opts:
+ if opt == '-h':
+ help(sys.argv[0])
+ sys.exit()
+ elif opt == '-p':
+ path = arg
+ elif opt == '--no-header':
+ addheader = False
+except getopt.GetoptError:
+ help(sys.argv[0])
+ sys.exit(2)
+
+
+if path == '':
+ path = raw_input('Path to template folder to convert: ')
+
if path[-1:] != '/':
path = path + '/'
filename = os.path.join(path,a_file)
ext = a_file.split('.')[-1]
if os.path.isfile(filename) and ext == 'tpl':
- with open(filename, 'r') as f:
- newfilename = os.path.join(outpath,a_file)
- with open(newfilename, 'w') as outf:
- print "Converting " + filename + " to " + newfilename
- convert(f, outf, php_tpl)
+ f = open(filename, 'r')
+
+ newfilename = os.path.join(outpath,a_file)
+ outf = open(newfilename, 'w')
+
+ print "Converting " + filename + " to " + newfilename
+ convert(f, outf, php_tpl)
+ outf.close()
+ f.close()