From 8069e235fd2971675ee5fc05026ffa9bce5cbbb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 2 Aug 2013 22:43:56 +0200 Subject: move Twig in 3rdparty --- inc/3rdparty/Twig/Extensions/Extension/Debug.php | 34 +++++++ inc/3rdparty/Twig/Extensions/Extension/I18n.php | 44 +++++++++ inc/3rdparty/Twig/Extensions/Extension/Intl.php | 66 ++++++++++++++ inc/3rdparty/Twig/Extensions/Extension/Text.php | 109 +++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 inc/3rdparty/Twig/Extensions/Extension/Debug.php create mode 100644 inc/3rdparty/Twig/Extensions/Extension/I18n.php create mode 100644 inc/3rdparty/Twig/Extensions/Extension/Intl.php create mode 100644 inc/3rdparty/Twig/Extensions/Extension/Text.php (limited to 'inc/3rdparty/Twig/Extensions/Extension') diff --git a/inc/3rdparty/Twig/Extensions/Extension/Debug.php b/inc/3rdparty/Twig/Extensions/Extension/Debug.php new file mode 100644 index 00000000..8974ce20 --- /dev/null +++ b/inc/3rdparty/Twig/Extensions/Extension/Debug.php @@ -0,0 +1,34 @@ + new Twig_Filter_Function('gettext'), + ); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'i18n'; + } +} diff --git a/inc/3rdparty/Twig/Extensions/Extension/Intl.php b/inc/3rdparty/Twig/Extensions/Extension/Intl.php new file mode 100644 index 00000000..40f7fc20 --- /dev/null +++ b/inc/3rdparty/Twig/Extensions/Extension/Intl.php @@ -0,0 +1,66 @@ + new Twig_Filter_Function('twig_localized_date_filter', array('needs_environment' => true)), + ); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'intl'; + } +} + +function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) +{ + $date = twig_date_converter($env, $date, $timezone); + + $formatValues = array( + 'none' => IntlDateFormatter::NONE, + 'short' => IntlDateFormatter::SHORT, + 'medium' => IntlDateFormatter::MEDIUM, + 'long' => IntlDateFormatter::LONG, + 'full' => IntlDateFormatter::FULL, + ); + + $formatter = IntlDateFormatter::create( + $locale !== null ? $locale : Locale::getDefault(), + $formatValues[$dateFormat], + $formatValues[$timeFormat], + $date->getTimezone()->getName(), + IntlDateFormatter::GREGORIAN, + $format + ); + + return $formatter->format($date->getTimestamp()); +} diff --git a/inc/3rdparty/Twig/Extensions/Extension/Text.php b/inc/3rdparty/Twig/Extensions/Extension/Text.php new file mode 100644 index 00000000..0a3dc35e --- /dev/null +++ b/inc/3rdparty/Twig/Extensions/Extension/Text.php @@ -0,0 +1,109 @@ + + * @package Twig + * @subpackage Twig-extensions + */ +class Twig_Extensions_Extension_Text extends Twig_Extension +{ + /** + * Returns a list of filters. + * + * @return array + */ + public function getFilters() + { + $filters = array( + 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)), + 'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)), + ); + + if (version_compare(Twig_Environment::VERSION, '1.5.0-DEV', '<')) { + $filters['nl2br'] = new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html'))); + } + + return $filters; + } + + /** + * Name of this extension + * + * @return string + */ + public function getName() + { + return 'Text'; + } +} + +function twig_nl2br_filter($value, $sep = '
') +{ + return str_replace("\n", $sep."\n", $value); +} + +if (function_exists('mb_get_info')) { + function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') + { + if (mb_strlen($value, $env->getCharset()) > $length) { + if ($preserve) { + if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) { + $length = $breakpoint; + } + } + + return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator; + } + + return $value; + } + + function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) + { + $sentences = array(); + + $previous = mb_regex_encoding(); + mb_regex_encoding($env->getCharset()); + + $pieces = mb_split($separator, $value); + mb_regex_encoding($previous); + + foreach ($pieces as $piece) { + while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) { + $sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); + $piece = mb_substr($piece, $length, 2048, $env->getCharset()); + } + + $sentences[] = $piece; + } + + return implode($separator, $sentences); + } +} else { + function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') + { + if (strlen($value) > $length) { + if ($preserve) { + if (false !== ($breakpoint = strpos($value, ' ', $length))) { + $length = $breakpoint; + } + } + + return rtrim(substr($value, 0, $length)) . $separator; + } + + return $value; + } + + function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) + { + return wordwrap($value, $length, $separator, !$preserve); + } +} \ No newline at end of file -- cgit v1.2.3