Repository navigation
[Data Liberation] Add HTML to Blocks converter - #2095
Merged
Merged
Conversation
Adds a basic WP_HTML_To_Blocks class that accepts HTML and outputs block markup. It only considers the markup and won't consider any visual changes introduced via CSS or JavaScript. A part of #1894 ## Example ```html $html = <<<HTML <meta name="post_title" content="My first post"> <p>Hello <b>world</b>!</p> HTML; $converter = new WP_HTML_To_Blocks( $html ); $converter->convert(); var_dump( $converter->get_all_metadata() ); /* * array( 'post_title' => array( 'My first post' ) ) */ var_dump( $converter->get_block_markup() ); /* * <!-- wp:paragraph --> * <p>Hello <b>world</b>!</p> * <!-- /wp:paragraph --> */ ``` ## Testing instructions This PR mostly adds new code. Just confirm the unit tests pass in CI.
adamziel
commented
Dec 17, 2024
| @@ -0,0 +1,70 @@ | |||
| <?php | |||
|
|
|||
| abstract class WP_Entity_Reader implements \Iterator { | |||
Collaborator
Author
There was a problem hiding this comment.
We don't need an abstraction yet in this PR, but I'm about to propose another one with more entity readers.
adamziel
commented
Dec 17, 2024
adamziel
force-pushed
the
html-importer
branch
from
December 17, 2024 13:26
3a3c8c3 to
92fda0a
Compare
This was referenced Dec 17, 2024
Member
|
It would be good if this creates the same output as the JS (DOM-based) converter. If @dmsnell ends up creating a JS HTML API, we could potentially move the JS one to it. Could the HTML API somehow be language agnostic? |
Collaborator
Author
|
@ellatrix What do you mean by the DOM-based converter? Would this involve changes in this code? Or would this involve designing the DOM-based version to match functionality with the PHP-based version? |
brandonpayton
pushed a commit
that referenced
this pull request
Dec 31, 2024
Adds a basic `WP_HTML_To_Blocks` class that accepts HTML and outputs block markup. It's a very basic converter. It only considers the markup and won't consider any visual changes introduced via CSS or JavaScript. Only a few core blocks are supported in this initial PR. The API can easily support more HTML elements and blocks. To preserve visual fidelity between the original HTML page and the produced block markup, we'll need an annotated HTML input produced by the [Try WordPress](https://github.com/WordPress/try-wordpress/) browser extension. It would contain each element's colors, sizes, etc. We cannot possibly get all from just analyzing the HTML on the server without building a full-blown, browser-like HTML renderer in PHP, and I know I'm not building one. A part of #1894 ## Example ```php $html = <<<HTML <meta name="post_title" content="My first post"> <p>Hello <b>world</b>!</p> HTML; $converter = new WP_HTML_To_Blocks( $html ); $converter->convert(); var_dump( $converter->get_all_metadata() ); /* * array( 'post_title' => array( 'My first post' ) ) */ var_dump( $converter->get_block_markup() ); /* * <!-- wp:paragraph --> * <p>Hello <b>world</b>!</p> * <!-- /wp:paragraph --> */ ``` ## Caveats I had to patch WP_HTML_Processor to stop baling out on `<meta>` tags referencing the document charset. Ideally we'd patch WordPress core to stop baling out when the charset is UTF-8. ## Testing instructions This PR mostly adds new code. Just confirm the unit tests pass in CI. cc @brandonpayton @zaerl @sirreal @dmsnell @ellatrix
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a basic
WP_HTML_To_Blocksclass that accepts HTML and outputs block markup.It's a very basic converter. It only considers the markup and won't consider any visual changes introduced via CSS or JavaScript. Only a few core blocks are supported in this initial PR. The API can easily support more HTML elements and blocks.
To preserve visual fidelity between the original HTML page and the produced block markup, we'll need an annotated HTML input produced by the Try WordPress browser extension. It would contain each element's colors, sizes, etc. We cannot possibly get all from just analyzing the HTML on the server without building a full-blown, browser-like HTML renderer in PHP, and I know I'm not building one.
A part of #1894
Example
Caveats
I had to patch WP_HTML_Processor to stop baling out on
<meta>tags referencing the document charset. Ideally we'd patch WordPress core to stop baling out when the charset is UTF-8.Testing instructions
This PR mostly adds new code. Just confirm the unit tests pass in CI.
cc @brandonpayton @zaerl @sirreal @dmsnell @ellatrix