From 9e269b92336a448337b02e306ad3c8d409781a85 Mon Sep 17 00:00:00 2001 From: Maxim Kostjukevich Date: Wed, 24 Jul 2013 12:39:50 +0300 Subject: [PATCH 1/3] implementation of auto loading modifiers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Вторая попытка, добавлен массив с модификаторами, а также вывод инклуда файлов при компиляции шаблона. Добавлена папка с модификаторами. --- src/Fenom.php | 16 ++++- src/Fenom/Template.php | 24 ++++++- src/plugins/modifier.capitalize.php | 44 +++++++++++++ src/plugins/modifier.count_characters.php | 40 ++++++++++++ src/plugins/modifier.count_paragraphs.php | 7 ++ src/plugins/modifier.count_sentences.php | 7 ++ src/plugins/modifier.count_words.php | 6 ++ src/plugins/modifier.date_format.php | 21 ++++++ src/plugins/modifier.default.php | 7 ++ src/plugins/modifier.escape.php | 72 +++++++++++++++++++++ src/plugins/modifier.get_query.php | 36 +++++++++++ src/plugins/modifier.hexcolor.php | 5 ++ src/plugins/modifier.indent.php | 7 ++ src/plugins/modifier.ini_get.php | 5 ++ src/plugins/modifier.int2color.php | 5 ++ src/plugins/modifier.mb_truncate.php | 30 +++++++++ src/plugins/modifier.natint.php | 6 ++ src/plugins/modifier.native_json_encode.php | 53 +++++++++++++++ src/plugins/modifier.nl2br.php | 5 ++ src/plugins/modifier.regex_replace.php | 10 +++ src/plugins/modifier.replace.php | 6 ++ src/plugins/modifier.safe_uri.php | 10 +++ src/plugins/modifier.signedint.php | 7 ++ src/plugins/modifier.spacify.php | 6 ++ src/plugins/modifier.spellcount.php | 13 ++++ src/plugins/modifier.string_format.php | 6 ++ src/plugins/modifier.strip.php | 6 ++ src/plugins/modifier.strip_tags.php | 10 +++ src/plugins/modifier.truncate.php | 52 +++++++++++++++ src/plugins/modifier.view_size.php | 18 ++++++ src/plugins/modifier.wordwrap.php | 68 +++++++++++++++++++ src/plugins/shared.make_timestamp.php | 30 +++++++++ 32 files changed, 636 insertions(+), 2 deletions(-) create mode 100644 src/plugins/modifier.capitalize.php create mode 100644 src/plugins/modifier.count_characters.php create mode 100644 src/plugins/modifier.count_paragraphs.php create mode 100644 src/plugins/modifier.count_sentences.php create mode 100644 src/plugins/modifier.count_words.php create mode 100644 src/plugins/modifier.date_format.php create mode 100644 src/plugins/modifier.default.php create mode 100644 src/plugins/modifier.escape.php create mode 100644 src/plugins/modifier.get_query.php create mode 100644 src/plugins/modifier.hexcolor.php create mode 100644 src/plugins/modifier.indent.php create mode 100644 src/plugins/modifier.ini_get.php create mode 100644 src/plugins/modifier.int2color.php create mode 100644 src/plugins/modifier.mb_truncate.php create mode 100644 src/plugins/modifier.natint.php create mode 100644 src/plugins/modifier.native_json_encode.php create mode 100644 src/plugins/modifier.nl2br.php create mode 100644 src/plugins/modifier.regex_replace.php create mode 100644 src/plugins/modifier.replace.php create mode 100644 src/plugins/modifier.safe_uri.php create mode 100644 src/plugins/modifier.signedint.php create mode 100644 src/plugins/modifier.spacify.php create mode 100644 src/plugins/modifier.spellcount.php create mode 100644 src/plugins/modifier.string_format.php create mode 100644 src/plugins/modifier.strip.php create mode 100644 src/plugins/modifier.strip_tags.php create mode 100644 src/plugins/modifier.truncate.php create mode 100644 src/plugins/modifier.view_size.php create mode 100644 src/plugins/modifier.wordwrap.php create mode 100644 src/plugins/shared.make_timestamp.php diff --git a/src/Fenom.php b/src/Fenom.php index f5faffe..434188c 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -108,6 +108,8 @@ class Fenom { "iterable" => 'Fenom\Modifier::isIterable' ); + protected $_plugins = array(); + /** * @var array of allowed PHP functions */ @@ -466,7 +468,14 @@ class Fenom { return $this->_modifiers[$modifier]; } elseif($this->isAllowedFunction($modifier)) { return $modifier; - } else { + } + + $path = __DIR__ . DIRECTORY_SEPARATOR . 'plugins'. DIRECTORY_SEPARATOR . 'modifier.'. $modifier . '.php'; + if (is_file($path) && is_readable($path)) { + $this->_plugins['fenom_modifier_'.$modifier] = $path; + require_once $path; + return 'fenom_modifier_'. $modifier; + } else { throw new \Exception("Modifier $modifier not found"); } } @@ -546,6 +555,11 @@ class Fenom { return $this->_options; } + public function getPlugins() { + return $this->_plugins; + } + + /** * @param bool|string $scm * @return Fenom\ProviderInterface diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 50399fd..a648e65 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -375,12 +375,34 @@ class Template extends Render { ), true).");\n"; } + /** + * Return PHP code for saving plugins include to file + * + * @return string + */ + public function getPluginsCode() { + + $plugins_code = "_fenom->getPlugins() as $name=>$path){ + $plugins_code .= "if (!is_callable('".$name."')) {include_once '".$path."';}"."\n"; + } + + $plugins_code .= " ?>"."\n"; + + return $plugins_code; + } + + /** * Return closure code * @return string */ private function _getClosureSource() { - return "function (\$tpl) {\n?>{$this->_body}getPluginsCode(); + + return "function (\$tpl) {\n?>{$plugins_code}\n{$this->_body} + * Name: capitalize
+ * Purpose: capitalize words in the string + * + * @link http://www.fenom.ru/docs/language.modifiers.tpl#language.modifier.capitalize + * @author + * + * @param string $string string to capitalize + * @param boolean $uc_digits also capitalize "x123" to "X123" + * @param boolean $lc_rest capitalize first letters, lowercase all following letters "aAa" to "Aaa" + * @return string capitalized string + * + */ + +function fenom_modifier_capitalize($string, $uc_digits = false) { + fenom_modifier_capitalize_ucfirst(NULL, $uc_digits); + return preg_replace_callback('!\'?\b\w(\w|\')*\b!', 'fenome_modifier_capitalize_ucfirst', $string); +} + +function fenom_modifier_capitalize_ucfirst($string, $uc_digits = null) { + static $_uc_digits = FALSE; + if (isset($uc_digits)) { + $_uc_digits = $uc_digits; + return; + } + if (substr($string[0], 0, 1) != '\'' && !preg_match('!\d!', $string[0]) || $_uc_digits) { + return ucfirst($string[0]); + } + else { + return $string[0]; + } +} diff --git a/src/plugins/modifier.count_characters.php b/src/plugins/modifier.count_characters.php new file mode 100644 index 0000000..a581ce6 --- /dev/null +++ b/src/plugins/modifier.count_characters.php @@ -0,0 +1,40 @@ + + * Name: count_characters
+ * Purpose: count the number of characters in a text + * + * @link http://www.fenom.ru/docs/language.modifiers.tpl#language.modifier.capitalize + * @author + * + * @param string $string string to count characters + * @param boolean $inckule_spaces flag count whitespaces + * @param boolean $lc_rest capitalize first letters, lowercase all following letters "aAa" to "Aaa" + * @return integer count characters in string + * + */ + + + + + function fenom_modifier_count_characters($string, $include_spaces = FALSE) { + + if ($include_spaces) { + return strlen($string); + } + + return preg_match_all('~\S~', $string, $match); + } + + +?> \ No newline at end of file diff --git a/src/plugins/modifier.count_paragraphs.php b/src/plugins/modifier.count_paragraphs.php new file mode 100644 index 0000000..042a806 --- /dev/null +++ b/src/plugins/modifier.count_paragraphs.php @@ -0,0 +1,7 @@ + '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', ' '<\/')); + } + if ($esc_type == 'mail') { + // safe way to display e-mail address on a web page + return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string); + } + if ($esc_type == 'nonstd') { + // escape non-standard chars, such as ms document quotes + $_res = ''; + for ($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) { + $_ord = ord(substr($string, $_i, 1)); + // non-standard char, escape it + if ($_ord >= 126) { + $_res .= '&#' . $_ord . ';'; + } + else { + $_res .= substr($string, $_i, 1); + } + } + return $_res; + } + return $string; + } \ No newline at end of file diff --git a/src/plugins/modifier.get_query.php b/src/plugins/modifier.get_query.php new file mode 100644 index 0000000..62f2fc5 --- /dev/null +++ b/src/plugins/modifier.get_query.php @@ -0,0 +1,36 @@ + $v) { + if (is_int($k) and isset($_GET[$k])) { + $params_from_get[$k] = $_GET[$k]; + } + else { + $params_sended[$k] = $v; + } + } + if ($all) { + foreach ($_GET as $k => $v) { + if (isset($_REQUEST[$k])) { + $params_from_get[$k] = $v; + } + } + } + $params = array_merge($params_from_get, $params_sended); + foreach ($params as $k => $v) { + if ($v === NULL) { + continue; + } + $query .= ($query !== '' ? $separator : '?') . (is_array($v) ? + http_build_query(array($k => $v), '', $separator) : + $k . '=' . urlencode($v) + ); + } + return $query; + } diff --git a/src/plugins/modifier.hexcolor.php b/src/plugins/modifier.hexcolor.php new file mode 100644 index 0000000..5818026 --- /dev/null +++ b/src/plugins/modifier.hexcolor.php @@ -0,0 +1,5 @@ + $length) { + $length -= mb_strlen($etc); + if (!$break_words && !$middle) { + $string = preg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length + 1)); + } + if (!$middle) { + $r = mb_substr($string, 0, $length) . $etc; + } + else { + $r = mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, -$length / 2); + } + } + else { + $r = $string; + } + if ($old != $encoding) { + mb_internal_encoding($old); + } + return $r; + } diff --git a/src/plugins/modifier.natint.php b/src/plugins/modifier.natint.php new file mode 100644 index 0000000..3747bf9 --- /dev/null +++ b/src/plugins/modifier.natint.php @@ -0,0 +1,6 @@ + 0 ? $int : 0; + } diff --git a/src/plugins/modifier.native_json_encode.php b/src/plugins/modifier.native_json_encode.php new file mode 100644 index 0000000..49f51bc --- /dev/null +++ b/src/plugins/modifier.native_json_encode.php @@ -0,0 +1,53 @@ + $v) { + $result[] = native_json_encode($k) . ':' . native_json_encode($v); + } + return '{' . join(',', $result) . '}'; + } + } + } \ No newline at end of file diff --git a/src/plugins/modifier.nl2br.php b/src/plugins/modifier.nl2br.php new file mode 100644 index 0000000..2785063 --- /dev/null +++ b/src/plugins/modifier.nl2br.php @@ -0,0 +1,5 @@ + 0 ? '+' : '') . $int; + } + diff --git a/src/plugins/modifier.spacify.php b/src/plugins/modifier.spacify.php new file mode 100644 index 0000000..d26c03d --- /dev/null +++ b/src/plugins/modifier.spacify.php @@ -0,0 +1,6 @@ += 2 && $num % 10 <= 4 && ($num % 100 < 10 || $num % 100 >= 20)) { + echo $num . ' ' . $two; + } else { + echo $num . ' ' . $many; + } + } +?> \ No newline at end of file diff --git a/src/plugins/modifier.string_format.php b/src/plugins/modifier.string_format.php new file mode 100644 index 0000000..15bf2c6 --- /dev/null +++ b/src/plugins/modifier.string_format.php @@ -0,0 +1,6 @@ +]*?>~', ' ', $string); + } + else { + return strip_tags($string); + } + } diff --git a/src/plugins/modifier.truncate.php b/src/plugins/modifier.truncate.php new file mode 100644 index 0000000..38e7e6a --- /dev/null +++ b/src/plugins/modifier.truncate.php @@ -0,0 +1,52 @@ + + * Name: truncate
+ * Purpose: Truncate a string to a certain length if necessary, + * optionally splitting in the middle of a word, and + * appending the $etc string or inserting $etc into the middle. + + * @param string $string input string + * @param integer $length length of truncated text + * @param string $etc end string + * @param boolean $by_words truncate at word boundary + * @param boolean $middle truncate in the middle of text + * @return string truncated string + * + * @link http://www.fenom.ru/docs/language.modifier.truncate.tpl truncate + * @author + */ + + function fenom_modifier_truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false) { + if($middle) { + if(preg_match('#^(.{'.$length.'}).*?(.{'.$length.'})?$#usS', $string, $match)) { + if(count($match) == 3) { + if($by_words) { + return preg_replace('#\s.*$#usS', "", $match[1]).$etc.preg_replace('#^.*\s#usS', "", $match[2]); + } else { + return $match[1].$etc.$match[2]; + } + } + } + } else { + if(preg_match('#^(.{'.$length.'})#usS', $string, $match)) { + if($by_words) { + return preg_replace('#\s.*$#usS', "", $match[1]).$etc; + } else { + return $match[1].$etc; + } + } + } + return $string; + } + + diff --git a/src/plugins/modifier.view_size.php b/src/plugins/modifier.view_size.php new file mode 100644 index 0000000..cf73653 --- /dev/null +++ b/src/plugins/modifier.view_size.php @@ -0,0 +1,18 @@ += 1073741824) { + $size = round($size / 1073741824 * 100) / 100 . ' GB'; + } + elseif ($size >= 1048576) { + $size = round($size / 1048576 * 100) / 100 . ' MB'; + } + elseif ($size >= 1024) { + $size = round($size / 1024 * 100) / 100 . ' KB'; + } + else { + $size = $size . ' B'; + } + return $size; + } diff --git a/src/plugins/modifier.wordwrap.php b/src/plugins/modifier.wordwrap.php new file mode 100644 index 0000000..86adb48 --- /dev/null +++ b/src/plugins/modifier.wordwrap.php @@ -0,0 +1,68 @@ + $last_line + $width + 1) { + $formatted .= mb_substr($str, $last_line + 1, $prev_position - $last_line - 1, 'utf-8') . $break; + $last_line = $prev_position; + } + $prev_position = $position; + } + + /// adding last line without the break + $formatted .= mb_substr($str, $last_line + 1, mb_strlen($str), 'utf-8'); + return $formatted; + } + + function strwidth($s) { + $ret = mb_strwidth($s, 'UTF-8'); + return $ret; + } + + function mb_wordwrap($str, $wid, $tag) { + $pos = 0; + $tok = array(); + $l = mb_strlen($str, 'UTF-8'); + if ($l == 0) { + return ''; + } + $flag = false; + $tok[0] = mb_substr($str, 0, 1, 'UTF-8'); + for ($i = 1; $i < $l; ++$i) { + $c = mb_substr($str, $i, 1, 'UTF-8'); + if (!preg_match('/[a-z\'\"]/i', $c)) { + ++$pos; + $flag = true; + } + elseif ($flag) { + ++$pos; + $flag = false; + } + $tok[$pos] .= $c; + } + + $linewidth = 0; + $pos = 0; + $ret = array(); + $l = count($tok); + for ($i = 0; $i < $l; ++$i) { + if ($linewidth + ($w = strwidth($tok[$i])) > $wid) { + ++$pos; + $linewidth = 0; + } + $ret[$pos] .= $tok[$i]; + $linewidth += $w; + } + return implode($tag, $ret); + } + } diff --git a/src/plugins/shared.make_timestamp.php b/src/plugins/shared.make_timestamp.php new file mode 100644 index 0000000..7fbb24f --- /dev/null +++ b/src/plugins/shared.make_timestamp.php @@ -0,0 +1,30 @@ + Date: Wed, 24 Jul 2013 12:53:12 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9E=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ошибка в названии функции, перенес свою функцию из smarty :) --- src/plugins/modifier.spellcount.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/modifier.spellcount.php b/src/plugins/modifier.spellcount.php index d931c2d..4c5c2f6 100644 --- a/src/plugins/modifier.spellcount.php +++ b/src/plugins/modifier.spellcount.php @@ -1,6 +1,6 @@ Date: Wed, 24 Jul 2013 13:29:23 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9E=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=86=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ошибка конца файла --- src/plugins/modifier.native_json_encode.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/plugins/modifier.native_json_encode.php b/src/plugins/modifier.native_json_encode.php index 49f51bc..f16ea84 100644 --- a/src/plugins/modifier.native_json_encode.php +++ b/src/plugins/modifier.native_json_encode.php @@ -50,4 +50,5 @@ return '{' . join(',', $result) . '}'; } } - } \ No newline at end of file + } +?> \ No newline at end of file