Merge pull request #1 from Zigazou/master

Now delete unused blocks
This commit is contained in:
Gnieark 2018-03-20 08:29:01 +01:00 committed by GitHub
commit d02867eb5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ class TplBlock {
public $name = ''; public $name = '';
private $vars = array(); private $vars = array();
private $subBlocs = array(); private $subBlocs = array();
private $unusedRegex = "";
/* /*
* Initialise TplBlock * Initialise TplBlock
@ -29,6 +30,18 @@ class TplBlock {
public function __construct($name = NULL){ public function __construct($name = NULL){
$this->name = $name; $this->name = $name;
$this->unusedRegex = '/'
. self::blockStartStart
. ' *([a-z][a-z.]*) *'
. self::blockStartEnd
. '(.*?)'
. self::blockEndStart
. ' *\1 *'
. self::blockEndEnd
. '/is'
;
} }
/* /*
@ -52,6 +65,18 @@ class TplBlock {
$this->subBlocs[$bloc->name][] = $bloc; $this->subBlocs[$bloc->name][] = $bloc;
} }
private function subBlockRegex($prefix, $blocName) {
return '/'
. self::blockStartStart
. preg_quote($prefix . $blocName)
. self::blockStartEnd
. '(.*?)'
. self::blockEndStart
. preg_quote($prefix . $blocName)
. self::blockEndEnd
. '/is';
}
/* /*
* Shake template and input vars and returns the text * Shake template and input vars and returns the text
* Input: * Input:
@ -75,10 +100,7 @@ class TplBlock {
//parse blocs //parse blocs
foreach($this->subBlocs as $blocName => $blocsArr){ foreach($this->subBlocs as $blocName => $blocsArr){
$str = preg_replace_callback( $str = preg_replace_callback(
'/' . self::blockStartStart . preg_quote($prefix . $blocName) . self::blockStartEnd . $this->subBlockRegex($prefix, $blocName),
'(.*?)'.
self::blockEndStart . preg_quote($prefix . $blocName). self::blockEndEnd.
'/is',
function($m) use($blocName,$blocsArr,$prefix, $trim) { function($m) use($blocName,$blocsArr,$prefix, $trim) {
$out = ""; $out = "";
foreach($blocsArr as $bloc){ foreach($blocsArr as $bloc){
@ -86,14 +108,13 @@ class TplBlock {
$out.=$bloc->apply_tpl_str( $m[1] , $prefix . $blocName , $trim ); $out.=$bloc->apply_tpl_str( $m[1] , $prefix . $blocName , $trim );
} }
return $out; return $out;
}, }
$str ,$str
); );
} }
//delete unused blocs // Delete unused blocs
// TODO $str = preg_replace($this->unusedRegex, "", $str);
if($trim){ if($trim){
return trim($str,"\n"); return trim($str,"\n");