Skip to content

Commit

Permalink
Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
qstudio committed Jul 14, 2020
1 parent 1bfa8f9 commit 96401d4
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 48 deletions.
7 changes: 3 additions & 4 deletions library/buffer/_load.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ public static function run(){
}

// Header ##
h::log( 't:>@TODO... header inclusion needs to be more graceful, and render needs to have "blocks", which can be passed / set');
context::ui__header();

// Apply any filters to the final output
// echo \apply_filters( 'ob_output', $string );
echo self::prepare( $string );

h::log( 't:>@TODO... footer + header inclusion needs to be more graceful, and render needs to have "blocks", which can be passed / set');

// Footer ##
context::ui__footer();
// Footer, basically just wp_footer() + closing body / html tags ##
// context::ui__footer();

}, 0);

Expand Down
26 changes: 22 additions & 4 deletions library/context/ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,28 @@ public static function footer( $args = null )
}
\do_action( 'get_footer', $name );

return theme\view\ui\footer::render( $args );
// return render\fields::define([
// 'footer' => '' // hack.. nothing to pass here ##
// ]);
// required hook ##
\wp_footer();

// look for property "args->task" in config ##
if (
$config = core\config::get([ 'context' => $args['context'], 'task' => $args['task'] ])
){

// check ##
// h::log( $config );

// define "fields", passing returned data ##
return render\fields::define(
$config
);

} else {

// we can call the footer::render() method
return theme\view\ui\footer::render( $args );

}

}

Expand Down
23 changes: 20 additions & 3 deletions library/parse/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ public static function prepare( $args = null ){
// self::$args[$hash] = \q\core\method::parse_args( $config, self::$args[$hash] );
// h::log( self::$args );

// default is an html comment - also indicated with flag 'h', if set ##
// html comments are rendered on the UI, so require to add a variable tag to the markup ##
if (
empty( self::$flags )
|| isset( self::$flags['h'] )
isset( self::$flags['h'] )
){

// so, we can add a new field value to $args array based on the field name - with the comment as value
Expand All @@ -205,8 +204,12 @@ public static function prepare( $args = null ){
$variable = willow\tags::wrap([ 'open' => 'var_o', 'value' => self::$hash, 'close' => 'var_c' ]);
willow\markup::swap( self::$comment_match, $variable, 'comment', 'variable' ); // '{{ '.$field.' }}'

// update match string ##
// self::$comment_match = $variable;

}

// PHP log ##
if (
isset( self::$flags['p'] )
){
Expand All @@ -216,6 +219,20 @@ public static function prepare( $args = null ){

}

// default is a "silent" comment - also indicated with flag 's', if set ##
// this comment is not rendered in html or PHP and the willow tag is removed ##
// this goes last, as we might need the match reference for previous replacements ##
if (
empty( self::$flags )
|| isset( self::$flags['s'] )
){

// h::log( 'Silent comment, we need to remove the tag' );

self::$markup['template'] = willow\markup::remove( self::$comment_match, self::$markup['template'], 'comment' );

}

// clear slate ##
self::reset();

Expand Down
8 changes: 4 additions & 4 deletions library/parse/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static function format( $match = null ){
}

// test what we have ##
// h::log( 'd:>function: "'.$function.'"' );
// h::log( 'd:>function: "'.self::$function.'"' );
self::$hash = self::$hash.'.'.rand();
// h::log( 'hash at end is...: '.self::$hash );

Expand Down Expand Up @@ -272,8 +272,8 @@ public static function format( $match = null ){
// pass args, if set ##
if( self::$arguments ){

// h::log( 'passing args array to: '.self::$function );
// h::log( self::$arguments );
h::log( 'passing args array to: '.self::$function );
h::log( self::$arguments );

self::$return = call_user_func( self::$function, self::$arguments );

Expand All @@ -283,7 +283,7 @@ public static function format( $match = null ){

} else {

// h::log( 'NOT passing args array to: '.self::$function );
h::log( 'NOT passing args array to: '.self::$function );

// global functions skip internal processing and return their results directly to the buffer ##
self::$return = self::$function;
Expand Down
99 changes: 74 additions & 25 deletions library/parse/markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public static function swap( string $from = null, string $to = null, $from_type
case "string" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = ''; #'{{ ';
$needle_end = ''; #' }}';
$needle_start = '';
$needle_end = '';

break;

Expand All @@ -219,55 +219,84 @@ public static function swap( string $from = null, string $to = null, $from_type
case "partial" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'par_o' ); #'{{ ';
$needle_start = tags::g( 'par_o' ); #'{{> ';
$needle_end = tags::g( 'par_c' ); #' }}';

break ;

case "loop" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'loo_o' ); #'{{ ';
$needle_start = tags::g( 'loo_o' ); #'{{@ ';
$needle_end = tags::g( 'loo_c' ); #' }}';

break ;

case "willow" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'wil_o' ); #'{{ ';
$needle_start = tags::g( 'wil_o' ); #'{{~ ';
$needle_end = tags::g( 'wil_c' ); #' }}';

break ;

case "function" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'fun_o' ); #'{{ ';
$needle_end = tags::g( 'fun_c' ); #' }}';
$needle_start = tags::g( 'fun_o' ); #'<< ';
$needle_end = tags::g( 'fun_c' ); #' >>';

break ;

case "comment" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'com_o' ); #'{{ ';
$needle_start = tags::g( 'com_o' ); #'{{! ';
$needle_end = tags::g( 'com_c' ); #' }}';

break ;

}

// validate strings ##
// @todo - perhaps need to be more liberal or restrictive on this.. will see ##
if(
'string' == $to_type
&& ! is_string( $to )
){

// log ##
h::log( self::$args['task'].'~>e:>tag: "'.$to.'" is not a correctly formatted '.$to_type.'' );

// log ##
h::log( 'e:>tag: "'.$to.'" is not a correctly formatted '.$to_type.'' );

return false;

}

// trim for regex ##
$open = trim( $needle_start );
$close = trim( $needle_end );

// regex ##
$regex_find = \apply_filters(
'q/willow/parse/markup/regex/find',
"/$open\s+(.*?)\s+$close/s" // note:: added "+" for multiple whitespaces.. not sure it's good yet...
);

if (
! core\method::starts_with( $to, $needle_start )
|| ! core\method::ends_with( $to, $needle_end )
// ! core\method::starts_with( $to, $needle_start )
// || ! core\method::ends_with( $to, $needle_end )
! preg_match( $regex_find, $to )
&& 'string' != $to_type // we can skip strings, as their format was validated earlier ##
) {

// log ##
h::log( self::$args['task'].'~>e:>tag: "'.$to.'" is not correctly formatted - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );
h::log( self::$args['task'].'~>e:>tag: "'.$to.'" is not a correctly formatted '.$to_type.' - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );

// log ##
h::log( 'e:>tag: "'.$from.'" is not correctly formatted - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );
h::log( 'e:>tag: "'.$to.'" is not a correctly formatted '.$to_type.' - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );

return false;

Expand All @@ -280,8 +309,8 @@ public static function swap( string $from = null, string $to = null, $from_type
case "string" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = ''; #'{{ ';
$needle_end = ''; #' }}';
$needle_start = '';
$needle_end = '';

break;

Expand All @@ -297,52 +326,64 @@ public static function swap( string $from = null, string $to = null, $from_type
case "partial" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'par_o' ); #'{{ ';
$needle_start = tags::g( 'par_o' ); #'{{> ';
$needle_end = tags::g( 'par_c' ); #' }}';

break ;

case "comment" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'com_o' ); #'{{ ';
$needle_start = tags::g( 'com_o' ); #'{{! ';
$needle_end = tags::g( 'com_c' ); #' }}';

break ;

case "loop" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'loo_o' ); #'{{ ';
$needle_end = tags::g( 'loo_c' ); #' }}';
$needle_start = tags::g( 'loo_o' ); // '{{@ ';
$needle_end = tags::g( 'loo_c' ); // ' }}';

break ;

case "willow" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'wil_o' ); #'{{ ';
$needle_end = tags::g( 'wil_c' ); #' }}';
$needle_start = tags::g( 'wil_o' ); #'{{~ ';
$needle_end = tags::g( 'wil_c' ); #' ~}}';

break ;

case "function" :

// check if variable is correctly formatted --> {{> STRING }} ##
$needle_start = tags::g( 'fun_o' ); #'{{ ';
$needle_end = tags::g( 'fun_c' ); #' }}';
$needle_start = tags::g( 'fun_o' ); #'<< ';
$needle_end = tags::g( 'fun_c' ); #' >>';

break ;

}

// trim for regex ##
$open = trim( $needle_start );
$close = trim( $needle_end );

// regex ##
$regex_find = \apply_filters(
'q/willow/parse/markup/regex/find',
"/$open\s+(.*?)\s+$close/s" // note:: added "+" for multiple whitespaces.. not sure it's good yet...
// "/{{#(.*?)\/#}}/s"
);

if (
! core\method::starts_with( $from, $needle_start )
|| ! core\method::ends_with( $from, $needle_end )
// ! core\method::starts_with( $from, $needle_start )
// || ! core\method::ends_with( $from, $needle_end )
! preg_match( $regex_find, $from )
) {

// log ##
h::log( self::$args['task'].'~>e:>tag: "'.$from.'" is not correctly formatted - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );
h::log( self::$args['task'].'~>e:>tag: "'.$from.'" is not a correctly formatted '.$from_type.' - missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );

// log ##
h::log( 'e:>tag: "'.$from.'" is not a correctly formatted '.$from_type.' -> missing "'.$needle_start.'" at start or "'.$needle_end.'" at end.' );
Expand Down Expand Up @@ -414,6 +455,14 @@ public static function remove( string $variable = null, $markup = null, $type =

break ;

case "comment" :

// check if variable is correctly formatted --> {{ STRING }} ##
$needle_start = tags::g( 'com_o' ); #'{{ ';
$needle_end = tags::g( 'com_c' ); #' }}';

break ;

}

if (
Expand Down
16 changes: 8 additions & 8 deletions q-willow.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ class q_willow {
// based on Mustache, but not the same... https://github.com/bobthecow/mustache.php/wiki/Mustache-Tags
$tags = [

// willow tag
'willow' => [
'open' => '{{~ ', // open ##
'close' => ' ~}}', // close ##
],

// variables ##
'variable' => [
'open' => '{{ ', // open ##
Expand All @@ -176,15 +182,9 @@ class q_willow {

// loops ##
'loop' => [
'open' => '{{# ', // open ##
'open' => '{{@ ', // open ##
'close' => ' }}', // close ##
'end' => '{{/#}}' // end statement ##
],

// placeholder -- i.e. willow tag
'willow' => [
'open' => '{{{ ', // open ##
'close' => ' }}}', // close ##
'end' => '{{/@}}' // end statement ##
],

// function
Expand Down

0 comments on commit 96401d4

Please sign in to comment.