feat: add langauge converter support

This should allow MW apply langauge conversion to tab title
This commit is contained in:
alistair3149 2023-02-16 14:10:46 -05:00
parent 365777eae3
commit 874738db2a
No known key found for this signature in database
2 changed files with 88 additions and 86 deletions

View file

@ -87,10 +87,11 @@ class Tabber {
// Use array_pad to make sure at least 2 array values are always returned
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' );
$tabName = trim( $tabName );
// Use language converter to get variant title and also escape html
$tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) );
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
$tab = '<article class="tabber__panel" data-title="' . htmlspecialchars( $tabName ) .
$tab = '<article class="tabber__panel" data-title="' . $tabName .
'">' . $tabBody . '</article>';
return $tab;

View file

@ -1,84 +1,85 @@
<?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;
use Wikimedia\Parsoid\Ext\ExtensionTagHandler;
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
class TabberParsoid extends ExtensionTagHandler {
/** @inheritDoc */
public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
$html = self::render( $extApi, $src );
$extApi->addModules( [ 'ext.tabberNeue' ] );
return $extApi->htmlToDom( $html );
}
/**
* Renders the necessary HTML for a <tabber> tag.
*
* @param PParsoidExtensionAPI $extApi
* @param string $src The input URL between the beginning and ending tags.
*
* @return string HTML
*/
public static function render( ParsoidExtensionAPI $extApi, string $src ) {
$arr = explode( "|-|", $src );
$htmlTabs = '';
foreach ( $arr as $tab ) {
$htmlTabs .= self::buildTab( $extApi, $tab );
}
$html = '<div class="tabber">' .
'<section class="tabber__section">' . $htmlTabs . "</section></div>";
return $html;
}
/**
* Build individual tab.
*
* @param PParsoidExtensionAPI $extApi
* @param string $tab Tab information
*
* @return string HTML
*/
private static function buildTab( ParsoidExtensionAPI $extApi, string $tab ) {
if ( empty( trim( $tab ) ) ) {
return '';
}
// Use array_pad to make sure at least 2 array values are always returned
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' );
$tabName = trim( $tabName );
$tabBody = $extApi->domToHTML(
$extApi->wikitextToDOM(
$tabBody,
[
'parseOpts' => [
'extTag' => 'tabber',
'context' => 'inline',
]
],
true // sol
)
);
$tab = '<article class="tabber__panel" title="' . htmlspecialchars( $tabName ) .
'">' . $tabBody . '</article>';
return $tab;
}
}
<?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;
use Wikimedia\Parsoid\Ext\ExtensionTagHandler;
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
class TabberParsoid extends ExtensionTagHandler {
/** @inheritDoc */
public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
$html = self::render( $extApi, $src );
$extApi->addModules( [ 'ext.tabberNeue' ] );
return $extApi->htmlToDom( $html );
}
/**
* Renders the necessary HTML for a <tabber> tag.
*
* @param PParsoidExtensionAPI $extApi
* @param string $src The input URL between the beginning and ending tags.
*
* @return string HTML
*/
public static function render( ParsoidExtensionAPI $extApi, string $src ) {
$arr = explode( "|-|", $src );
$htmlTabs = '';
foreach ( $arr as $tab ) {
$htmlTabs .= self::buildTab( $extApi, $tab );
}
$html = '<div class="tabber">' .
'<section class="tabber__section">' . $htmlTabs . "</section></div>";
return $html;
}
/**
* Build individual tab.
*
* @param PParsoidExtensionAPI $extApi
* @param string $tab Tab information
*
* @return string HTML
*/
private static function buildTab( ParsoidExtensionAPI $extApi, string $tab ) {
if ( empty( trim( $tab ) ) ) {
return '';
}
// Use array_pad to make sure at least 2 array values are always returned
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' );
// Use language converter to get variant title and also escape html
$tabName = $parser->getTargetLanguageConverter()->convertHtml( trim( $tabName ) );
$tabBody = $extApi->domToHTML(
$extApi->wikitextToDOM(
$tabBody,
[
'parseOpts' => [
'extTag' => 'tabber',
'context' => 'inline',
]
],
true // sol
)
);
$tab = '<article class="tabber__panel" title="' . $tabName .
'">' . $tabBody . '</article>';
return $tab;
}
}