1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-25 07:50:23 +01:00

rewrite filename truncation code; ref #53

This commit is contained in:
czaks 2014-05-11 14:10:53 +02:00
parent cda2c59a12
commit 5f626acd50
2 changed files with 17 additions and 11 deletions

View File

@ -253,16 +253,6 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) {
return $body;
}
function truncate_filename($filename, $length) {
if (strlen($filename) <= $length) return $filename;
$matches = array();
if (!preg_match("/.*(\\..+)/", $filename, $matches)) return $filename // what, no extension
$length -= strlen($matches[1]);
if ($length <= 0) return '(...)' . $matches[1]; // lmao
$filename = substr($filename, 0, $length) . '(...)' . $matches[1];
return $filename;
}
function bidi_cleanup($data) {
// Closes all embedded RTL and LTR unicode formatting blocks in a string so that
// it can be used inside another without controlling its direction.

View File

@ -13,7 +13,7 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
new Twig_SimpleFilter('filesize', 'format_bytes'),
new Twig_SimpleFilter('truncate', 'twig_truncate_filter'),
new Twig_SimpleFilter('truncate_body', 'truncate'),
new Twig_SimpleFilter('truncate_filename', 'truncate_filename'),
new Twig_SimpleFilter('truncate_filename', 'twig_filename_truncate_filter'),
new Twig_SimpleFilter('extension', 'twig_extension_filter'),
new Twig_SimpleFilter('sprintf', 'sprintf'),
new Twig_SimpleFilter('capcode', 'capcode'),
@ -104,6 +104,22 @@ function twig_truncate_filter($value, $length = 30, $preserve = false, $separato
return $value;
}
function twig_filename_truncate_filter($value, $length = 30, $separator = '&hellip;') {
if (mb_strlen($value) > $length) {
$value = strrev($value);
$array = array_reverse(explode(".", $value, 2));
$array = array_map("strrev", $array);
$filename = &$array[0];
$extension = isset($array[1]) ? $array[1] : false;
$filename = mb_substr($filename, 0, $length - ($extension ? mb_strlen($extension) + 1 : 0)) . $separator;
return implode(".", $array);
}
return $value;
}
function twig_ratio_function($w, $h) {
return fraction($w, $h, ':');
}