Skip to content

Commit c036061

Browse files
committed
Actually parse the wt2Html
1 parent 9e880d5 commit c036061

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

includes/Parsoid/ParsoidPortableInfoboxRenderService.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Wikimedia\Parsoid\Core\Sanitizer;
1010
use Wikimedia\Parsoid\DOM\Document;
1111
use Wikimedia\Parsoid\DOM\Element;
12+
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
1213
use Wikimedia\Parsoid\Utils\DOMCompat;
1314

1415
class ParsoidPortableInfoboxRenderService {
@@ -41,31 +42,34 @@ public function __construct() {
4142
* @param array $params
4243
* @return void
4344
*/
44-
private function buildParamMap( array $params ): void {
45-
// loop over all of the parameters we received and if they have both a value
46-
// and a name, then add them to the array - if they do not have a "wt" value, then the value was
47-
// empty and therefore we should not render this part of the infobox
48-
// At this point, we have what we pretty much have what we pass to the legacy function on line
49-
// 104 of PortableInfoboxParserTagController::class
50-
foreach ( $params as $param ) {
51-
if ( isset( $param->k ) && isset( $param->valueWt ) ) {
52-
$this->paramMap[ $param->k ] = $param->valueWt;
53-
}
54-
}
55-
}
45+
private function buildParamMap( ParsoidExtensionAPI $extApi, array $params ): void {
46+
// loop over all of the parameters we received and if they have both a value
47+
// and a name, then add them to the array - if they do not have a "valueWt" value, then the value was
48+
// empty and therefore we should not render this part of the infobox
49+
// At this point, we have what we pretty much have what we pass to the legacy function on line
50+
// 104 of PortableInfoboxParserTagController::class
51+
foreach ( $params as $param ) {
52+
if ( isset( $param->k ) && isset( $param->valueWt ) ) {
53+
$htmlValue = $this->processWikitextToHtml( $extApi, $param->valueWt );
54+
$this->paramMap[ $param->k ] = $htmlValue;
55+
}
56+
}
57+
}
58+
5659

5760
/**
5861
* This function is responsible for rendering the actual infobox to the DOM
5962
* This is the entrypoint which should be called to render the infobox from the
6063
* DOMProcessor. This will delegate appropriately
6164
*/
6265
public function render(
66+
ParsoidExtensionAPI $extApi,
6367
Element $container,
6468
Document $doc,
6569
array $params,
6670
string $parsoidData
6771
): void {
68-
$this->buildParamMap( $params );
72+
$this->buildParamMap( $extApi, $params );
6973
[ $data, $attr ] = $this->prepareInfobox( $parsoidData, $this->paramMap ?: [] );
7074

7175
$themes = $this->getThemes( $attr );
@@ -100,7 +104,6 @@ public function prepareInfobox(
100104
string $parsoidData,
101105
array $params,
102106
): array {
103-
$this->buildParamMap( $params );
104107

105108
// same as legacy!
106109
$infoboxNode = NodeFactory::newFromXML( $parsoidData, $this->paramMap ?: [] );
@@ -488,4 +491,33 @@ private function createHorizontalGroupData( array $groupData ) {
488491
return $horizontalGroupData;
489492
}
490493

494+
/**
495+
* A utility function to parse wikitext to HTML which will be passed into the infobox
496+
* template
497+
* @param \Wikimedia\Parsoid\Ext\ParsoidExtensionAPI $extApi
498+
* @param string $wikitext
499+
* @return string
500+
*/
501+
private function processWikitextToHtml( ParsoidExtensionAPI $extApi, string $wikitext ): string {
502+
503+
$paramParsed = $extApi->wikitextToDOM( $wikitext, [
504+
'processInNewFrame' => true,
505+
'parseOpts' => [ 'context' => 'inline' ]
506+
], true );
507+
508+
509+
$htmlContent = '';
510+
// this feels really hacky?!
511+
if ( $paramParsed->hasChildNodes() ) {
512+
513+
$tempDoc = $paramParsed->ownerDocument;
514+
515+
foreach ( $paramParsed->childNodes as $childNode ) {
516+
$htmlContent .= $tempDoc->saveHTML( $childNode );
517+
}
518+
}
519+
520+
return $htmlContent;
521+
}
522+
491523
}

includes/Parsoid/PortableInfoboxDOMProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function wtPostprocess(
5555
// which is something akin to what PortableInfoboxParserTagController::renderInfobox()
5656
// expects to be passed, albeit we'll need to fudge it a bit!
5757
$params = $part->paramInfos ?? [];
58-
58+
5959
$portableInfoboxRenderService = new ParsoidPortableInfoboxRenderService();
6060

61-
$portableInfoboxRenderService->render( $child, $doc, $params, $parsoidData );
61+
$portableInfoboxRenderService->render( $extApi, $child, $doc, $params, $parsoidData );
6262
}
6363

6464
}

0 commit comments

Comments
 (0)