Skip to content

Commit

Permalink
v. 0.8-alpha,
Browse files Browse the repository at this point in the history
* enable custom dynamic expressions in config with Xpresion engine
* fix Custom_Parser.js variable undefined
* fix Custom_Parser.php, unquoted value was not trimmed
* refactoring, update doc plugins to use custom RegExps
  • Loading branch information
Nikos M committed Apr 13, 2015
1 parent af81fa5 commit ff4df33
Show file tree
Hide file tree
Showing 17 changed files with 853 additions and 197 deletions.
76 changes: 70 additions & 6 deletions Beeld.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* https://github.com/foo123/Beeld
*
* A scriptable and configurable source code builder framework in Node/PHP/Python
* @version: 0.7.1
* @version: 0.8-alpha
*
**/
!function (root, moduleName, moduleDefinition) {
Expand Down Expand Up @@ -96,7 +96,7 @@

// needed variables
BEELD_FILE, BEELD_ROOT, BEELD_INCLUDES, BEELD_PARSERS, BEELD_COMPILERS, BEELD_TEMPLATES, BEELD_PLUGINS,
TPLS = { }, PublishSubscribe, /*List, Map,*/ OrderedMap,
TPLS = { }, PublishSubscribe, Xpresion, /*List, Map,*/ OrderedMap,
BeeldParser, BeeldCompiler, Beeld
;

Expand All @@ -109,6 +109,7 @@
BEELD_PLUGINS = join_path(BEELD_ROOT, "plugins") + '/';

PublishSubscribe = require(BEELD_INCLUDES + 'PublishSubscribe.js');
Xpresion = require(BEELD_INCLUDES + 'Xpresion.js');

//List = Array;
//Map = Object;
Expand Down Expand Up @@ -184,6 +185,37 @@
return out;
}

function regex( rex, evt )
{
var settings = evt.data.data.config.settings;
if (rex instanceof RegExp) return rex;
else if (settings.RegExp && rex.substr && startsWith(rex, settings.RegExp)) return new RegExp(rex.substr(settings.RegExp.length));
return false;
}

function xpresion( xpr, evt )
{
var settings = evt.data.data.config.settings;
if ( settings.Xpresion )
{
if ( xpr instanceof Xpresion )
{
return xpr;
}
else if ( xpr.substr && startsWith(xpr, settings.Xpresion) )
{
xpr = new Xpresion( xpr.substr(settings.Xpresion.length) );
return xpr;
}
}
return xpr;
}

function evaluate( xpr, data )
{
return xpr instanceof Xpresion ? xpr.evaluate(data) : xpr;
}

//
// adapted from node-commander package
// https://github.com/visionmedia/commander.js/
Expand Down Expand Up @@ -318,6 +350,11 @@
else return file;
}

function read_file( filename, basePath )
{
return read( get_real_path( filename, basePath ) );
}

BeeldParser = function(path, class_name, name) {
var self = this;
self.path = path;
Expand Down Expand Up @@ -406,7 +443,7 @@
,'action_out': Beeld.Actions.action_out
};
};
Beeld.VERSION = "0.7.1";
Beeld.VERSION = "0.8-alpha";

Beeld.OrderedMap = function( om ){
return new OrderedMap(om);
Expand All @@ -422,6 +459,8 @@

Beeld.Obj = PublishSubscribe.Data;

Beeld.Xpresion = Xpresion;

Beeld.Utils = {
startsWith: startsWith,
multi_replace: multi_replace,
Expand All @@ -432,7 +471,10 @@
join_path: join_path,
tmpfile: tmpfile,
get_real_path: get_real_path,
extend: extend
extend: extend,
regex: regex,
xpresion: xpresion,
evaluate: evaluate
};

//
Expand All @@ -457,6 +499,15 @@
// aliases
Beeld.Parsers[".yaml"] = Beeld.Parsers[".yml"];
Beeld.Parsers["*"] = Beeld.Parsers[".custom"];
Xpresion.defaultConfiguration();
Xpresion.defFunc({
'file': Xpresion.Func('file', 'Fn.file($0)'),
'tpl': Xpresion.Func('tpl', 'Fn.tpl($0)')
});
Xpresion.defRuntimeFunc({
'file': read_file,
'tpl': get_tpl
});

//
// Beeld default actions
Expand Down Expand Up @@ -704,12 +755,15 @@
if ( current.action_cfg )
{
var replace = Beeld.OrderedMap(current.action_cfg), rep,
hasHeader = !!(data.header && data.header.length)
hasHeader = !!(data.header && data.header.length),
xpresion_data = {}
;
// ordered map
while ( replace.hasNext( ) )
{
rep = replace.getNext();
rep[1] = Beeld.Utils.xpresion(rep[1], evt); // parse xpresion if any
rep[1] = Beeld.Utils.evaluate(rep[1], xpresion_data);
data.src = data.src.split(rep[0]).join(rep[1]);
if ( hasHeader ) data.header = data.header.split(rep[0]).join(rep[1]);
}
Expand Down Expand Up @@ -1040,11 +1094,21 @@
});
params.data = Beeld.Obj();
params.current = Beeld.Obj();
params.config = config;

if ( config[HAS]('settings') )
{
if ( !config['settings'][HAS]('RegExp') ) config['settings']['RegExp'] = false;
if ( !config['settings'][HAS]('Xpresion') ) config['settings']['Xpresion'] = false;
}
else
{
config['settings'] = {'RegExp':false, 'Xpresion':false};
}
if ( config[HAS]('plugins') )
{
this.loadPlugins(config.plugins, params.options.basePath);
}
params.config = config;
return params;
};

Expand Down
70 changes: 66 additions & 4 deletions Beeld.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://github.com/foo123/Beeld
*
* A scriptable and configurable source code builder framework in Node/PHP/Python
* @version: 0.7.1
* @version: 0.8-alpha
*
**/
if (!class_exists('Beeld'))
Expand All @@ -22,6 +22,7 @@
define('BEELD_PLUGINS', BEELD_ROOT . 'plugins' . DIRECTORY_SEPARATOR);

require(BEELD_INCLUDES . 'PublishSubscribe.php');
require(BEELD_INCLUDES . 'Xpresion.php');

//
// beeld utils
Expand Down Expand Up @@ -154,6 +155,11 @@ public static function get_tpl( $id, $enc=null )
return self::$TPLS[$tpl_id];
}

public static function read_file($filename, $basePath='')
{
return file_get_contents( self::get_real_path( $filename, $basePath ) );
}

public static function multi_replace($tpl, $reps)
{
$out = $tpl;
Expand All @@ -164,6 +170,37 @@ public static function multi_replace($tpl, $reps)
return $out;
}

// http://stackoverflow.com/questions/10778318/test-if-a-string-is-regex
public static function regex($rex, $evt)
{
$settings = $evt->data->data->config['settings'];
if ( $settings['RegExp'] && is_string($rex) && self::startsWith($rex, $settings['RegExp']) )
return '/' . str_replace('/', '\\/', substr($rex, strlen($settings['RegExp']))) . '/';
return false;
}

public static function xpresion($xpr, $evt)
{
$settings = $evt->data->data->config['settings'];
if ( $settings['Xpresion'] )
{
if ( $xpr instanceof Xpresion )
{
return $xpr;
}
else if ( is_string($xpr) && self::startsWith($xpr, $settings['Xpresion']) )
{
$xpr = new Xpresion( substr($xpr, strlen($settings['Xpresion'])) );
return $xpr;
}
}
return $xpr;
}

public static function evaluate($xpr, $data) {
return $xpr instanceof Xpresion ? $xpr->evaluate($data) : $xpr;
}

/**
* parseArgs Command Line Interface (CLI) utility function.
* @author Patrick Fisher <[email protected]>
Expand Down Expand Up @@ -732,10 +769,12 @@ public static function action_replace($evt)
// ordered map
$replace = Beeld::OrderedMap($current->action_cfg);
$hasHeader = ($data->header && strlen($data->header)) ? true : false;

$xpresion_data = array();
while ($replace->hasNext())
{
$rep = $replace->getNext();
$rep[1] = BeeldUtils::xpresion($rep[1], $evt); // parse xpresion if any
$rep[1] = BeeldUtils::evaluate($rep[1], $xpresion_data);
$data->src = str_replace($rep[0], $rep[1], $data->src);
if ( $hasHeader )
$data->header = str_replace($rep[0], $rep[1], $data->header);
Expand Down Expand Up @@ -882,7 +921,7 @@ public static function action_out($evt)
// extends/implements PublishSubscribe
class Beeld extends PublishSubscribe
{
const VERSION = "0.7.1";
const VERSION = "0.8-alpha";
public static $Parsers = null;

public $actions = null;
Expand All @@ -907,6 +946,11 @@ public static function Obj($props=null)
return PublishSubscribe::Data($props);
}

/*public static function Xpresion($xpr)
{
return new Xpresion($xpr);
}*/

public static function init( )
{
//
Expand All @@ -933,6 +977,15 @@ public static function init( )
// aliases
self::$Parsers[".yaml"] = self::$Parsers[".yml"];
self::$Parsers["*"] = self::$Parsers[".custom"];
Xpresion::defaultConfiguration();
Xpresion::defFunc(array(
'file'=> Xpresion::Func('file', '$Fn->file($0)'),
'tpl'=> Xpresion::Func('tpl', '$Fn->tpl($0)')
));
Xpresion::defRuntimeFunc(array(
'file'=> array('BeeldUtils', 'read_file'),
'tpl'=> array('BeeldUtils', 'get_tpl')
));
}

public function __construct()
Expand Down Expand Up @@ -1049,12 +1102,21 @@ public function &parse( )
));
$params->data = Beeld::Obj();
$params->current = Beeld::Obj();
$params->config = $config;

if ( isset($config['settings']) )
{
if ( !isset($config['settings']['RegExp']) ) $config['settings']['RegExp'] = false;
if ( !isset($config['settings']['Xpresion']) ) $config['settings']['Xpresion'] = false;
}
else
{
$config['settings'] = array('RegExp'=>false, 'Xpresion'=>false);
}
if ( isset($config['plugins']) )
{
$this->loadPlugins($config['plugins'], $params->options->basePath);
}
$params->config = $config;

return $params;
}
Expand Down
Loading

0 comments on commit ff4df33

Please sign in to comment.