[plugin_comments] Fix wordwrap at wound boundaries.

Add ChangeLog.

Signed-off-by: Thomas Hochstein <thh@inter.net>
This commit is contained in:
Thomas Hochstein 2020-03-21 16:01:02 +01:00
parent 9709592b7c
commit 95c71c36c9
2 changed files with 23 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Version 1.16:
------------------------------------------------------------------------
* Fix: wordwrap at word boundaries instead of "truncating" the lines

View File

@ -20,7 +20,7 @@ class serendipity_plugin_comments extends serendipity_plugin
$propbag->add('description', PLUGIN_COMMENTS_BLAHBLAH); $propbag->add('description', PLUGIN_COMMENTS_BLAHBLAH);
$propbag->add('stackable', true); $propbag->add('stackable', true);
$propbag->add('author', 'Garvin Hicking, Tadashi Jokagi, Judebert, G. Brockhaus'); $propbag->add('author', 'Garvin Hicking, Tadashi Jokagi, Judebert, G. Brockhaus');
$propbag->add('version', '1.15'); $propbag->add('version', '1.16');
$propbag->add('requirements', array( $propbag->add('requirements', array(
'serendipity' => '1.6', 'serendipity' => '1.6',
'smarty' => '2.6.7', 'smarty' => '2.6.7',
@ -193,6 +193,7 @@ class serendipity_plugin_comments extends serendipity_plugin
if ($sql && is_array($sql)) { if ($sql && is_array($sql)) {
foreach($sql AS $key => $row) { foreach($sql AS $key => $row) {
# truncate comment to $max_chars
if (function_exists('mb_strimwidth')) { if (function_exists('mb_strimwidth')) {
$comment = mb_strimwidth(strip_tags($row['comment']), 0, $max_chars, " [...]", LANG_CHARSET); $comment = mb_strimwidth(strip_tags($row['comment']), 0, $max_chars, " [...]", LANG_CHARSET);
} else { } else {
@ -228,14 +229,29 @@ class serendipity_plugin_comments extends serendipity_plugin
$user = PLUGIN_COMMENTS_ANONYMOUS; $user = PLUGIN_COMMENTS_ANONYMOUS;
} }
if (function_exists('mb_strimwidth')) { # wrap lines at $wordwrap
if (function_exists('mb_strimwidth') && function_exists('mb_strrpos') && function_exists('mb_substr')) {
$pos = 0; $pos = 0;
$parts = array(); $parts = array();
$enc = LANG_CHARSET; $enc = LANG_CHARSET;
$comment_len = mb_strlen($comment, $enc); $comment_len = mb_strlen($comment, $enc);
# iterate over the (truncated) comment and wrap each line at $wordwrap
while ($pos < $comment_len) { while ($pos < $comment_len) {
$part = mb_strimwidth($comment, $pos, $wordwrap, '', $enc); # do we still need to wrap this line or is it shorter than $wordwrap?
if ($comment_len - $pos > $wordwrap) {
# location of first space
$spacepos = mb_strrpos(mb_substr($comment, $pos, $wordwrap, $enc), ' ', $enc);
# wrap at word boundary if we have at least one space
$part = ( $spacepos > 0 ) ? mb_substr($comment, $pos, $spacepos, $enc) : mb_strimwidth($comment, $pos, $wordwrap, '', $enc);;
} else {
# wrap "hard", i.e. truncate words that are too long
$part = mb_substr($comment, $pos, $wordwrap, $enc);
}
# forward the pointer
$pos += mb_strlen($part, $enc); $pos += mb_strlen($part, $enc);
# remove leading spaces
$part = ltrim($part);
# re-assemble the lines, i.e. add our current line
$parts[] = $part; $parts[] = $part;
} }
$comment = implode("\n", $parts); $comment = implode("\n", $parts);