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..f16ea84
--- /dev/null
+++ b/src/plugins/modifier.native_json_encode.php
@@ -0,0 +1,54 @@
+ $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 @@
+