2022-04-20 17:50:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TabberNeue
|
|
|
|
* Tabber Class
|
|
|
|
* Implement <tabber> tag
|
|
|
|
*
|
|
|
|
* @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 );
|
|
|
|
|
2022-06-29 21:22:14 +00:00
|
|
|
namespace MediaWiki\Extension\TabberNeue;
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
use JsonException;
|
2023-07-05 21:26:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-04-20 17:50:38 +00:00
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
|
|
|
|
|
|
|
class Tabber {
|
2023-07-12 02:06:57 +00:00
|
|
|
/**
|
|
|
|
* Flag that checks if this is a nested tabber
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $isNested = false;
|
|
|
|
|
|
|
|
private static $useCodex = false;
|
|
|
|
|
2024-04-24 19:13:08 +00:00
|
|
|
private static $parseTabName = false;
|
|
|
|
|
2022-04-20 19:23:45 +00:00
|
|
|
/**
|
|
|
|
* Parser callback for <tabber> tag
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
2023-07-12 02:06:57 +00:00
|
|
|
* @param string|null $input
|
2022-04-20 19:23:45 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
|
|
|
* @return string HTML
|
2022-04-20 19:23:45 +00:00
|
|
|
*/
|
2023-07-12 02:06:57 +00:00
|
|
|
public static function parserHook( ?string $input, array $args, Parser $parser, PPFrame $frame ) {
|
2024-04-24 19:13:08 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
|
|
|
self::$parseTabName = $config->get( 'TabberNeueParseTabName' );
|
|
|
|
self::$useCodex = $config->get( 'TabberNeueUseCodex' );
|
2023-07-12 02:06:57 +00:00
|
|
|
|
2024-01-03 20:00:55 +00:00
|
|
|
$html = self::render( $input ?? '', $parser, $frame );
|
2023-07-05 21:26:33 +00:00
|
|
|
|
2022-04-20 19:23:45 +00:00
|
|
|
if ( $input === null ) {
|
2023-07-12 02:06:57 +00:00
|
|
|
return '';
|
2022-04-20 19:23:45 +00:00
|
|
|
}
|
2023-07-05 21:26:33 +00:00
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
if ( self::$useCodex === true ) {
|
2023-07-06 03:00:32 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue.codex' ] );
|
|
|
|
} else {
|
2024-05-25 07:02:18 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( [ 'ext.tabberNeue.init.styles' ] );
|
2024-05-25 04:33:22 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue' ] );
|
2023-07-05 21:26:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 17:08:50 +00:00
|
|
|
$parser->addTrackingCategory( 'tabberneue-tabber-category' );
|
2022-04-20 19:32:19 +00:00
|
|
|
return $html;
|
2022-04-20 19:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 17:50:38 +00:00
|
|
|
/**
|
|
|
|
* Renders the necessary HTML for a <tabber> tag.
|
|
|
|
*
|
|
|
|
* @param string $input The input URL between the beginning and ending tags.
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2023-07-12 02:06:57 +00:00
|
|
|
public static function render( string $input, Parser $parser, PPFrame $frame ): string {
|
|
|
|
$arr = explode( '|-|', $input );
|
2022-04-20 17:50:38 +00:00
|
|
|
$htmlTabs = '';
|
2024-11-15 03:59:52 +00:00
|
|
|
|
2022-04-20 17:50:38 +00:00
|
|
|
foreach ( $arr as $tab ) {
|
2024-11-15 03:59:52 +00:00
|
|
|
$tabData = self::getTabData( $tab, $parser );
|
|
|
|
if ( $tabData['label'] === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$htmlTabs .= self::buildTabpanel( $tabData, $parser, $frame );
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:06:57 +00:00
|
|
|
if ( self::$useCodex && self::$isNested ) {
|
|
|
|
$tab = rtrim( implode( '},', explode( '}', $htmlTabs ) ), ',' );
|
|
|
|
$tab = strip_tags( html_entity_decode( $tab ) );
|
|
|
|
$tab = str_replace( ',,', ',', $tab );
|
|
|
|
$tab = str_replace( ',]', ']', $tab );
|
|
|
|
|
|
|
|
return sprintf( '[%s]', $tab );
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<div class="tabber">' .
|
2023-07-05 22:32:18 +00:00
|
|
|
'<header class="tabber__header"></header>' .
|
2022-05-06 21:45:05 +00:00
|
|
|
'<section class="tabber__section">' . $htmlTabs . '</section></div>';
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-11-15 03:59:52 +00:00
|
|
|
* Get parsed tab labels
|
2022-04-20 17:50:38 +00:00
|
|
|
*
|
2024-11-15 03:59:52 +00:00
|
|
|
* @param string $label tab label wikitext
|
2022-04-20 17:50:38 +00:00
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
*
|
2024-11-15 03:59:52 +00:00
|
|
|
* @return string
|
2022-04-20 17:50:38 +00:00
|
|
|
*/
|
2024-11-15 03:59:52 +00:00
|
|
|
private static function getTabLabel( string $label, Parser $parser ): string {
|
|
|
|
$label = trim( $label );
|
|
|
|
if ( $label === '' ) {
|
2022-06-05 19:13:24 +00:00
|
|
|
return '';
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 03:59:52 +00:00
|
|
|
if ( !self::$parseTabName || self::$useCodex ) {
|
|
|
|
// Only plain text is needed
|
|
|
|
// Use language converter to get variant title and also escape html
|
|
|
|
$label = $parser->getTargetLanguageConverter()->convertHtml( $label );
|
|
|
|
} else {
|
|
|
|
// Might contains HTML
|
|
|
|
$label = $parser->recursiveTagParseFully( $label );
|
|
|
|
$label = $parser->stripOuterParagraph( $label );
|
|
|
|
$label = htmlentities( $label );
|
|
|
|
}
|
|
|
|
return $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parsed tab content
|
|
|
|
*
|
|
|
|
* @param string $content tab content wikitext
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function getTabContent( string $content ): string {
|
|
|
|
$content = trim( $content );
|
|
|
|
if ( $content === '' ) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-06-23 20:30:52 +00:00
|
|
|
// Fix #151
|
2024-11-15 03:59:52 +00:00
|
|
|
$content = "\n" . $content;
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get individual tab data from wikitext.
|
|
|
|
*
|
|
|
|
* @param string $tab tab wikitext
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
*
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
private static function getTabData( string $tab, Parser $parser ): array {
|
|
|
|
$data = [
|
|
|
|
'label' => '',
|
|
|
|
'content' => ''
|
|
|
|
];
|
|
|
|
if ( empty( trim( $tab ) ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
|
|
|
[ $label, $content ] = array_pad( explode( '=', $tab, 2 ), 2, '' );
|
|
|
|
|
|
|
|
$data['label'] = self::getTabLabel( $label, $parser );
|
|
|
|
// Label is empty, we cannot generate tabber
|
|
|
|
if ( $data['label'] === '' ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['content'] = self::getTabContent( $content );
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build individual tabpanel.
|
|
|
|
*
|
|
|
|
* @param array $tabData Tab data
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
|
|
|
private static function buildTabpanel( array $tabData, Parser $parser, PPFrame $frame ): string {
|
|
|
|
$tabName = $tabData['label'];
|
|
|
|
$tabBody = $tabData['content'];
|
2023-07-12 02:06:57 +00:00
|
|
|
|
2024-04-24 19:13:08 +00:00
|
|
|
// Codex mode
|
|
|
|
if ( self::$useCodex ) {
|
|
|
|
// A nested tabber which should return json in codex
|
|
|
|
if ( strpos( $tabBody, '{{#tag:tabber' ) !== false ) {
|
|
|
|
self::$isNested = true;
|
|
|
|
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
|
|
|
|
self::$isNested = false;
|
|
|
|
// The outermost tabber that must be parsed fully in codex for correct json
|
|
|
|
} else {
|
|
|
|
$tabBody = $parser->recursiveTagParseFully( $tabBody, $frame );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( self::$isNested ) {
|
|
|
|
return json_encode( [
|
|
|
|
'label' => $tabName,
|
|
|
|
'content' => $tabBody
|
|
|
|
],
|
|
|
|
JSON_THROW_ON_ERROR
|
|
|
|
);
|
|
|
|
}
|
2023-07-12 02:06:57 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 19:13:08 +00:00
|
|
|
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
|
2023-07-06 03:07:57 +00:00
|
|
|
|
|
|
|
// If $tabBody does not have any HTML element (i.e. just a text node), wrap it in <p/>
|
2023-11-05 23:10:02 +00:00
|
|
|
if ( $tabBody && $tabBody[0] !== '<' ) {
|
2023-07-06 03:07:57 +00:00
|
|
|
$tabBody = '<p>' . $tabBody . '</p>';
|
|
|
|
}
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2024-06-23 20:30:52 +00:00
|
|
|
// \n is needed for #151
|
2024-05-24 02:05:28 +00:00
|
|
|
return '<article class="tabber__panel" data-mw-tabber-title="' . $tabName .
|
2024-06-23 20:37:11 +00:00
|
|
|
'">' . $tabBody . "</article>\n";
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
}
|