------------------------------------------------------------
revno: 1113
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Sun 2012-02-26 23:54:13 +0100
message:
libs/markup.php added
added:
libs/markup.php
modified:
libs/Engine_purephp.php
libs/Engine_v2.php
--
lp:aikiframework
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk
Your team Aiki Framework Developers is subscribed to branch lp:aikiframework.
To unsubscribe from this branch go to
https://code.launchpad.net/~aikiframework-devel/aikiframework/trunk/+edit-subscription
=== modified file 'libs/Engine_purephp.php'
--- libs/Engine_purephp.php 2012-02-26 21:02:15 +0000
+++ libs/Engine_purephp.php 2012-02-26 22:54:13 +0000
@@ -21,12 +21,11 @@
*/
if (!defined('IN_AIKI')) {
-
die('No direct script access allowed');
}
//@TODO move to bootstrap, when it is stable.
-include_once("markup.php");
+include_once("libs/markup.php");
class engine_purephp {
=== modified file 'libs/Engine_v2.php'
--- libs/Engine_v2.php 2012-02-26 21:02:15 +0000
+++ libs/Engine_v2.php 2012-02-26 22:54:13 +0000
@@ -26,7 +26,7 @@
}
//@TODO move to bootstrap, when it is stable.
-include_once("markup.php");
+include_once("libs/markup.php");
class engine_v2 {
=== added file 'libs/markup.php'
--- libs/markup.php 1970-01-01 00:00:00 +0000
+++ libs/markup.php 2012-02-26 22:54:13 +0000
@@ -0,0 +1,310 @@
+<?php
+
+/**
+ * Aiki Framework (PHP)
+ *
+ * LICENSE
+ *
+ * This source file is subject to the AGPL-3.0 license that is bundled
+ * with this package in the file LICENSE.
+ *
+ * @author Roger Martin (rg1024) - Aikilab http://www.aikilab.com
+ * @copyright (c) 2008-2011 Aiki Lab Pte Ltd
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
+ * @link http://www.aikiframework.org
+ * @category Aiki
+ * @package Library
+ * @version 0.1
+ * @filesource
+ *
+ *
+ *
+ */
+
+if (!defined('IN_AIKI')) {
+
+
+ die('No direct script access allowed');
+}
+
+
+function aiki_get_markup_codes($which){
+ /* @TODO this complicate array can be generated by a auxiliary programs, who with a given
+ * syntax ( for example (TAG(TRUE-BLOCK)else(FALSE-BLOCK)TAG) calculate array
+ */
+
+ // 0 begin regex
+ // 1 end regex
+ // 2 else literal
+ // 3 end search string with %s
+ // 4 begin signal (first character of a begin tag)
+ // 5 trim characters that will be deleted from tag.
+ // 6 first character from true-block
+
+ $markups= array (
+ // (TAG()TRUE-BLOCK)else(FALSE-BLOCK)TAG)
+ "aiki" => array("\([A-z0-9_]*\(", "\)[A-z0-9]*\)", ")else(", ")%s)","(", "#\((.*)\(#","" ),
+ // (TAG( para {TRUE-BLOCK}else{FALSE-BLOCK})TAG)
+ "aiki2" => array("\([A-z0-9_]*\(", "\}\)[A-z0-9]*\)", "})else({", "})%s)","(", "#\((.*)\(#","" ),
+ // (TAG(COND){true-block}else{false-block}TAG)
+ "(c)"=> array("\([A-z0-9_]+\(","\}[A-z0-9]+\)", "}else{", "}%s)", "(","#\((.*)\(#", "{" ),
+ // <%TAG(COND){true-block}else{false-block}%>
+ "<%"=> array("<%[A-z0-9_]+\(","\}%>", "}else{", "}%>", "<","#<%(.*)\(#","" )
+ );
+
+ return ( isset ($markups[$which]) ? $markups[$which] : $markups["aiki"] );
+
+}
+
+/**
+ * extract the first markup present in the given string
+ *
+ * @param string text to parse
+ * @param mixed array codes to detect markup | string with markup style name
+ *
+ * @return array
+ * 0 => text before tag
+ * 1 => tag
+ * 2 => array of parameters
+ * 3 => true-block
+ * 4 => false-block
+ * 5 => text after tag
+ *
+ * @TODO, instead detect abstrat tag: ([A_z0-9]*) it can only detect
+ * allowed markup: form|sql|aiki|... that will be more exact,
+ */
+
+function extract_markup(&$string, $markup) {
+
+ if (!is_array($markup) ){
+ $markup= aiki_get_markup_codes($markup);
+ }
+
+ if ( !preg_match_all(
+ "#{$markup[0]}|{$markup[1]}|". preg_quote($markup[2])."#",
+ $string,
+ $matches,
+ PREG_OFFSET_CAPTURE)) {
+ return $string ;
+ };
+
+ $matches= $matches[0];
+ $max= count($matches);
+ preg_match ( $markup[5], $matches[0][0],$temp);
+ $tag = $temp[1];
+
+ $search= sprintf( $markup[3], $tag );
+ $level = 0;
+ $else = 0; // where start the else block
+
+ for($i=1;$i<$max;$i++){
+ $found = $matches[$i][0]; // for comodity
+ if ( $found==$markup[2] ) {
+ if ($level==0 ){ // else are in the middle
+ $else= $matches[$i][1];
+ }
+ } elseif ($level==0 && $found==$search ) {
+ // we found ending tag
+ $offset= strlen($search);
+ $start = $matches[0][1]+$offset;
+ $elselen= strlen($markup[2]);
+ $trueBlock= ( $else ? substr( $string,$start, $else-$start) : substr( $string,$start, $matches[$i][1]-$start));
+ $parameters = extract_parameters( $trueBlock, $markup[6]);
+ return array(
+ substr( $string,0,$start-$offset ),
+ $tag,
+ $parameters[0],
+ $parameters[1],
+ ( $else ? substr( $string,$else+$elselen, $matches[$i][1]-$else-$elselen) : NULL),
+ substr($string, $matches[$i][1]+$offset));
+ } elseif ( $found[0] =="(" ){
+ $level++;
+ } else {
+ $level--;
+ }
+ }
+ return $string;
+}
+
+
+/**
+ * convert a given array of parameters in real parameters (evaluate them)
+ *
+ * @param array of originals parameters
+ *
+ * Example
+ * eval_parameters ( array("true","2","{1,2,{3,4}", "{"foo":"bar"}"
+ * => array (0=>true, 1=>2, 3=>array(1,2,array(3,4)), json-object foo->bar)
+ *
+ * @TODO define the list of valid functions: substr, replace,
+ * @TODO nested array and functions.
+ * @TOTO eval expression
+ */
+
+function eval_parameters( $originals ){
+
+ $translation= array("replace"=>"str_replace", "tolower"=> "strtolower", "toupper"=>"strtoupper" );
+
+ $ret = array();
+ $temp= false;
+
+ foreach ( $originals as $input){
+ if ( $input==="" ) { $ret[]="";}
+ elseif ( $input==="0" ) { $ret[]=0;}
+ elseif ( $input==="false" ){ $ret[]=false;}
+ elseif ( $input==="true" ){ $ret[]=true;}
+ elseif ( $input[0]=="'" || $input[0]=='"' ) { $ret[]= substr($input,1,-1);}
+ elseif ( preg_match('#^\s*[+\-]?[1-9][0-9]*\s*$#', $input, $temp)) { $ret[]= (int) $input;}
+ elseif ( preg_match('#^\s*[+\-]?[1-9][0-9]*\.[0-9]*\s*$#', $input, $temp)){ $ret[]= (float) $input;}
+ elseif ( preg_match('#^\{\s*"[^"]+"\s*\:\s*"[^"]*"#',$input, $temp)){ $ret[]= json_decode($input);}
+ elseif ( $input[0]=="[") {$ret[]= json_decode($input);}
+ elseif ( $input[0]=="{") {
+ $temp = extract_parameters( substr($input,1,-1));
+ $ret[]= eval_parameters( $temp[0] );}
+ elseif ( preg_match ('#^\s*aiki\-\>(.*)$#',$input,$temp )){
+ $temp = $temp[1];
+ $ret[]= $aiki->$temp; // @TODO need test
+ }elseif ( preg_match ('#^\s*([A-z_0-9]+)\((.*)\)$#', $input, $temp )){
+
+ $call=$temp[1];
+ if ( isset($translation[$call])) {
+ $call= $translation[$call];
+ }
+
+ switch ( $call) {
+ case "str_replace":
+ case "substr":
+ $para = extract_parameters($temp[2]);
+ $para = eval_parameters($para[0]);
+ $ret[] = call_user_func_array($call,$para);
+ break;
+ default:
+ $ret[]=$input;
+ }
+
+ } else {
+ $ret[]=$input;
+ }
+ }
+
+ return $ret;
+
+}
+
+
+/**
+ * extract the parameters present at the begining of text
+ * @return array
+ * 0 => array of parameters.
+ * 1 => rest of string
+ * example: extract_parameters ( "foo'bar",foo(bar),{foo,bar,foo()} ) rest"
+ * return a array
+ * 0=>array ( "foo'bar", "foo(bar)", "{foo,bar,foo()}")
+ * 1=> " rest"
+ */
+
+function extract_parameters( $string, $find="") {
+
+ $max= strlen($string);
+
+ $state= 0;
+ $current= false;
+ $end = false;
+ $level =0;
+ $ret= array();
+
+ // there are 4 states:
+ // 1 over a delimited with ' string
+ // 2 over a delimited with " string
+ // 3 over a structur begine with { or )
+
+
+ for($i=0; $i<$max && !$end; $i++){
+ $add = false;
+ $char = $string[$i];
+
+ switch ( $char ){
+
+ case "'":
+ switch ($state){
+ case 0: $state=1; break;
+ case 1: if ($level==0 && !$escaped ){ $add= true;}
+ }
+ break;
+ case '"':
+ switch ($state){
+ case 0: $state=2; break;
+ case 2: if ( $level==0 && !$escaped ) { $add= true;}
+ }
+ break;
+ // do we found a other arg?
+ case ',':
+ if (($state==0 || $state==3) && $level==0) {
+ $add = true;
+ $char= false;
+ }
+ break;
+
+ // we a begin (
+ case '[':
+ case '(':
+ case '{':
+ if ($state==0 || $state==3) {
+ $level++;
+ $state=3;
+ }
+ break;
+
+ // do we found a ending { ?
+ case ']':
+ case '}':
+ case ')':
+ if ($state==0 || $state ==3 ) {
+ if ( $level == 0 ) {
+ $add = true;
+ $end = true;
+ $char = false;
+ } elseif ($level==1) {
+ $add = true;
+ $level=0;;
+ } else {
+ $level--;
+ }
+ }
+ break;
+ case " ":
+ break;
+ default :
+ if ( $state== 0 ){
+ $state=3;
+ }
+ }
+
+ if ( $state != 0 ){
+ $current .= $char;
+ }
+
+ if ( $add ){
+ if ( $current !== false ){
+ $ret[] = $current;
+ $current = false;
+ }
+ $state = 0;
+ }
+
+ $escaped= ( $char=="\\" );
+ } // for
+ if ( $current !== false ){
+ $ret[] = $current;
+ }
+
+ // for the (a,b) { begin ..supress the { @TODO improve.
+ if ( $find !="") {
+ $temp = stripos ($string,$find,$i);
+ if ( $temp !==false ){
+ $i=$temp+1;
+ }
+ }
+
+ return array ( $ret, substr( $string,$i));
+}
_______________________________________________
Mailing list: https://launchpad.net/~aikiframework-devel
Post to : [email protected]
Unsubscribe : https://launchpad.net/~aikiframework-devel
More help : https://help.launchpad.net/ListHelp