mirror of
https://github.com/StarCitizenTools/mediawiki-extensions-TabberNeue.git
synced 2024-11-24 00:13:28 +00:00
refactor: more function clean up
This commit is contained in:
parent
a6de7f9519
commit
21ad702ee2
|
@ -76,11 +76,17 @@ class Tabber {
|
||||||
$htmlTabs = '';
|
$htmlTabs = '';
|
||||||
|
|
||||||
foreach ( $arr as $tab ) {
|
foreach ( $arr as $tab ) {
|
||||||
$tabData = self::getTabData( $tab, $parser );
|
$tabData = self::getTabData( $tab, $parser, $frame );
|
||||||
if ( $tabData['label'] === '' ) {
|
if ( $tabData['label'] === '' ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$htmlTabs .= self::buildTabpanel( $tabData, $parser, $frame );
|
|
||||||
|
if ( self::$useCodex && self::$isNested ) {
|
||||||
|
$htmlTabs .= self::getCodexNestedTabJSON( $tabData );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$htmlTabs .= self::buildTabpanel( $tabData );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( self::$useCodex && self::$isNested ) {
|
if ( self::$useCodex && self::$isNested ) {
|
||||||
|
@ -128,16 +134,31 @@ class Tabber {
|
||||||
* Get parsed tab content
|
* Get parsed tab content
|
||||||
*
|
*
|
||||||
* @param string $content tab content wikitext
|
* @param string $content tab content wikitext
|
||||||
|
* @param Parser $parser Mediawiki Parser Object
|
||||||
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private static function getTabContent( string $content ): string {
|
private static function getTabContent( string $content, Parser $parser, PPFrame $frame ): string {
|
||||||
$content = trim( $content );
|
$content = trim( $content );
|
||||||
if ( $content === '' ) {
|
if ( $content === '' ) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
// Fix #151
|
// Fix #151, some wikitext magic
|
||||||
$content = "\n" . $content;
|
$content = "\n" . $content . "\n";
|
||||||
|
if ( !self::$useCodex ) {
|
||||||
|
$content = $parser->recursiveTagParse( $content, $frame );
|
||||||
|
} else {
|
||||||
|
// A nested tabber which should return json in codex
|
||||||
|
if ( strpos( $content, '{{#tag:tabber' ) !== false ) {
|
||||||
|
self::$isNested = true;
|
||||||
|
$content = $parser->recursiveTagParse( $content, $frame );
|
||||||
|
self::$isNested = false;
|
||||||
|
// The outermost tabber that must be parsed fully in codex for correct json
|
||||||
|
} else {
|
||||||
|
$content = $parser->recursiveTagParseFully( $content, $frame );
|
||||||
|
}
|
||||||
|
}
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,10 +167,11 @@ class Tabber {
|
||||||
*
|
*
|
||||||
* @param string $tab tab wikitext
|
* @param string $tab tab wikitext
|
||||||
* @param Parser $parser Mediawiki Parser Object
|
* @param Parser $parser Mediawiki Parser Object
|
||||||
|
* @param PPFrame $frame Mediawiki PPFrame Object
|
||||||
*
|
*
|
||||||
* @return array<string, string>
|
* @return array<string, string>
|
||||||
*/
|
*/
|
||||||
private static function getTabData( string $tab, Parser $parser ): array {
|
private static function getTabData( string $tab, Parser $parser, PPFrame $frame ): array {
|
||||||
$data = [
|
$data = [
|
||||||
'label' => '',
|
'label' => '',
|
||||||
'content' => ''
|
'content' => ''
|
||||||
|
@ -166,7 +188,7 @@ class Tabber {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['content'] = self::getTabContent( $content );
|
$data['content'] = self::getTabContent( $content, $parser, $frame );
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,47 +196,38 @@ class Tabber {
|
||||||
* Build individual tabpanel.
|
* Build individual tabpanel.
|
||||||
*
|
*
|
||||||
* @param array $tabData Tab data
|
* @param array $tabData Tab data
|
||||||
* @param Parser $parser Mediawiki Parser Object
|
*
|
||||||
* @param PPFrame $frame Mediawiki PPFrame Object
|
* @return string HTML
|
||||||
|
*/
|
||||||
|
private static function buildTabpanel( array $tabData ): string {
|
||||||
|
$label = $tabData['label'];
|
||||||
|
$content = $tabData['content'];
|
||||||
|
|
||||||
|
$isContentHTML = strpos( $content, '<' ) === 0;
|
||||||
|
if ( $content && !$isContentHTML ) {
|
||||||
|
// If $content does not have any HTML element (i.e. just a text node), wrap it in <p/>
|
||||||
|
$content = '<p>' . $content . '</p>';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '<article class="tabber__panel" data-mw-tabber-title="' . $label .
|
||||||
|
'">' . $content . "</article>";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get JSON representation of a nested tab for Codex
|
||||||
|
*
|
||||||
|
* @param array $tabData Tab data
|
||||||
*
|
*
|
||||||
* @return string HTML
|
* @return string HTML
|
||||||
* @throws JsonException
|
* @throws JsonException
|
||||||
*/
|
*/
|
||||||
private static function buildTabpanel( array $tabData, Parser $parser, PPFrame $frame ): string {
|
private static function getCodexNestedTabJSON( array $tabData ): string {
|
||||||
$tabName = $tabData['label'];
|
|
||||||
$tabBody = $tabData['content'];
|
|
||||||
|
|
||||||
// Codex mode
|
|
||||||
if ( self::$useCodex ) {
|
|
||||||
// A nested tabber which should return json in codex
|
// 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( [
|
return json_encode( [
|
||||||
'label' => $tabName,
|
'label' => $tabData['label'],
|
||||||
'content' => $tabBody
|
'content' => $tabData['content']
|
||||||
],
|
],
|
||||||
JSON_THROW_ON_ERROR
|
JSON_THROW_ON_ERROR
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tabBody = $parser->recursiveTagParse( $tabBody, $frame );
|
|
||||||
|
|
||||||
// If $tabBody does not have any HTML element (i.e. just a text node), wrap it in <p/>
|
|
||||||
if ( $tabBody && $tabBody[0] !== '<' ) {
|
|
||||||
$tabBody = '<p>' . $tabBody . '</p>';
|
|
||||||
}
|
|
||||||
|
|
||||||
// \n is needed for #151
|
|
||||||
return '<article class="tabber__panel" data-mw-tabber-title="' . $tabName .
|
|
||||||
'">' . $tabBody . "</article>\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -40,7 +40,6 @@ function initApp( tabber ) {
|
||||||
*/
|
*/
|
||||||
function main() {
|
function main() {
|
||||||
const tabbers = document.querySelectorAll( '.tabber:not( .tabber--live )' );
|
const tabbers = document.querySelectorAll( '.tabber:not( .tabber--live )' );
|
||||||
|
|
||||||
tabbers.forEach( initApp );
|
tabbers.forEach( initApp );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue