1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-16 13:52:03 +02:00

[ticket/14323] Renamed AUTOLINK_TEXT to LINK_TEXT

Expanded link text shortening to [url] BBCodes with no parameters

PHPBB3-14323
This commit is contained in:
JoshyPHP 2015-12-27 16:43:45 +01:00
parent 5c8373dc20
commit 909f8653ec
5 changed files with 31 additions and 37 deletions

View File

@ -26,9 +26,6 @@ services:
text_formatter.utils:
alias: text_formatter.s9e.utils
text_formatter.s9e.autolink_helper:
class: phpbb\textformatter\s9e\autolink_helper
text_formatter.s9e.factory:
class: phpbb\textformatter\s9e\factory
arguments:
@ -36,11 +33,14 @@ services:
- '@cache.driver'
- '@dispatcher'
- '@config'
- '@text_formatter.s9e.autolink_helper'
- '@text_formatter.s9e.link_helper'
- '%text_formatter.cache.dir%'
- '%text_formatter.cache.parser.key%'
- '%text_formatter.cache.renderer.key%'
text_formatter.s9e.link_helper:
class: phpbb\textformatter\s9e\link_helper
text_formatter.s9e.parser:
class: phpbb\textformatter\s9e\parser
arguments:

View File

@ -23,9 +23,9 @@ use s9e\TextFormatter\Configurator\Items\UnsafeTemplate;
class factory implements \phpbb\textformatter\cache_interface
{
/**
* @var \phpbb\textformatter\s9e\autolink_helper
* @var \phpbb\textformatter\s9e\link_helper
*/
protected $autolink_helper;
protected $link_helper;
/**
* @var \phpbb\cache\driver\driver_interface
@ -138,14 +138,14 @@ class factory implements \phpbb\textformatter\cache_interface
* @param \phpbb\cache\driver\driver_interface $cache
* @param \phpbb\event\dispatcher_interface $dispatcher
* @param \phpbb\config\config $config
* @param \phpbb\textformatter\s9e\autolink_helper $autolink_helper
* @param \phpbb\textformatter\s9e\link_helper $link_helper
* @param string $cache_dir Path to the cache dir
* @param string $cache_key_parser Cache key used for the parser
* @param string $cache_key_renderer Cache key used for the renderer
*/
public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, \phpbb\textformatter\s9e\autolink_helper $autolink_helper, $cache_dir, $cache_key_parser, $cache_key_renderer)
public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, \phpbb\textformatter\s9e\link_helper $link_helper, $cache_dir, $cache_key_parser, $cache_key_renderer)
{
$this->autolink_helper = $autolink_helper;
$this->link_helper = $link_helper;
$this->cache = $cache;
$this->cache_dir = $cache_dir;
$this->cache_key_parser = $cache_key_parser;
@ -414,29 +414,28 @@ class factory implements \phpbb\textformatter\cache_interface
// Add a tag filter that creates a tag that stores and replace the
// content of a link created by the Autolink plugin
$configurator->Autolink->getTag()->filterChain
->add(array($this->autolink_helper, 'generate_autolink_text_tag'))
->add(array($this->link_helper, 'generate_link_text_tag'))
->resetParameters()
->addParameterByName('tag')
->addParameterByName('parser');
// 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 = $configurator->tags->add('LINK_TEXT');
$tag->attributes->add('text');
$tag->attributes->add('url', array('required' => false))->filterChain->add('#url');
$tag->template = '<xsl:value-of select="@text"/>';
$tag->filterChain
->add(array($this->autolink_helper, 'truncate_local_url'))
->add(array($this->link_helper, 'truncate_local_url'))
->resetParameters()
->addParameterByName('tag')
->addParameterByValue(generate_board_url() . '/');
$tag->filterChain
->add(array($this->autolink_helper, 'truncate_text'))
->add(array($this->link_helper, 'truncate_text'))
->resetParameters()
->addParameterByName('tag');
$tag->filterChain
->add(array($this->autolink_helper, 'cleanup_tag'))
->add(array($this->link_helper, 'cleanup_tag'))
->resetParameters()
->addParameterByName('tag')
->addParameterByName('parser');

View File

@ -13,23 +13,20 @@
namespace phpbb\textformatter\s9e;
class autolink_helper
class link_helper
{
/**
* Clean up and invalidate an AUTOLINK_TEXT tag if applicable
* Clean up and invalidate a LINK_TEXT tag if applicable
*
* Will invalidate the tag if its replacement text is the same as the original
* text and would have no visible effect
*
* @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_TEXT tag
* @param \s9e\TextFormatter\Parser $parser Parser
* @return bool Whether the tag is valid
*/
public function cleanup_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser)
{
// Remove the url attribute because it's not needed.
$tag->removeAttribute('url');
// Invalidate if the content of the tag matches the text attribute
$text = substr($parser->getText(), $tag->getPos(), $tag->getLen());
@ -37,34 +34,32 @@ class autolink_helper
}
/**
* Create an AUTOLINK_TEXT tag inside of a link created by the Autolink plugin
* Create a LINK_TEXT tag inside of a link
*
* 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
* Meant to only apply to linkified URLs and [url] BBCodes without a parameter
*
* @param \s9e\TextFormatter\Parser\Tag $tag URL tag (start tag)
* @param \s9e\TextFormatter\Parser $parser Parser
* @return bool Always true to indicate that the tag is valid
*/
public function generate_autolink_text_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser)
public function generate_link_text_tag(\s9e\TextFormatter\Parser\Tag $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 ($tag->getLen() > 0 || !$tag->getEndTag())
// Only create a LINK_TEXT tag if the start tag is paired with an end
// tag, which is the case with tags from the Autolink plugins and with
// the [url] BBCode when its content is used for the URL
if (!$tag->getEndTag())
{
return true;
}
// Capture the text between the start tag and its end tag
$start = $tag->getPos();
$start = $tag->getPos() + $tag->getLen();
$end = $tag->getEndTag()->getPos();
$length = $end - $start;
$text = substr($parser->getText(), $start, $length);
// Create a tag that consumes the link's text
$parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length)->setAttribute('text', $text);
$parser->addSelfClosingTag('LINK_TEXT', $start, $length)->setAttribute('text', $text);
return true;
}
@ -72,7 +67,7 @@ class autolink_helper
/**
* Remove the board's root URL from a the start of a string
*
* @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_TEXT tag
* @param string $board_url Forum's root URL (with trailing slash)
* @return bool Always true to indicate that the tag is valid
*/
@ -88,9 +83,9 @@ class autolink_helper
}
/**
* Truncate the replacement text set in an AUTOLINK_TEXT tag
* Truncate the replacement text set in a LINK_TEXT tag
*
* @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_TEXT tag
* @return bool Always true to indicate that the tag is valid
*/
public function truncate_text(\s9e\TextFormatter\Parser\Tag $tag)

View File

@ -501,7 +501,7 @@ class phpbb_test_case_helpers
}
// Create and register the text_formatter.s9e.factory service
$factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, new \phpbb\textformatter\s9e\autolink_helper, $cache_dir, $cache_key_parser, $cache_key_renderer);
$factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, new \phpbb\textformatter\s9e\link_helper, $cache_dir, $cache_key_parser, $cache_key_renderer);
$container->set('text_formatter.s9e.factory', $factory);
// Create a user if none was provided, and add the common lang strings

View File

@ -50,7 +50,7 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case
$this->cache,
$this->dispatcher,
new \phpbb\config\config(array('allowed_schemes_links' => 'http,https,ftp')),
new \phpbb\textformatter\s9e\autolink_helper,
new \phpbb\textformatter\s9e\link_helper,
$this->get_cache_dir(),
'_foo_parser',
'_foo_renderer'