Split and streamline HTML formatter code

Bug: T301337
Change-Id: Ic2dac65e6e54411abc33326abf8d0fab148cb784
This commit is contained in:
Thiemo Kreuz 2022-02-03 11:30:21 +01:00 committed by Thiemo Kreuz (WMDE)
parent 5407262418
commit b1a4a27531
2 changed files with 48 additions and 42 deletions

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\TemplateData;
use Html;
use MessageLocalizer;
use stdClass;
class TemplateDataHtmlFormatter {
@ -91,7 +92,8 @@ class TemplateDataHtmlFormatter {
. '</tr></thead>'
. '<tbody>';
if ( count( (array)$data->params ) === 0 ) {
$paramNames = $data->paramOrder ?? array_keys( (array)$data->params );
if ( !$paramNames ) {
// Display no parameters message
$html .= '<tr>'
. Html::element( 'td',
@ -104,33 +106,36 @@ class TemplateDataHtmlFormatter {
. '</tr>';
}
$paramNames = $data->paramOrder ?? array_keys( (array)$data->params );
foreach ( $paramNames as $paramName ) {
$param = $data->params->$paramName;
$html .= $this->formatParameterTableRow( $paramName, $data->params->$paramName );
}
$html .= '</tbody></table>';
$aliases = '';
if ( count( $param->aliases ) ) {
return Html::rawElement( 'section', [ 'class' => 'mw-templatedata-doc-wrap' ], $html );
}
/**
* @param int|string $paramName
* @param stdClass $param
*
* @return string HTML
*/
private function formatParameterTableRow( $paramName, stdClass $param ): string {
'@phan-var object $param';
$allParamNames = [ Html::element( 'code', [], $paramName ) ];
foreach ( $param->aliases as $alias ) {
$aliases .= $this->localizer->msg( 'word-separator' )->escaped()
. Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$allParamNames[] = Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$alias
);
}
}
$suggestedValuesLine = '';
if ( $param->suggestedvalues ) {
$suggestedValues = '';
$suggestedValues = [];
foreach ( $param->suggestedvalues as $suggestedValue ) {
$suggestedValues .= $this->localizer->msg( 'word-separator' )->escaped()
. Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$suggestedValues[] = Html::element( 'code', [ 'class' => 'mw-templatedata-doc-param-alias' ],
$suggestedValue
);
}
$suggestedValuesLine .= Html::element( 'dt', [],
$this->localizer->msg( 'templatedata-doc-param-suggestedvalues' )->text()
) . Html::rawElement( 'dd', [], $suggestedValues );
}
if ( $param->deprecated ) {
$status = 'deprecated';
@ -142,12 +147,12 @@ class TemplateDataHtmlFormatter {
$status = 'optional';
}
$html .= '<tr>'
return '<tr>'
// Label
. Html::element( 'th', [], $param->label ?? $paramName )
// Parameters and aliases
. Html::rawElement( 'td', [ 'class' => 'mw-templatedata-doc-param-name' ],
Html::element( 'code', [], $paramName ) . $aliases
implode( $this->localizer->msg( 'word-separator' )->escaped(), $allParamNames )
)
// Description
. Html::rawElement( 'td', [
@ -161,7 +166,12 @@ class TemplateDataHtmlFormatter {
)
. Html::rawElement( 'dl', [],
// Suggested Values
$suggestedValuesLine .
( $suggestedValues ? ( Html::element( 'dt', [],
$this->localizer->msg( 'templatedata-doc-param-suggestedvalues' )->text()
)
. Html::rawElement( 'dd', [],
implode( $this->localizer->msg( 'word-separator' )->escaped(), $suggestedValues )
) ) : '' ) .
// Default
( $param->default !== null ? ( Html::element( 'dt', [],
$this->localizer->msg( 'templatedata-doc-param-default' )->text()
@ -227,9 +237,5 @@ class TemplateDataHtmlFormatter {
)
. '</tr>';
}
$html .= '</tbody></table>';
return Html::rawElement( 'section', [ 'class' => 'mw-templatedata-doc-wrap' ], $html );
}
}

View file

@ -1503,7 +1503,7 @@ HTML
<td class="">
<p>Param docs</p>
<dl>
<dt>(templatedata-doc-param-suggestedvalues)</dt><dd>(word-separator)<code class="mw-templatedata-doc-param-alias">Suggested1</code>(word-separator)<code class="mw-templatedata-doc-param-alias">Suggested2</code></dd>
<dt>(templatedata-doc-param-suggestedvalues)</dt><dd><code class="mw-templatedata-doc-param-alias">Suggested1</code>(word-separator)<code class="mw-templatedata-doc-param-alias">Suggested2</code></dd>
<dt>(templatedata-doc-param-default)</dt><dd>Default docs</dd>
<dt>(templatedata-doc-param-example)</dt><dd>Example docs</dd>
<dt>(templatedata-doc-param-autovalue)</dt><dd><code>Auto value</code></dd>