Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/fabpot/Twig-extensions
Browse files Browse the repository at this point in the history
Conflicts:
	Extension/Text.php
  • Loading branch information
henrikbjorn committed Mar 25, 2010
2 parents bf48b7c + 33fde50 commit 43960f3
Showing 1 changed file with 77 additions and 77 deletions.
154 changes: 77 additions & 77 deletions Extension/Text.php
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of Twig.
*
Expand All @@ -13,92 +14,91 @@
*/
class Twig_Extension_Text extends Twig_Extension
{
/**
* Returns a list of filters.
*
* @return array
*/
public function getFilters()
{
return 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)),
'nl2br' => new Twig_Filter_Function('nl2br'),
);
}
/**
* Name of this extension
*
* @return string
*/
public function getName()
{
return 'Text';
}
/**
* Returns a list of filters.
*
* @return array
*/
public function getFilters()
{
return 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)),
'nl2br' => new Twig_Filter_Function('nl2br'),
);
}

/**
* Name of this extension
*
* @return string
*/
public function getName()
{
return 'Text';
}
}

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 mb_substr($value, 0, $length, $env->getCharset()) . $separator;
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 $value;

}

return mb_substr($value, 0, $length, $env->getCharset()) . $separator;
}
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);

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 substr($value, 0, $length) . $separator;
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 $value;
}

function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
{
return wordwrap($value, $length, $separator, !$preserve);

}

return 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);
}
}

0 comments on commit 43960f3

Please sign in to comment.