]> git.mxchange.org Git - friendica.git/blobdiff - mods/friendica-to-smarty-tpl.py
Merge pull request #587 from oohlaf/fixes
[friendica.git] / mods / friendica-to-smarty-tpl.py
index 6037d04ea21cd3d2ea9ea9f646772a4258b735f4..8149051caec76896410afb141875322b386467aa 100755 (executable)
@@ -1,14 +1,17 @@
 #!/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':
@@ -77,7 +80,7 @@ def fix_element(element):
 
                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('}') 
@@ -93,6 +96,10 @@ def fix_element(element):
 
 
 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
@@ -170,7 +177,39 @@ def convert(filename, tofilename, php_tpl):
                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 + '/'
 
@@ -189,10 +228,14 @@ for a_file in files:
        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()