From 9f5d82dd8ca90fb398c071f73d2e8e9148e1f976 Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Fri, 8 Jul 2011 11:57:01 +0200 Subject: [PATCH 1/7] An 'icon' Tag which generates the url for frequently used icons. This is a very convenient tag for use in combination with the icon sets from famfamfam.com or similar large icon collections. Simply set up a config category named [misc] and define a setting "icondir" where the icon files can be found. Note: This is a very rudimentary implementation. --- view/engines/hydrogen/tags/IconTag.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 view/engines/hydrogen/tags/IconTag.php diff --git a/view/engines/hydrogen/tags/IconTag.php b/view/engines/hydrogen/tags/IconTag.php new file mode 100644 index 0000000..9057bfa --- /dev/null +++ b/view/engines/hydrogen/tags/IconTag.php @@ -0,0 +1,26 @@ + \ No newline at end of file From d5d4a749b86280744d4ccbfcea4e7826a4880e52 Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Fri, 8 Jul 2011 21:25:33 +0200 Subject: [PATCH 2/7] Excerpt filter --- .../hydrogen/filters/ExcerptFilter.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 view/engines/hydrogen/filters/ExcerptFilter.php diff --git a/view/engines/hydrogen/filters/ExcerptFilter.php b/view/engines/hydrogen/filters/ExcerptFilter.php new file mode 100644 index 0000000..37a3876 --- /dev/null +++ b/view/engines/hydrogen/filters/ExcerptFilter.php @@ -0,0 +1,84 @@ + + * {% set myVar %} + * This is a very long Text. + * {% endset %} + * {{myVar|excerpt:5:w}} => This is a very long [...] + * {{myVar|excerpt:12:c}} => This is a ve [...] + * {{myVar|excerpt:3:l}} => This is a very long Text. + * + * As you can see, the third case is not appended with an ellipsis, as the + * filtered string is not shorter than the input. + */ +class ExcerptFilter implements Filter { + + public static function applyTo($string, $args, &$escape, $phpfile) { + $phpfile->addFunction('excerptFilter', + array('$str', '$num', '$needle', '$esc'), <<<'PHP' + $str = trim($str); + $strlen = strlen( $str ); + if($strlen==0) return utf8_encode($str); + $findpos = 0; + $cutpos = 0; + $steps = 0; + if($needle==false) { + $cutpos = $num; + } else { + while( $steps < $num && $findpos!==false) { + $cutpos = $findpos; + $findpos = strpos( $str, $needle, $findpos + 1 ); + $steps++; + } + } + if($cutpos === false || $steps<$num) + return utf8_encode($str); + $ellipsis = ( $strlen > $cutpos ) ? ' [...]' : ''; + return utf8_encode( substr( $str, 0, $cutpos ) . + $ellipsis); +PHP + ); + $num = (isset($args[0])) ? $args[0]->getValue($phpfile) : 20; + $mode = (isset($args[1])) ? $args[1]->getValue($phpfile) : 'w'; + $needle = false; + switch($mode) { + case 'l': + $needle = "\n"; + break; + case 'c': + $needle = false; + break; + case 'w': + default: + $needle = ' '; + } + $string = 'excerptFilter(' . $string . ',' . + $num . ',"' . $needle . '",' . + ($escape ? 'true' : 'false') . ')'; + $escape = false; + return $string; + } + +}"" + +?> \ No newline at end of file From a031d023ca8292c1a1e8818411fa239ed2411759 Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Sun, 9 Oct 2011 11:15:47 +0200 Subject: [PATCH 3/7] strange... --- .../engines/hydrogen/filters/ExerptFilter.php | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 view/engines/hydrogen/filters/ExerptFilter.php diff --git a/view/engines/hydrogen/filters/ExerptFilter.php b/view/engines/hydrogen/filters/ExerptFilter.php new file mode 100644 index 0000000..ed2a35d --- /dev/null +++ b/view/engines/hydrogen/filters/ExerptFilter.php @@ -0,0 +1,113 @@ + + * Config::setVal('view', 'excerpt_append', '…'); + * + * + * Note that if escaping is on, the string will be escaped BEFORE the append + * string is added, so the apprend string can contain HTML or entities. + * + * For special cases where the append string should be different than the + * default, simply supply it as a third argument. + * + * Example usage: + * + *
+ * {% set myVar %}
+ *   This is a very long Text.
+ * {% endset %}
+ * {{myVar|excerpt:5:"w"}} => This is a very long [...]
+ * {{myVar|excerpt:12:"c":"--"}} => This is a ve--
+ * {{myVar|excerpt:3:"l"}} => This is a very long Text.
+ * 
+ * + * Note that the third case is not appended with an ellipsis, as the filtered + * string is not shorter than the original. + */ +class ExcerptFilter implements Filter { + + 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; +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)"; + } +} + +?> From 5f95c2e071d6fe3a3f23b809c61f234e3d5b73ac Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Sun, 9 Oct 2011 13:07:28 +0200 Subject: [PATCH 4/7] feeling like an idiot... --- .../hydrogen/filters/{ExerptFilter.php => ExcerptFilter.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename view/engines/hydrogen/filters/{ExerptFilter.php => ExcerptFilter.php} (99%) diff --git a/view/engines/hydrogen/filters/ExerptFilter.php b/view/engines/hydrogen/filters/ExcerptFilter.php similarity index 99% rename from view/engines/hydrogen/filters/ExerptFilter.php rename to view/engines/hydrogen/filters/ExcerptFilter.php index ed2a35d..0d858bf 100644 --- a/view/engines/hydrogen/filters/ExerptFilter.php +++ b/view/engines/hydrogen/filters/ExcerptFilter.php @@ -78,7 +78,7 @@ public static function applyTo($string, $args, &$escape, $phpfile) { return substr($str, 0, $cutpos) . $append; } return $str; -PHP +PHP; ); $num = isset($args[0]) ? $args[0]->getValue($phpfile) : 20; $mode = isset($args[1]) ? trim($args[1]->getValue($phpfile), "'") : 'w'; From e255c95a817c977b266a79fac32c5a2dcfeb27b7 Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Sun, 9 Oct 2011 14:04:46 +0200 Subject: [PATCH 5/7] ... --- view/engines/hydrogen/filters/ExcerptFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/engines/hydrogen/filters/ExcerptFilter.php b/view/engines/hydrogen/filters/ExcerptFilter.php index 0d858bf..ed2a35d 100644 --- a/view/engines/hydrogen/filters/ExcerptFilter.php +++ b/view/engines/hydrogen/filters/ExcerptFilter.php @@ -78,7 +78,7 @@ public static function applyTo($string, $args, &$escape, $phpfile) { return substr($str, 0, $cutpos) . $append; } return $str; -PHP; +PHP ); $num = isset($args[0]) ? $args[0]->getValue($phpfile) : 20; $mode = isset($args[1]) ? trim($args[1]->getValue($phpfile), "'") : 'w'; From 0f6ee86bfc7499ec88b3c2594b02b0e8283cd06d Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Sun, 9 Oct 2011 16:59:48 +0200 Subject: [PATCH 6/7] ... From 16a9c88176245d5974837b2c465dd5758aaa4eaf Mon Sep 17 00:00:00 2001 From: Johannes Becker Date: Wed, 2 Nov 2011 10:14:54 -0400 Subject: [PATCH 7/7] Fixing FilesizeformatFilter to correctly handle zero byte sizes. --- view/engines/hydrogen/filters/FilesizeformatFilter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/view/engines/hydrogen/filters/FilesizeformatFilter.php b/view/engines/hydrogen/filters/FilesizeformatFilter.php index ca9bf6f..7994a07 100644 --- a/view/engines/hydrogen/filters/FilesizeformatFilter.php +++ b/view/engines/hydrogen/filters/FilesizeformatFilter.php @@ -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 );