kopia lustrzana https://github.com/friendica/friendica
Create event for html2bbcode hook
rodzic
a94bead5f6
commit
3d3e34865b
|
@ -282,11 +282,13 @@ class OEmbed
|
|||
}
|
||||
}
|
||||
} elseif (!strpos($oembed->html, $oembed->embed_url)) {
|
||||
// add <a> for html2bbcode conversion
|
||||
// add <a> for html to bbcode conversion
|
||||
$ret .= '<a href="' . $oembed->embed_url . '" rel="oembed">' . $oembed->title . '</a>';
|
||||
}
|
||||
|
||||
$ret .= '</div>';
|
||||
|
||||
// FIXME: Why is $test unused?
|
||||
$test = Proxy::proxifyHtml($ret, $uriid);
|
||||
|
||||
return str_replace("\n", "", $ret);
|
||||
|
|
|
@ -10,10 +10,10 @@ namespace Friendica\Content\Text;
|
|||
use DOMDocument;
|
||||
use DOMXPath;
|
||||
use Friendica\Protocol\HTTP\MediaType;
|
||||
use Friendica\Core\Hook;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Core\Search;
|
||||
use Friendica\DI;
|
||||
use Friendica\Event\ArrayFilterEvent;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Util\Strings;
|
||||
use Friendica\Util\XML;
|
||||
|
@ -141,7 +141,9 @@ class HTML
|
|||
DI::profiler()->startRecording('rendering');
|
||||
$message = str_replace("\r", "", $message);
|
||||
|
||||
$message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) {
|
||||
$eventDispatcher = DI::eventDispatcher();
|
||||
|
||||
$message = Strings::performWithEscapedBlocks($message, '#<pre><code.*</code></pre>#iUs', function ($message) use($eventDispatcher) {
|
||||
$message = str_replace(
|
||||
[
|
||||
"<li><p>",
|
||||
|
@ -314,7 +316,13 @@ class HTML
|
|||
$message = preg_replace('=\r *\r=i', "\n", $message);
|
||||
$message = str_replace("\r", "\n", $message);
|
||||
|
||||
Hook::callAll('html2bbcode', $message);
|
||||
$message_data = ['html2bbcode' => $message];
|
||||
|
||||
$message_data = $eventDispatcher->dispatch(
|
||||
new ArrayFilterEvent(ArrayFilterEvent::HTML_TO_BBCODE_END, $message_data),
|
||||
)->getArray();
|
||||
|
||||
$message = $message_data['html2bbcode'] ?? $message;
|
||||
|
||||
$message = strip_tags($message);
|
||||
|
||||
|
|
|
@ -56,6 +56,7 @@ final class HookEventBridge
|
|||
ArrayFilterEvent::PAGE_INFO => 'page_info_data',
|
||||
ArrayFilterEvent::SMILEY_LIST => 'smilie',
|
||||
ArrayFilterEvent::BBCODE_TO_HTML_START => 'bbcode',
|
||||
ArrayFilterEvent::HTML_TO_BBCODE_END => 'html2bbcode',
|
||||
ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'bb2diaspora',
|
||||
HtmlFilterEvent::HEAD => 'head',
|
||||
HtmlFilterEvent::FOOTER => 'footer',
|
||||
|
@ -92,6 +93,7 @@ final class HookEventBridge
|
|||
ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::BBCODE_TO_HTML_START => 'onBbcodeToHtmlEvent',
|
||||
ArrayFilterEvent::HTML_TO_BBCODE_END => 'onHtmlToBbcodeEvent',
|
||||
ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'onBbcodeToMarkdownEvent',
|
||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||
|
@ -147,6 +149,20 @@ final class HookEventBridge
|
|||
$event->setArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the HTML_TO_BBCODE_END event to `html2bbcode` hook
|
||||
*/
|
||||
public static function onHtmlToBbcodeEvent(ArrayFilterEvent $event): void
|
||||
{
|
||||
$data = $event->getArray();
|
||||
|
||||
$html2bbcode = (string) $data['html2bbcode'] ?? '';
|
||||
|
||||
$data['html2bbcode'] = static::callHook($event->getName(), $html2bbcode);
|
||||
|
||||
$event->setArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the BBCODE_TO_MARKDOWN_END event to `bb2diaspora` hook
|
||||
*/
|
||||
|
|
|
@ -50,6 +50,8 @@ final class ArrayFilterEvent extends Event
|
|||
|
||||
public const BBCODE_TO_HTML_START = 'friendica.data.bbcode_to_html_start';
|
||||
|
||||
public const HTML_TO_BBCODE_END = 'friendica.data.html_to_bbcode_end';
|
||||
|
||||
public const BBCODE_TO_MARKDOWN_END = 'friendica.data.bbcode_to_markdown_end';
|
||||
|
||||
private array $array;
|
||||
|
|
|
@ -45,6 +45,7 @@ class HookEventBridgeTest extends TestCase
|
|||
ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent',
|
||||
ArrayFilterEvent::BBCODE_TO_HTML_START => 'onBbcodeToHtmlEvent',
|
||||
ArrayFilterEvent::HTML_TO_BBCODE_END => 'onHtmlToBbcodeEvent',
|
||||
ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'onBbcodeToMarkdownEvent',
|
||||
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
|
||||
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
|
||||
|
@ -205,6 +206,28 @@ class HookEventBridgeTest extends TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testOnHtmlToBbcodeEventCallsHookWithCorrectValue(): void
|
||||
{
|
||||
$event = new ArrayFilterEvent(ArrayFilterEvent::HTML_TO_BBCODE_END, ['html2bbcode' => '<b>original</b>']);
|
||||
|
||||
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
|
||||
$reflectionProperty->setValue(null, function (string $name, string $data): string {
|
||||
$this->assertSame('html2bbcode', $name);
|
||||
$this->assertSame('<b>original</b>', $data);
|
||||
|
||||
return '[b]changed[/b]';
|
||||
});
|
||||
|
||||
HookEventBridge::onHtmlToBbcodeEvent($event);
|
||||
|
||||
$this->assertSame(
|
||||
['html2bbcode' => '[b]changed[/b]'],
|
||||
$event->getArray(),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOnBbcodeToMarkdownEventCallsHookWithCorrectValue(): void
|
||||
{
|
||||
$event = new ArrayFilterEvent(ArrayFilterEvent::BBCODE_TO_MARKDOWN_END, ['bbcode2markdown' => '[b]original[/b]']);
|
||||
|
|
Ładowanie…
Reference in New Issue