diff options
author | JoshyPHP <s9e.dev@gmail.com> | 2015-12-05 07:55:52 +0100 |
---|---|---|
committer | JoshyPHP <s9e.dev@gmail.com> | 2015-12-26 14:19:22 +0100 |
commit | eb6ceb963ea7b32ef852b53d3da3a7e795291359 (patch) | |
tree | 0a5c9966ee763907b3bff5b6c8e2372857928b7e /phpBB | |
parent | 28969511d2c2942d8a5d4aebe3aede2ccb10c0ba (diff) | |
download | forums-eb6ceb963ea7b32ef852b53d3da3a7e795291359.tar forums-eb6ceb963ea7b32ef852b53d3da3a7e795291359.tar.gz forums-eb6ceb963ea7b32ef852b53d3da3a7e795291359.tar.bz2 forums-eb6ceb963ea7b32ef852b53d3da3a7e795291359.tar.xz forums-eb6ceb963ea7b32ef852b53d3da3a7e795291359.zip |
[ticket/14323] Added support for truncating long URLs
PHPBB3-14323
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/factory.php | 28 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/parser.php | 40 |
2 files changed, 66 insertions, 2 deletions
diff --git a/phpBB/phpbb/textformatter/s9e/factory.php b/phpBB/phpbb/textformatter/s9e/factory.php index 63b23d2fd0..dd3102d4de 100644 --- a/phpBB/phpbb/textformatter/s9e/factory.php +++ b/phpBB/phpbb/textformatter/s9e/factory.php @@ -332,8 +332,7 @@ class factory implements \phpbb\textformatter\cache_interface } // Load the magic links plugins. We do that after BBCodes so that they use the same tags - $configurator->plugins->load('Autoemail'); - $configurator->plugins->load('Autolink', array('matchWww' => true)); + $this->configure_autolink($configurator); // Register some vars with a default value. Those should be set at runtime by whatever calls // the parser @@ -395,6 +394,31 @@ class factory implements \phpbb\textformatter\cache_interface } /** + * Configure the Autolink / Autoemail plugins used to linkify text + * + * @param \s9e\TextFormatter\Configurator $configurator + * @return void + */ + protected function configure_autolink(Configurator $configurator) + { + $configurator->plugins->load('Autoemail'); + $configurator->plugins->load('Autolink', array('matchWww' => true)); + + // Create a tag that will be used to display the truncated text by replacing the original + // content with the content of the @text attribute + $tag = $configurator->tags->add('AUTOLINK_TEXT'); + $tag->attributes->add('text'); + $tag->template = '<xsl:value-of select="@text"/>'; + + // Add a tag filter that replaces the text of links that were created by the Autolink plugin + $configurator->Autolink->getTag()->filterChain + ->add(__NAMESPACE__ . '\\parser::generate_autolink_text') + ->resetParameters() + ->addParameterByName('tag') + ->addParameterByName('parser'); + } + + /** * Return the default BBCodes configuration * * @return array 2D array. Each element has a 'usage' key, a 'template' key, and an optional 'options' key diff --git a/phpBB/phpbb/textformatter/s9e/parser.php b/phpBB/phpbb/textformatter/s9e/parser.php index ffaffbc63c..faddc806cd 100644 --- a/phpBB/phpbb/textformatter/s9e/parser.php +++ b/phpBB/phpbb/textformatter/s9e/parser.php @@ -393,4 +393,44 @@ class parser implements \phpbb\textformatter\parser_interface return $url; } + + /** + * Replace the content displayed inside of a URL tag + * + * Will only apply to URL tags that do not use any markup (e.g. not "[url]") on the assumption + * that those tags were created by the Autolink plugin to linkify URLs found in plain text + * + * @param \s9e\TextFormatter\Parser\Tag $url_tag URL tag (start tag) + * @param \s9e\TextFormatter\Parser $parser Parser + * @return bool Always TRUE to indicate that the tag is valid + */ + public static function generate_autolink_text(\s9e\TextFormatter\Parser\Tag $url_tag, \s9e\TextFormatter\Parser $parser) + { + // If the tag consumes any text then we ignore it because it's not a linkified URL. Same if + // it's not paired with an end tag that doesn't consume any text either + if ($url_tag->getLen() > 0 || !$url_tag->getEndTag()) + { + return true; + } + + // Capture the text between the start tag and its end tag + $start = $url_tag->getPos(); + $end = $url_tag->getEndTag()->getPos(); + $length = $end - $start; + $text = substr($parser->getText(), $start, $length); + + if ($length <= 55 || utf8_strlen($text) <= 55) + { + // Do not do anything if the text is not longer than 55 characters + return true; + } + + $tag = $parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length); + $url_tag->cascadeInvalidationTo($tag); + + $text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10); + $tag->setAttribute('text', $text); + + return true; + } } |