Merge pull request #53 from 3edgy6u/master

Truncate filenames properly
This commit is contained in:
Marcin Łabanowski 2014-05-10 21:55:04 +02:00
commit 7b57a4b911
3 changed files with 12 additions and 1 deletions

View File

@ -253,6 +253,16 @@ 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,6 +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('extension', 'twig_extension_filter'),
new Twig_SimpleFilter('sprintf', 'sprintf'),
new Twig_SimpleFilter('capcode', 'capcode'),

View File

@ -22,7 +22,7 @@
{% if config.show_filename and file.filename %}
,
{% if file.filename|length > config.max_filename_display %}
<span class="postfilename" title="{{ file.filename|e }}">{{ file.filename|truncate(config.max_filename_display)|bidi_cleanup }}</span>
<span class="postfilename" title="{{ file.filename|e }}">{{ file.filename|truncate_filename(config.max_filename_display)|bidi_cleanup }}</span>
{% else %}
<span class="postfilename">{{ file.filename|e|bidi_cleanup }}</span>
{% endif %}