[rkward-cvs] SF.net SVN: rkward:[2680] trunk/rkward/rkward/plugins/php2js.js

tfry at users.sourceforge.net tfry at users.sourceforge.net
Sat Oct 3 15:46:33 UTC 2009


Revision: 2680
          http://rkward.svn.sourceforge.net/rkward/?rev=2680&view=rev
Author:   tfry
Date:     2009-10-03 15:46:33 +0000 (Sat, 03 Oct 2009)

Log Message:
-----------
v2 of the php2js script simplifies echo-statements and empty functions.

Modified Paths:
--------------
    trunk/rkward/rkward/plugins/php2js.js

Modified: trunk/rkward/rkward/plugins/php2js.js
===================================================================
--- trunk/rkward/rkward/plugins/php2js.js	2009-10-03 11:39:22 UTC (rev 2679)
+++ trunk/rkward/rkward/plugins/php2js.js	2009-10-03 15:46:33 UTC (rev 2680)
@@ -182,10 +182,98 @@
 }
 
 
+// this function is meant to be run in step 2 (i.e. on already js code)
+function mergeEchos (line) {
+	var output_save = output;
+	output = "";
 
+	var directly_after_echo = false;
+	for (var i = 0; i < line.length; ++i) {
+		var c = line.charAt (i);
 
+		if (!directly_after_echo) {
+			if (c == "'") {
+				// hack: skips js quotes, too
+				i += convertPHPQuote (line.substr (i), "'");
+				continue;
+			}
+		}
 
+		if (line.indexOf ("echo", i) == i) {
+			i += 4;
+			var output_save_2 = output;
+			output = "";
+			while (line.charAt (i) != ";") {
+				if (line.charAt (i) == "'") {
+					i += convertPHPQuote (line.substr (i), "'");
+				} else {
+					output += line.charAt (i);
+					++i;
+				}
+			}
 
+			var fragment = output;
+			output = output_save_2;
+			if (i >= line.length) {
+				print ("Strange echo statement. Please check by hand.");
+				continue;
+			}
+			fragment = fragment.replace (/^\s*/, "");
+			fragment = fragment.replace (/^\(/, "");
+			fragment = fragment.replace (/\)$/, "");
+
+			if (!directly_after_echo) {
+				output += "echo (";
+			} else {
+				output += " + ";
+			}
+			output += fragment;
+
+			directly_after_echo = true;
+		} else {
+			if (directly_after_echo) {
+				if (c != " ") {
+					output += "); " + c;
+					directly_after_echo = false;
+				}
+			} else {
+				output += c;
+			}
+		}
+	}
+
+	if (directly_after_echo) {
+		output += ");";
+	}
+
+	var ret = output;
+	output = output_save;
+	return (ret);
+}
+
+function postProcess (input) {
+	var lines = input.split ("\n");
+	var olines = new Array ();
+
+	for (var i = 0; i < lines.length; ++i) {
+		if (lines[i].search (/^function /) >= 0) {
+			olines.push (lines[i]);
+			while (lines[++i].search (/^\s*$/) >= 0) {
+				// skip empty line
+			}
+			if (lines[i] == "}") {
+				// kill entire function
+				olines.pop ();
+				continue;
+			}
+		}
+//		olines.push (lines[i]);
+		olines.push (mergeEchos (lines[i]));
+	}
+
+	return (olines.join ("\n"));
+}
+
 // the output buffer
 var output = "";
 // list of global vars
@@ -195,6 +283,7 @@
 
 // main conversion step
 convertTopLevel (file);
+output = postProcess (output);
 
 // add global var declarations
 globals.sort ();


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list