2023-02-16 19:11:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TabberNeue
|
|
|
|
* TabberParsoid Class
|
|
|
|
* Implement <tabber> tag in Parsoid
|
|
|
|
*
|
|
|
|
* @package TabberNeue
|
|
|
|
* @author alistair3149, Eric Fortin, Alexia E. Smith, Ciencia Al Poder
|
|
|
|
* @license GPL-3.0-or-later
|
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:TabberNeue
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\TabberNeue;
|
|
|
|
|
2023-07-06 01:47:50 +00:00
|
|
|
use Wikimedia\Parsoid\Ext\ExtensionModule;
|
2023-02-16 19:11:17 +00:00
|
|
|
use Wikimedia\Parsoid\Ext\ExtensionTagHandler;
|
|
|
|
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
|
|
|
|
|
2023-07-06 01:47:50 +00:00
|
|
|
class TabberParsoid extends ExtensionTagHandler implements ExtensionModule {
|
|
|
|
/** @inheritDoc */
|
|
|
|
public function getConfig(): array {
|
2023-07-06 01:48:00 +00:00
|
|
|
return [
|
|
|
|
'name' => 'TabberNeue',
|
2023-07-06 01:47:50 +00:00
|
|
|
'tags' => [
|
|
|
|
[
|
|
|
|
'name' => 'tabber',
|
|
|
|
'handler' => self::class
|
|
|
|
]
|
|
|
|
]
|
2023-07-06 01:48:00 +00:00
|
|
|
];
|
|
|
|
}
|
2023-07-06 01:47:50 +00:00
|
|
|
|
2023-02-16 19:11:17 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
|
|
|
|
$html = self::render( $extApi, $src );
|
2023-07-12 02:06:57 +00:00
|
|
|
$extApi->getMetadata()->addModules( [ 'ext.tabberNeue.codex' ] );
|
2023-02-16 19:11:17 +00:00
|
|
|
return $extApi->htmlToDom( $html );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the necessary HTML for a <tabber> tag.
|
|
|
|
*
|
2023-07-12 02:06:57 +00:00
|
|
|
* @param ParsoidExtensionAPI $extApi
|
2023-02-16 19:11:17 +00:00
|
|
|
* @param string $src The input URL between the beginning and ending tags.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2023-07-12 02:06:57 +00:00
|
|
|
public static function render( ParsoidExtensionAPI $extApi, string $src ): string {
|
|
|
|
$arr = explode( '|-|', $src );
|
2023-02-16 19:11:17 +00:00
|
|
|
$htmlTabs = '';
|
|
|
|
foreach ( $arr as $tab ) {
|
|
|
|
$htmlTabs .= self::buildTab( $extApi, $tab );
|
|
|
|
}
|
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
return '<div class="tabber">' .
|
2023-07-06 01:47:50 +00:00
|
|
|
'<header class="tabber__header"></header>' .
|
2023-02-16 19:11:17 +00:00
|
|
|
'<section class="tabber__section">' . $htmlTabs . "</section></div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build individual tab.
|
|
|
|
*
|
2023-07-12 02:06:57 +00:00
|
|
|
* @param ParsoidExtensionAPI $extApi
|
2023-02-16 19:11:17 +00:00
|
|
|
* @param string $tab Tab information
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2023-07-12 02:06:57 +00:00
|
|
|
private static function buildTab( ParsoidExtensionAPI $extApi, string $tab ): string {
|
2023-02-16 19:11:17 +00:00
|
|
|
if ( empty( trim( $tab ) ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
2023-07-12 02:06:57 +00:00
|
|
|
[ $tabName, $tabBody ] = array_pad( explode( '=', $tab, 2 ), 2, '' );
|
2023-02-16 19:11:17 +00:00
|
|
|
|
2023-07-06 01:47:50 +00:00
|
|
|
/*
|
|
|
|
* Use language converter to get variant title and also escape html
|
|
|
|
* FIXME: No replacement method yet
|
|
|
|
* See T85581, T272943
|
|
|
|
*/
|
|
|
|
// $tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) );
|
2023-02-16 19:11:17 +00:00
|
|
|
$tabBody = $extApi->domToHTML(
|
2023-07-12 02:06:57 +00:00
|
|
|
$extApi->wikitextToDOM(
|
|
|
|
$tabBody,
|
|
|
|
[
|
|
|
|
'parseOpts' => [
|
|
|
|
'extTag' => 'tabber',
|
|
|
|
'context' => 'inline',
|
|
|
|
]
|
|
|
|
],
|
2024-04-24 19:13:26 +00:00
|
|
|
true // sol
|
2023-07-12 02:06:57 +00:00
|
|
|
)
|
|
|
|
);
|
2023-02-16 19:11:17 +00:00
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
return '<article class="tabber__panel" title="' . $tabName .
|
2023-02-16 19:11:17 +00:00
|
|
|
'">' . $tabBody . '</article>';
|
|
|
|
}
|
|
|
|
}
|