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
|
|
|
|
|
|
|
use Parser;
|
|
|
|
use PPFrame;
|
|
|
|
|
|
|
|
class Tabber {
|
2022-04-20 19:23:45 +00:00
|
|
|
/**
|
|
|
|
* Parser callback for <tabber> tag
|
2022-04-20 19:32:19 +00:00
|
|
|
*
|
2022-04-20 19:23:45 +00:00
|
|
|
* @param string $input
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
public static function parserHook( string $input, array $args, Parser $parser, PPFrame $frame ) {
|
|
|
|
$tabber = new Tabber();
|
2022-04-20 19:32:19 +00:00
|
|
|
$html = $tabber->render( $input, $parser, $frame );
|
2022-04-20 19:23:45 +00:00
|
|
|
if ( $input === null ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 21:45:05 +00:00
|
|
|
// Critial rendering styles
|
|
|
|
// See ext.tabberNeue.inline.less
|
|
|
|
$style = sprintf(
|
|
|
|
'<style id="tabber-style">%s</style>',
|
2023-02-16 19:02:04 +00:00
|
|
|
'.client-js .tabber__header{height:2.6em;box-shadow:inset 0-1px 0 0;opacity:0.1}.client-js .tabber__header:after{position:absolute;width:16ch;height:0.5em;margin-top:1em;margin-left:0.75em;background:#000;border-radius:40px;content:""}.client-js .tabber__panel:not(:first-child){display:none}'
|
2022-05-06 21:45:05 +00:00
|
|
|
);
|
|
|
|
$parser->getOutput()->addHeadItem( $style, true );
|
2022-04-20 19:23:45 +00:00
|
|
|
$parser->getOutput()->addModules( [ 'ext.tabberNeue' ] );
|
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
|
|
|
|
*/
|
2022-04-20 19:23:45 +00:00
|
|
|
public static function render( $input, Parser $parser, PPFrame $frame ) {
|
2022-04-20 17:50:38 +00:00
|
|
|
$arr = explode( "|-|", $input );
|
|
|
|
$htmlTabs = '';
|
|
|
|
foreach ( $arr as $tab ) {
|
|
|
|
$htmlTabs .= self::buildTab( $tab, $parser, $frame );
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:45:05 +00:00
|
|
|
$noscriptMsg = wfMessage( 'tabberneue-noscript' )->text();
|
2022-04-20 17:50:38 +00:00
|
|
|
$html = '<div class="tabber">' .
|
2023-02-16 19:02:04 +00:00
|
|
|
'<header class="tabber__header"><noscript><div class="mw-message-box mw-message-box-warning">' . $noscriptMsg . '</div></noscript></header>' .
|
2022-05-06 21:45:05 +00:00
|
|
|
'<section class="tabber__section">' . $htmlTabs . '</section></div>';
|
2022-04-20 17:50:38 +00:00
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build individual tab.
|
|
|
|
*
|
|
|
|
* @param string $tab Tab information
|
|
|
|
* @param Parser $parser Mediawiki Parser Object
|
|
|
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
private static function buildTab( $tab, Parser $parser, PPFrame $frame ) {
|
2022-06-05 19:13:24 +00:00
|
|
|
if ( empty( trim( $tab ) ) ) {
|
|
|
|
return '';
|
2022-04-20 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use array_pad to make sure at least 2 array values are always returned
|
2022-06-05 19:13:24 +00:00
|
|
|
list( $tabName, $tabBody ) = array_pad( explode( '=', $tab, 2 ), 2, '' );
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2022-06-05 19:13:24 +00:00
|
|
|
$tabName = trim( $tabName );
|
|
|
|
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
|
2022-04-20 17:50:38 +00:00
|
|
|
|
2022-11-02 23:55:46 +00:00
|
|
|
$tab = '<article class="tabber__panel" data-title="' . htmlspecialchars( $tabName ) .
|
2022-04-20 17:50:38 +00:00
|
|
|
'">' . $tabBody . '</article>';
|
|
|
|
|
|
|
|
return $tab;
|
|
|
|
}
|
|
|
|
}
|