Add STRIP option

This commit is contained in:
bzick
2014-05-08 12:56:37 +04:00
parent 83e02ebbe9
commit 45afbfabdf
12 changed files with 209 additions and 87 deletions

View File

@@ -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
*/

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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;
}
/**