mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add STRIP option
This commit is contained in:
@@ -35,11 +35,8 @@ class Fenom
|
||||
const DISABLE_CACHE = 0x400;
|
||||
const FORCE_VERIFY = 0x800;
|
||||
const AUTO_TRIM = 0x1000; // reserved
|
||||
const DENY_STATICS = 0x2000;
|
||||
const AUTO_STRIP = 0x4000;
|
||||
|
||||
/* @deprecated */
|
||||
const DENY_INLINE_FUNCS = 0x20;
|
||||
const DENY_STATICS = 0x2000;
|
||||
const AUTO_STRIP = 0x4000;
|
||||
|
||||
/* Default parsers */
|
||||
const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose';
|
||||
@@ -66,6 +63,7 @@ class Fenom
|
||||
"force_verify" => self::FORCE_VERIFY,
|
||||
"auto_trim" => self::AUTO_TRIM,
|
||||
"disable_statics" => self::DENY_STATICS,
|
||||
"strip" => self::AUTO_STRIP,
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -265,10 +263,20 @@ class Fenom
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagRaw'
|
||||
),
|
||||
'autoescape' => array(
|
||||
'autoescape' => array( // deprecated
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::autoescapeOpen',
|
||||
'close' => 'Fenom\Compiler::autoescapeClose'
|
||||
'open' => 'Fenom\Compiler::escapeOpen',
|
||||
'close' => 'Fenom\Compiler::nope'
|
||||
),
|
||||
'escape' => array(
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::escapeOpen',
|
||||
'close' => 'Fenom\Compiler::nope'
|
||||
),
|
||||
'strip' => array(
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::stripOpen',
|
||||
'close' => 'Fenom\Compiler::nope'
|
||||
)
|
||||
);
|
||||
|
||||
@@ -988,7 +996,7 @@ class Fenom
|
||||
}
|
||||
|
||||
/**
|
||||
* Register PSR-0 autoload for Fenom
|
||||
* Register PSR-0 autoload
|
||||
* @param string $dir custom directory for autoloading, if NULL — autoload itself
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
@@ -962,7 +962,7 @@ class Compiler
|
||||
* @param Tokenizer $tokens
|
||||
* @param Tag $tag
|
||||
*/
|
||||
public static function autoescapeOpen(Tokenizer $tokens, Tag $tag)
|
||||
public static function escapeOpen(Tokenizer $tokens, Tag $tag)
|
||||
{
|
||||
$expected = ($tokens->get(T_STRING) == "true" ? true : false);
|
||||
$tokens->next();
|
||||
@@ -970,11 +970,20 @@ class Compiler
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Tag $tag
|
||||
* Do nothing
|
||||
*/
|
||||
public static function autoescapeClose(Tokenizer $tokens, Tag $tag)
|
||||
public static function nope()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Tag $tag
|
||||
*/
|
||||
public static function stripOpen(Tokenizer $tokens, Tag $tag)
|
||||
{
|
||||
$expected = ($tokens->get(T_STRING) == "true" ? true : false);
|
||||
$tokens->next();
|
||||
$tag->setOption(\Fenom::AUTO_STRIP, $expected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ class Modifier
|
||||
{
|
||||
$str = trim($str);
|
||||
if ($to_line) {
|
||||
return preg_replace('#[\s]+#ms', ' ', $str);
|
||||
return preg_replace('#\s+#ms', ' ', $str);
|
||||
} else {
|
||||
return preg_replace('#[ \t]{2,}#', ' ', $str);
|
||||
}
|
||||
@@ -158,7 +158,7 @@ class Modifier
|
||||
} elseif (is_array($item)) {
|
||||
return count($item);
|
||||
} elseif ($item instanceof \Countable) {
|
||||
return count($item);
|
||||
return $item->count();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -263,13 +263,27 @@ class Tag extends \ArrayObject
|
||||
return $this->tpl->out($code, $this->escape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable escape option for the tag
|
||||
*/
|
||||
public function optEscape()
|
||||
{
|
||||
$this->escape = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable escape option for the tag
|
||||
*/
|
||||
public function optRaw()
|
||||
{
|
||||
$this->escape = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable strip spaces option for the tag
|
||||
*/
|
||||
public function optStrip()
|
||||
{
|
||||
$this->setOption(\Fenom::AUTO_STRIP, true);
|
||||
}
|
||||
}
|
||||
@@ -344,7 +344,6 @@ class Template extends Render
|
||||
foreach ($this->_filters as $filter) {
|
||||
$text = call_user_func($filter, $this, $text);
|
||||
}
|
||||
$this->_body .= $text;
|
||||
} else {
|
||||
$fragments = explode("<?", $text);
|
||||
foreach ($fragments as &$fragment) {
|
||||
@@ -354,11 +353,16 @@ class Template extends Render
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->_body .= implode('<?php echo "<?"; ?>', $fragments);
|
||||
$text = implode('<?php echo "<?"; ?>', $fragments);
|
||||
}
|
||||
} else {
|
||||
$this->_body .= str_replace("<?", '<?php echo "<?"; ?>' . PHP_EOL, $text);
|
||||
$text = str_replace("<?", '<?php echo "<?"; ?>' . PHP_EOL, $text);
|
||||
}
|
||||
if($this->_options & Fenom::AUTO_STRIP) {
|
||||
$text = preg_replace('/\s+/uS', ' ', $text);
|
||||
$text = preg_replace('/\s*([\pP\pS]+)\s*/uS', '$1', $text);
|
||||
}
|
||||
$this->_body .= $text;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user