[texworks] Scripting: Libraries: FYI - compilation of equivalent PHP functions for Qt/JavaScript

Paul A Norman paul.a.norman at gmail.com
Sun Oct 31 08:29:45 CET 2010


Ok,

I've worked through a proof of concept and this looks very feasible,
in fact I'm using it in every day work now.

In it I have used a work around in the meanwhile for loading ECMA libraries.

And wrapped a number of properties and functions in an object called $Tw

The php_js functions are in an object called $PhpJs detailed in
phpjs.namespaced.js from their web site (
http://phpjs.org/packages/configure ).  There would be some editing of
phpjs.namespaced.js needed longterm, but it provides very useful
immediate functionality.

Although php_js does not require it for its own use, I am temporarily
relying on php to give me file read access in loading the needed
JavaScript modules as shown here. This gets the first set of modules
and top level functions in, after that all that would be required for
extra JS libraries is       function module_load(module_name) ...

////////// Begin Preamble

/*  Creates $Tw object with properties and helper functions
    and a function in the current scope:  module_load()

    And sets up  $phpjs = new PHP_JS();

    See notes in scripts/mod_general.js    */

   var scriptName = __FILE__ ;
   var scriptRoot = __FILE__.substr(0,__FILE__.lastIndexOf("/")) ;
   var scriptRoot = scriptRoot.substr(0,scriptRoot.lastIndexOf("/")+1) ;
   var loadFile =  "\"" + scriptRoot + 'mod_loadFile.php\" mod_general.js';
// Non-windows OSes won't need the cmd /c
   var JavaScript = TW.app.system('cmd /c php ' +  loadFile);
   eval(JavaScript);  /*  $Tw and $phpjs objects created  */

////////// END Preamble

Here are some example calls (below) in a Tw QtScript and results,
moving from what we'd expect EMCA to do any way, to extras.

If any one wants to look through this please drop me an email or a
note here, and I'll try and put something of the current state of it
up on the web for download. (Not sure how many are doing Scripting at
present.)

Paul

 $Tw.alert($PhpJs.ucwords("hello this should appear in title case"));

 http://dl.dropbox.com/u/13401476/general_images/texWorks_%24Tw.alert%28%24PhpJs.str_replace%28.jpg


 $Tw.alert($PhpJs.sort("hello this should appear in title
case".split(" ")).join(" "));

http://dl.dropbox.com/u/13401476/general_images/texWorks_%24Tw.alert%28%24PhpJs.sort%28.jpg


 $Tw.alert($PhpJs.str_replace(["this","title"],["Brian","lower"],"hello
this should appear in title case"));

http://dl.dropbox.com/u/13401476/general_images/texWorks_%24Tw.alert%28%24PhpJs.str_replace.jpg


 $Tw.alert( $Tw.file_get_contents("c:/windows/php.ini") );

http://dl.dropbox.com/u/13401476/general_images/texWorks_%24Tw.alert%28%20%24Tw.file_get_contents%28.jpg



On 22 October 2010 17:43, Paul A Norman <paul.a.norman at gmail.com> wrote:
> Hi,  FYI,
>
>      "Use PHP functions in JavaScript"
>
>  http://phpjs.org/pages/home
>
> Some very clever fellows have ported a considerable number of string
> handling, array handling and other potentially very useful functions
> across from php to EMCA style scripting, and made them all work as per
> the PHP manual (documentation as it already exists).
>
>  http://phpjs.org/functions/index
>
> (I was thinking these may be useful for Qt scripting? There are of
> course many things that would never be used - and perhaps a core set
> of things we could tie into the TwApi perhaps thorugh a wrapper? like
> a specialized  file_get_contents()  etc ...
>
> As said there would be many things that are too web orientated and
> would never be used. And some stuff that wold be directly useful now.)
>
> Even more cleverly they have made their 'library' customisable
> pre-download so that a script-developer could define their own
> package(s) as needed, with only QtScript relevant functions and
> dependencies in it.
>
>  http://phpjs.org/packages/configure
>
> Food for thought.
>
> Paul
> ---- Quote from web site: ----
>
> "Use PHP functions in JavaScript"
>
> "php.js
>
> "php.js is an open source project that brings high-level PHP functions
> to low-level JavaScript platforms such as web browsers, browser
> extensions ( Mozilla/Firefox, Chrome ), AIR, and SSJS engines like V8
> ( node.js, v8cgi ), Rhino, and SpiderMonkey ( CouchDB )
>
> "If you want to perform high-level operations on these platforms, you
> probably need to write JS that combines its lower-level functions and
> build it up until you have something useful like: strip_tags(),
> strtotime(), number_format(), wordwrap().
>
> "That's what we are doing for you.
>
> "Pure JavaScript so no additional components required
>



More information about the texworks mailing list