Skip to content
116 changes: 58 additions & 58 deletions view/engines/hydrogen/filters/ExcerptFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
* <pre>
* {% set myVar %}
* This is a very long Text.
* This is a very long Text.
* {% endset %}
* {{myVar|excerpt:5:"w"}} => This is a very long [...]
* {{myVar|excerpt:12:"c":"--"}} => This is a ve--
Expand All @@ -50,64 +50,64 @@
* string is not shorter than the original.
*/
class ExcerptFilter implements Filter {
const DEFAULT_APPEND_STRING = " [...]";
const DEFAULT_APPEND_STRING = " [...]";

public static function applyTo($string, $args, &$escape, $phpfile) {
if (count($args) > 3) {
throw new TemplateSyntaxException(
'The "excerpt" filter supports only three arguments.');
}
$phpfile->addFunction('excerptFilter',
array('$str', '$num', '$needle', '$append'), <<<'PHP'
if ($str = trim($str)) {
$cutpos = 0;
if ($needle === false)
$cutpos = $num;
else {
$steps = 0;
$findpos = 0;
while ($steps < $num && $findpos !== false) {
$findpos = strpos($str, $needle, $findpos + 1);
$steps++;
}
if ($findpos)
$cutpos = $findpos;
}
if ($cutpos && strlen($str) > $cutpos)
return substr($str, 0, $cutpos) . $append;
}
return $str;
public static function applyTo($string, $args, &$escape, $phpfile) {
if (count($args) > 3) {
throw new TemplateSyntaxException(
'The "excerpt" filter supports only three arguments.');
}
$phpfile->addFunction('excerptFilter',
array('$str', '$num', '$needle', '$append'), <<<'PHP'
if ($str = trim($str)) {
$cutpos = 0;
if ($needle === false)
$cutpos = $num;
else {
$steps = 0;
$findpos = 0;
while ($steps < $num && $findpos !== false) {
$findpos = strpos($str, $needle, $findpos + 1);
$steps++;
}
if ($findpos)
$cutpos = $findpos;
}
if ($cutpos && strlen($str) > $cutpos)
return substr($str, 0, $cutpos) . $append;
}
return $str;
PHP
);
$num = isset($args[0]) ? $args[0]->getValue($phpfile) : 20;
$mode = isset($args[1]) ? trim($args[1]->getValue($phpfile), "'") : 'w';
$append = isset($args[2]) ? $args[2]->getValue($phpfile) : false;
if ($append === false) {
$append = Config::getVal('view', 'excerpt_append') ?:
self::DEFAULT_APPEND_STRING;
$append = "'" . str_replace("'", '\\\'', $append) . "'";
}
$needle = false;
switch($mode) {
case 'l':
$needle = '"\n"';
break;
case 'c':
$needle = 'false';
break;
case 'w':
default:
$needle = '" "';
}
// Manually handle the escaping here just in case excerptFilter needs
// to append something that can't be escaped.
if ($escape) {
$string = "htmlentities($string)";
$escape = false;
}
return "excerptFilter($string, $num, $needle, $append)";
}
);
$num = isset($args[0]) ? $args[0]->getValue($phpfile) : 20;
$mode = isset($args[1]) ? trim($args[1]->getValue($phpfile), "'") : 'w';
$append = isset($args[2]) ? $args[2]->getValue($phpfile) : false;
if ($append === false) {
$append = Config::getVal('view', 'excerpt_append') ?:
self::DEFAULT_APPEND_STRING;
$append = "'" . str_replace("'", '\\\'', $append) . "'";
}
$needle = false;
switch($mode) {
case 'l':
$needle = '"\n"';
break;
case 'c':
$needle = 'false';
break;
case 'w':
default:
$needle = '" "';
}
// Manually handle the escaping here just in case excerptFilter needs
// to append something that can't be escaped.
if ($escape) {
$string = "htmlentities($string)";
$escape = false;
}
return "excerptFilter($string, $num, $needle, $append)";
}
}

?>
?>
3 changes: 3 additions & 0 deletions view/engines/hydrogen/filters/FilesizeformatFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public static function applyTo($string, $args, &$escape, $phpfile) {
}
$size = (string)number_format($size, $decimals);
while (($char = $size[strlen($size) - 1]) === '0' || $char === '.')
if(!isset($size[1]))
break;
$size = substr($size, 0, -1);
}
return $size . ' ' . $type;
PHP
);
Expand Down
26 changes: 26 additions & 0 deletions view/engines/hydrogen/tags/IconTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* Copyright (c) 2009 - 2011, Frosted Design
* All rights reserved.
*/

namespace hydrogen\view\engines\hydrogen\tags;

use hydrogen\view\engines\hydrogen\Tag;
use hydrogen\view\engines\hydrogen\nodes\TextNode;
use hydrogen\config\Config;

class IconTag extends Tag {

public static function getNode($cmd, $args, $parser, $origin) {
$i = (is_array($args))?$args[0]:$args;
$icon = sprintf("%s/%s/%s.png",
Config::getVal("general", "app_url"),
Config::getVal("misc","icondir"),
$i);
return new TextNode($icon, $origin);
}

}

?>