------------------------------------------------------------
revno: 1123
committer: Roger Martin <[email protected]>
branch nick: aikiframework
timestamp: Mon 2012-03-05 21:33:48 +0100
message:
engine_v2 with noaki,nl2p, nl2br
modified:
libs/Engine_v2.php
libs/markup.php
libs/text.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_v2.php'
--- libs/Engine_v2.php 2012-03-01 22:20:20 +0000
+++ libs/Engine_v2.php 2012-03-05 20:33:48 +0000
@@ -35,11 +35,12 @@
private $convert_widgets;
private $markup_codes;
- private $parsers; // array of parser (markup=>callback)
-
private $widget_css;
private $widget_html;
+ private $parsers; // all allowed parsers
+ private $simple_parsers; // list of simple parser.
+
/*
* Create layout
*/
@@ -79,7 +80,8 @@
$this->markup = 'aiki';
}
- // set parsers
+ // set parsers
+ // @TODO load dynamically
$this->parsers = array (
"" => "parse_vars",
"widget" => "parse_widget",
@@ -89,8 +91,14 @@
"sql" => "parse_sql",
"script" => "parse_script",
"t" => "parse_t",
+ "nl2p" => "parse_nl2p",
+ "nl2br" => "parse_nl2br",
"__" => "parse_translate");
+ // set special parsers
+ // @TODO load dynamically
+ $this->simple_parsers = array ( "t", "noaiki", "nl2p", "nl2br" );
+
if ( isset($parameters["parsers-allowed"]) ){
$this->parsers = array_intersect (
$this->parsers ,
@@ -177,7 +185,7 @@
function parse($text){
// extract begin,tag,condition & true-block, else-block,rest
- $match = extract_markup ( $text,$this->markup_codes );
+ $match = extract_markup ( $text, $this->markup_codes, $this->simple_parsers );
if (!is_array($match) ){
return $text;
@@ -192,9 +200,10 @@
$result = t("Parser $parserToCall not found");
}
- // return begin+result+parsing the the rest..
+
+
return $match[0].
- $this->parse($result).
+ ($match[1]=="noaiki" ? $result: $this->parse($result)).
$this->parse($match[5]);
}
@@ -222,6 +231,7 @@
$language = $aiki->site->language();
$prefix = $aiki->site->prefix();
$view_prefix= $aiki->site->view_prefix();
+
$paths= array();
if ($prefix) {
@@ -240,7 +250,7 @@
$url = str_replace("http://", "https://", $url);
}
- $trimedUrl = preg_replace('#/$#',"",$url); // reg: remove ending /
+ $trimedUrl = preg_replace('#/$#',"",$url); // reg: remove ending /
$bufferReplace = array(
'userid' => $aiki->membership->userid,
@@ -263,8 +273,14 @@
'root-language' => $trimedUrl . "/" . $aiki->site->language(),
'site_prefix' => $prefix ,
'view_prefix' => $view_prefix ,
- 'route]' => $trimedUrl. "/". $paths,
- '$route-local' => $paths );
+ 'route' => $trimedUrl. "/". $paths,
+ 'url' => $aiki->url->get(),
+ 'route-local' => $paths );
+ foreach ($bufferReplace as $key => $value ){
+ if (is_null($value) ){
+ $bufferReplace[$key]="";
+ }
+ }
}
$token=$parameters[0];
@@ -612,8 +628,22 @@
return "";
}
- function parse_noaiki(&$cond, &$text, $else){
- return $text;
+
+ /*
+ * These are simple parse with only a raw block as argument.
+ */
+
+ function parse_noaiki($text){
+ return $text; //noaki can't contain any markup
+ }
+
+ function parse_nl2br ($text){
+ return nl2br( $this->parse($text));
+ }
+
+ function parse_nl2p ( $text){
+ global $aiki;
+ return $aiki->text->nl2p($this->parse($text));
}
=== modified file 'libs/markup.php'
--- libs/markup.php 2012-03-01 22:20:20 +0000
+++ libs/markup.php 2012-03-05 20:33:48 +0000
@@ -37,19 +37,27 @@
// 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
+ // 4 short search string with %s
+ // 5 begin signal (first character of a begin tag)
+ // 6 trim characters that will be deleted from tag.
+ // 7 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{", "}%>", "<","#<%(.*)\(#","" )
+ // (TAG(COND){true-block}else{false-block}TAG)
+ // (TAG(COND){true-block}TAG)
+ // (TAG(COND){}TAG)
+ // (TAG(COND)TAG)
+ "aiki" => array("\([A-z0-9_]*\(", "[\}\)][A-z0-9]*\)", "}else{", "}%s)",")%s)","(", "#\((.*)\(#","" ),
+
+ // (TAG(COND)({true-block})else({false-block})TAG)
+ // (TAG(COND)({true-block})TAG)
+ // (TAG(COND)TAG) NO VALID.
+ "aiki-discarded" => array("\([A-z0-9_]*\(", "\}\)[A-z0-9]*\)", "})else({", "})%s)","","(", "#\((.*)\(#","" ),
+
+ // <%TAG(COND){true-block} _/>
+ // <%TAG(COND)%>
+ // <%TAG(COND)true-block%>
+ "<%"=> array("<%[A-z0-9_]+\(","\}%>", "}else{", "}%>","", "<","#<%(.*)\(#","" )
);
return ( isset ($markups[$which]) ? $markups[$which] : $markups["aiki"] );
@@ -74,7 +82,7 @@
* allowed markup: form|sql|aiki|... that will be more exact,
*/
-function extract_markup(&$string, $markup) {
+function extract_markup(&$string, $markup, &$simpleParsers=NULL) {
if (!is_array($markup) ){
$markup= aiki_get_markup_codes($markup);
@@ -90,10 +98,37 @@
$matches= $matches[0];
$max= count($matches);
- preg_match ( $markup[5], $matches[0][0],$temp);
- $tag = $temp[1];
-
- $search= sprintf( $markup[3], $tag );
+
+ // extract the tag
+ preg_match ( $markup[6], $matches[0][0],$temp);
+ $tag = $temp[1];
+
+ $search = sprintf( $markup[3], $tag );
+ $shortSearch= ($markup[4] ? sprintf( $markup[4], $tag ): false );
+
+ // there are two type of parser: complex with three block and simple
+ // with only a raw block.
+ if (!is_null($simpleParsers) && in_array ( $tag, $simpleParsers)){
+ $to = strpos ( $string,$shortSearch);
+ $offset = strlen($shortSearch);
+ if ( $to===false){
+ $to= strpos ( $string,$search);
+ $offset = strlen($search);
+ }
+ if ( $to === false ){
+ return "" ;// invalid widget
+ }
+ //
+ $start = $matches[0][1];
+ return array(
+ substr( $string,0,$start ),
+ $tag,
+ substr( $string, $start+strlen($matches[0][0]),$to-$start-$offset) ,
+ "",
+ "",
+ substr($string, $to+$offset));
+ }
+
$level = 0;
$else = 0; // where start the else block
@@ -103,13 +138,13 @@
if ($level==0 ){ // else are in the middle
$else= $matches[$i][1];
}
- } elseif ($level==0 && $found==$search ) {
+ } elseif ($level==0 && ( $found==$search || $found==$shortSearch)) {
// 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]);
+ $parameters = extract_parameters( $trueBlock, $markup[7]);
return array(
substr( $string,0,$start-$offset ),
$tag,
@@ -117,7 +152,7 @@
$parameters[1],
( $else ? substr( $string,$else+$elselen, $matches[$i][1]-$else-$elselen) : NULL),
substr($string, $matches[$i][1]+$offset));
- } elseif ( $found[0] =="(" ){
+ } elseif ( $found[0] == $markup[5]){
$level++;
} else {
$level--;
=== modified file 'libs/text.php' (properties changed: +x to -x)
--- libs/text.php 2011-11-08 13:42:45 +0000
+++ libs/text.php 2012-03-05 20:33:48 +0000
@@ -18,8 +18,14 @@
*/
if (!defined('IN_AIKI')) {
+
+ $t= "[br[1\n2\n3\n]br]";
+ $text = new text();
+ echo "hi",$text->aiki_nl2br($t);
+
die('No direct script access allowed');
}
+
/**
@@ -31,22 +37,29 @@
* @todo rethink aikimarkup
*/
class text {
+
+
+ private function nl2br ( $match ){
+ return nl2br($match[1]);
+ }
+
/**
* Newline to br tag aiki style.
*
* @param string $text text for processing
* @return string
*/
+
public function aiki_nl2br($text) {
- if (preg_match_all( "#\[br\[(.*)\]br\]#sUi", $text, $captured)) {
- foreach ( $captured[1] as $i=>$match ) {
- $replace[ $captured[0][$i]]= nl2br($match);
- }
- $text= strtr($text, $replace);
- }
- return $text;
+ return preg_replace_callback( "#\[br\[(.*)\]br\]#sUi", array ( $this,"nl2br"), $text);
}
+ public function nl2p($match) {
+ // if match is an array, we suppose the caller is preg_replace_callback.
+ $text = "<p>".str_replace("\n\r", "</p><p>", ( is_array($match) ? $match[1]: $match)). "</p>";
+ return strtr( nl2br($text), array ( "<br />\r</p>"=>"</p>", "<p><br />"=>"<p>"));
+ }
+
/**
* newline to paragraph aiki style
*
@@ -54,22 +67,8 @@
* @global aiki $aiki global aiki instance
* @return string
*/
- public function aiki_nl2p($text) {
- global $aiki;
-
- if (preg_match_all("#\[p\[(.*)\]p\]#sUi", $text, $captured)) {
- foreach ( $captured[1] as $i=>$match ) {
- $nl2p_text = str_replace("\n\r", "</p><p>", $match);
- $nl2p_text = "<p>".$nl2p_text."</p>";
- $nl2p_text = nl2br($nl2p_text);
- $nl2p_text = str_replace("<br />\r</p>", "</p>", $nl2p_text);
- $nl2p_text = str_replace("<p><br />", "<p>", $nl2p_text);
- $replace[$captured[0][$i]] =
- str_replace("<p></p>", "<br />", $nl2p_text);
- }
- $text= strtr($text, $replace);
- }
- return $text;
+ public function aiki_nl2p($text) {
+ return preg_replace_callback ("#\[p\[(.*)\]p\]#sUi", array($this,"nl2p"), $text);
}
/**
@@ -152,4 +151,4 @@
} // end of is_valid
} // end of class
-?>
\ No newline at end of file
+?>
_______________________________________________
Mailing list: https://launchpad.net/~aikiframework-devel
Post to : [email protected]
Unsubscribe : https://launchpad.net/~aikiframework-devel
More help : https://help.launchpad.net/ListHelp