-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: ParsoidModule implementation #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
originalauthority
wants to merge
57
commits into
Universal-Omega:main
Choose a base branch
from
originalauthority:parsoid-patch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
c446e95
Initial DOMProcessor
originalauthority 97603af
Bump version to 1.0 so we can track which functions were added when
originalauthority 79a4dc5
Begin InfoboxTag and Controller
originalauthority 368842e
More updates
originalauthority 45ad372
Fix parameter name
originalauthority 06e2fa1
change function name
originalauthority 69f2e95
First pass at Parsoid compat render service
originalauthority 59e2e63
fix if statement
originalauthority e360d7e
remove duplicate declaration
originalauthority 7bfdafc
Further work on generating infobox template
originalauthority 07156bf
Don't add the classes to the wrapper in this hook
originalauthority 841d6f3
Add the modules back
originalauthority f4e0c7e
Use DOMCompat instead of constructing a DOMDocument
originalauthority 9e880d5
Simplify this abit using DOMCompat
originalauthority c036061
Actually parse the wt2Html
originalauthority 3698cc3
Create new parser for Parsoid
originalauthority aa812cd
Don't parse the wikitext here, it will be parsed later by ParsoidMed…
originalauthority a40626d
introduce a MediaNode specifically for Parsoid
originalauthority 9bcc4e6
Pass the parser here
originalauthority 4641e51
Refactor this to only check the instance once
originalauthority c4b3fbb
Do some hacky regex to get our galleries!
originalauthority f19719e
Add missing consts - this class should be refactored into an abstract…
originalauthority 32b9fb9
New nodes
originalauthority 44ea661
if just one image, render it
originalauthority 8c73325
Merge pull request #1 from OAuthority/parsoid-complex
originalauthority 0bb334c
[chore]: split a lot of the stuff into a abstract class to reduce dup…
originalauthority bc90daa
Move the render media function into abstract
originalauthority 8a24cbd
Change func. name
originalauthority de7e5d6
Delete unused TagController
originalauthority f74f02a
Merge pull request #2 from OAuthority/parsoid-complex
originalauthority badca54
fix fatal error when an image isn't passed
originalauthority e108e0b
Fix null return error when a media node is unused but exists
originalauthority d9073ef
chore: back working on this - add note about Parsoid config options
originalauthority 1389a0b
CI: Lint to MediaWiki standards
originalauthority fdf0dab
Add Parsoid tests
originalauthority a721f73
Merge branch 'main' into parsoid-patch
Universal-Omega 36745b8
Fix some phan failures
Universal-Omega d567e14
Fix some phan errors
Universal-Omega fa0d5e9
Fix phan failures
Universal-Omega 4df0e3f
Fix
Universal-Omega 6169502
Fix phan errors
Universal-Omega 9200f1b
Fix
Universal-Omega 6dc9c5a
Remove unused variable
Universal-Omega 0b1faf9
Fix
Universal-Omega 49a032f
-
Universal-Omega 1978d60
Remove 1.43 and 1.44 CI
Universal-Omega 7c88b4b
Require 1.45
Universal-Omega 3f80508
Fixes
Universal-Omega 4fc98a0
Fix
Universal-Omega b684233
Fix phan
Universal-Omega 67695ba
Fix
Universal-Omega 466e885
Fix
Universal-Omega 4342523
Replace deprecated methods for Phan (hopefully correctly)
Universal-Omega 6352638
Fix phan
Universal-Omega 85dd853
Add @covers
Universal-Omega 246f856
+ add test case for Cite
originalauthority 9962d5c
+ add some wikitext to infobox test
originalauthority File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| namespace PortableInfobox\Parsoid; | ||
|
|
||
| use Wikimedia\Parsoid\Core\ContentMetadataCollectorStringSets as CMCSS; | ||
| use Wikimedia\Parsoid\Ext\ExtensionModule; | ||
| use Wikimedia\Parsoid\Ext\ExtensionTagHandler; | ||
| use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; | ||
|
|
||
| class InfoboxTag extends ExtensionTagHandler implements ExtensionModule { | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getConfig(): array { | ||
| return [ | ||
| 'name' => 'PortableInfobox', | ||
| 'tags' => [ | ||
| [ | ||
| 'name' => 'infobox', | ||
| 'handler' => self::class, | ||
| ], | ||
| ], | ||
| 'domProcessors' => [ | ||
| 'PortableInfobox\\Parsoid\\PortableInfoboxDOMProcessor', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function sourceToDom( ParsoidExtensionAPI $api, string $src, array $args ) { | ||
| $domFragments = $api->extTagToDOM( $args, $src, [ | ||
| 'wrapperTag' => 'aside', | ||
| 'parseOpts' => [ | ||
| 'extTag' => 'infobox', | ||
| 'context' => 'inline', | ||
| ], | ||
| ] ); | ||
|
|
||
| $api->getMetadata()->appendOutputStrings( CMCSS::MODULE_STYLE, [ 'ext.PortableInfobox.styles' ] ); | ||
| $api->getMetadata()->appendOutputStrings( CMCSS::MODULE, [ 'ext.PortableInfobox.scripts' ] ); | ||
|
|
||
| // return this back. At this point, we have constructed the outer tag (<aside class=...</aside>) | ||
| // and this function is done with its work. The rest of the work will happen in the DOMProcessor | ||
| return $domFragments; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| namespace PortableInfobox\Parsoid; | ||
|
|
||
| use MediaWiki\Title\Title; | ||
| use PortableInfobox\Services\Parser\ExternalParser; | ||
| use Wikimedia\Parsoid\DOM\Element; | ||
| use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; | ||
| use Wikimedia\Parsoid\Utils\DOMCompat; | ||
|
|
||
| class ParsoidMediaWikiParser implements ExternalParser { | ||
|
|
||
| public function __construct( | ||
| private readonly ParsoidExtensionAPI $api, | ||
| ) { | ||
| } | ||
|
|
||
| public function parseRecursive( $wikitext ) { | ||
| if ( $wikitext === null ) { | ||
| return null; | ||
| } | ||
|
|
||
| $paramParsed = $this->api->wikitextToDOM( $wikitext, [ | ||
| // this differs from earlier as we need the frame to be able to grab the | ||
| // params the user passed - parsoid handles this internally it appears | ||
| 'processInNewFrame' => false, | ||
| 'parseOpts' => [ 'context' => 'inline' ], | ||
| ], true ); | ||
| '@phan-var Element $paramParsed'; | ||
|
|
||
| // we don't want Parsoid to wrap in a span or add a typeof here, | ||
| // just interested in the content | ||
| return DOMCompat::getOuterHTML( $paramParsed ); | ||
| } | ||
|
|
||
| /** @param $wikitext @phan-unused-param */ | ||
| public function replaceVariables( $wikitext ) { | ||
| // no-op - I think handled by ->wikiTextToDOM? | ||
| } | ||
|
Comment on lines
+37
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should |
||
|
|
||
| /** | ||
| * @param $title @phan-unused-param | ||
| * @param array $sizeParams @phan-unused-param | ||
| */ | ||
| public function addImage( $title, array $sizeParams ): ?string { | ||
| // no-op at present. Used to extend the image tag with specific information for the PageImages extension. | ||
| // that extension relies on Parser hooks and therefore is not safe to assume that will work indefinitely. | ||
| // could potentially just do it for now whilst the hooks still exist, and maybe remove at a later date if | ||
| // PageImages is not made Parsoid-compat. | ||
| return ''; | ||
| } | ||
|
|
||
| public function getParsoidExtensionApi(): ParsoidExtensionAPI { | ||
| return $this->api; | ||
| } | ||
|
|
||
| /** | ||
| * Extract the gallery and return the filename -> captions. PortableInfobox currently does this | ||
| * a lot cleaner as it piggybacks on onAfterParserFetchFileAndTitle hook to set the images into the data bag. | ||
| * This hook is NOT available on Parsoid, and we have no other way to get the resultant class which PortableInfobox | ||
| * currently relies on. So we need to fake it as best we can and hope WMF comes up with something later down | ||
| * the line. | ||
| * @param string $wikitext | ||
| * @return array an array of the images | ||
| */ | ||
| public function extractGallery( string $wikitext ): array { | ||
| // the legacy implementation reuturns an array where each element is an array of the caption | ||
| // and the title object for that specific image. We don't have access to this by default, | ||
| // since there is no concept of half parsing in Parsoid - we either ask Parsoid for the Parsed wt->html | ||
| // or we work with the WT and grab what we need. | ||
| $result = []; | ||
|
|
||
| // this is quicker than passing the wikitext to Parsoid and extracting | ||
| // the images etc from it. | ||
| if ( preg_match( '/<gallery[^>]*>(.*?)<\/gallery>/s', $wikitext, $matches ) ) { | ||
| $galleryContent = trim( $matches[1] ); | ||
| if ( !$galleryContent ) { | ||
| return []; | ||
| } | ||
|
|
||
| $lines = explode( "\n", $galleryContent ); | ||
| foreach ( $lines as $line ) { | ||
| $line = trim( $line ); | ||
| if ( !$line ) { | ||
| continue; | ||
| } | ||
|
|
||
| $parts = explode( '|', $line, 2 ); | ||
| $filename = trim( $parts[0] ); | ||
| $caption = trim( $parts[1] ?? '' ); | ||
| if ( !$filename ) { | ||
| continue; | ||
| } | ||
|
|
||
| $title = Title::newFromText( $filename, NS_FILE ); | ||
| if ( $title !== null ) { | ||
| $result[] = [ | ||
| 'label' => $caption, | ||
| 'title' => $title, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment seems to be no longer true? I'm not seeing any difference between
trueandfalsefor this value.We actually had an issue where DSRs were off unless this value was
true, but that doesn't seem to be the case anymore. Parsoid update maybe?