Commit 9a4ffead by Scott

Merge pull request #250 from pupi1985/patch-59

Better snippets in the mouseover-layer plugin
parents f3363623 e16cfb97
...@@ -485,45 +485,50 @@ ...@@ -485,45 +485,50 @@
} }
function qa_shorten_string_line($string, $length) /**
/* * Converts a string to a single line and removes words from it until it fits in the given length. Words are removed
Return no more than $length characters from $string after converting it to a single line, by * from a position around two thirds of the string and are replaced by the given ellipsis string
removing words from the middle (and especially towards the end) *
*/ * @param string $string Text that will be turned into a single line and cut, if necessary
* @param int $length Maximum allowed length of the returned string. This value can be overriden by the length of the
* ellipsis if it is higher than the maximum allowed length
* @param string $ellipsis Text used to replace the removed words from the original text
* @return string The string turned into a single line and cut to fit the given length
*/
function qa_shorten_string_line($string, $length, $ellipsis = ' ... ')
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$string=strtr($string, "\r\n\t", ' '); $string = strtr($string, "\r\n\t", ' ');
if (qa_strlen($string)>$length) { if (qa_strlen($string) > $length) {
$remaining=$length-5; $remaining = $length - qa_strlen($ellipsis);
$words=qa_string_to_words($string, false, true); $words = qa_string_to_words($string, false, true);
$countwords=count($words); $countwords = count($words);
$prefix=''; $prefix = '';
$suffix=''; $suffix = '';
for ($addword=0; $addword<$countwords; $addword++) { for ($addword = 0; $addword < $countwords; $addword++) {
$tosuffix=(($addword%3)==1); // order: prefix, suffix, prefix, prefix, suffix, prefix, ... $tosuffix = $addword % 3 == 1; // order: prefix, suffix, prefix, prefix, suffix, prefix, ...
if ($tosuffix) $word = $tosuffix ? array_pop($words) : array_shift($words);
$word=array_pop($words);
else $wordLength = qa_strlen($word);
$word=array_shift($words);
if (qa_strlen($word)>$remaining) if ($wordLength > $remaining)
break; break;
if ($tosuffix) if ($tosuffix)
$suffix=$word.$suffix; $suffix = $word . $suffix;
else else
$prefix.=$word; $prefix .= $word;
$remaining-=qa_strlen($word); $remaining -= $wordLength;
} }
$string=$prefix.' ... '.$suffix; $string = $prefix . $ellipsis . $suffix;
} }
return $string; return $string;
......
...@@ -31,16 +31,15 @@ class qa_html_theme_layer extends qa_html_theme_base ...@@ -31,16 +31,15 @@ class qa_html_theme_layer extends qa_html_theme_base
$postids = array(); $postids = array();
foreach ($q_list['qs'] as $question) { foreach ($q_list['qs'] as $question) {
if (isset($question['raw']['postid'])) if (isset($question['raw']['postid']))
$postids[] = $question['raw']['postid']; $postids[] = $question['raw']['postid'];
} }
if (!empty($postids)) { if (!empty($postids)) {
// Retrieve the content for these questions from the database and put into an array fetching // Retrieve the content for these questions from the database
// the minimal amount of characters needed to determine the string should be shortened or not
$maxlength = qa_opt('mouseover_content_max_len'); $maxlength = qa_opt('mouseover_content_max_len');
$result = qa_db_query_sub('SELECT postid, LEFT(content, #) content, format FROM ^posts WHERE postid IN (#)', $maxlength + 1, $postids); $result = qa_db_query_sub('SELECT postid, content, format FROM ^posts WHERE postid IN (#)', $postids);
$postinfo = qa_db_read_all_assoc($result, 'postid'); $postinfo = qa_db_read_all_assoc($result, 'postid');
// Get the regular expression fragment to use for blocked words and the maximum length of content to show // Get the regular expression fragment to use for blocked words and the maximum length of content to show
...@@ -53,14 +52,29 @@ class qa_html_theme_layer extends qa_html_theme_base ...@@ -53,14 +52,29 @@ class qa_html_theme_layer extends qa_html_theme_base
if (isset($postinfo[$question['raw']['postid']])) { if (isset($postinfo[$question['raw']['postid']])) {
$thispost = $postinfo[$question['raw']['postid']]; $thispost = $postinfo[$question['raw']['postid']];
$text = qa_viewer_text($thispost['content'], $thispost['format'], array('blockwordspreg' => $blockwordspreg)); $text = qa_viewer_text($thispost['content'], $thispost['format'], array('blockwordspreg' => $blockwordspreg));
$text = preg_replace('/\s+/', ' ', $text); // Remove duplicated blanks, new line characters, tabs, etc
$text = qa_shorten_string_line($text, $maxlength); $text = qa_shorten_string_line($text, $maxlength);
$title = isset($question['title']) ? $question['title'] : ''; $title = isset($question['title']) ? $question['title'] : '';
$q_list['qs'][$index]['title'] = sprintf('<span title="%s">%s</span>', qa_html($text), $title); $q_list['qs'][$index]['title'] = $this->getHtmlTitle(qa_html($text), $title);
} }
} }
} }
} }
qa_html_theme_base::q_list($q_list); // call back through to the default function parent::q_list($q_list); // call back through to the default function
}
/**
* Returns the needed HTML to display the tip. Depending on the theme in use, this might need to be
* tuned in order for the tip to be displayed properly
*
* @access private
* @param string $mouseOverText Text of the tip
* @param string $questionTitle Question title
* @return string HTML needed to display the tip and the question title
*/
private function getHtmlTitle($mouseOverText, $questionTitle)
{
return sprintf('<span title="%s">%s</span>', $mouseOverText, $questionTitle);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment