-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Liberation] Block markup consumers and producers (#2121)
A part of #1894 Introduces a standardized API for converting between static data formats and blocks+metadata. * The `data format -> blocks+metadata` operation is represented by the WP_Data_Format_Consumer interface * The `blocks+metadata -> data format` operation is represented by the WP_Data_Format_Producer interface This PR also ships a few initial consumers and producers: * `WP_Annotated_Block_Markup_Consumer` – for consuming static block markup with `<meta>` tags. * `WP_Markup_Processor_Consumer` – for consuming an HTML/XHTML markup processor instance. It handles just the regular HTML/XHTML markup, not block markup. * `WP_Annotated_Block_Markup_Producer` – for serializing block markup + metadata array as block markup with `<meta>` tags ## Example The two-way conversion pipeline shipped in this PR goes between this: ```php $block_markup = <<<BLOCKS <!-- wp:paragraph --> <p>Hello <b>world</b>!</p> <!-- /wp:paragraph --> BLOCKS; $metadata = array( 'post_title' => array( 'My first post' ), ); ``` And this: ```html <meta name="post_title" content="My first post"> <!-- wp:paragraph --> <p>Hello <b>world</b>!</p> <!-- /wp:paragraph --> ``` ## Other changes This PR also ships the block parser from WordPress core to enable running unit tests – we need to call `parse_blocks()` now. ## Testing The code isn't used anywhere yet – just rely on the CI.
- Loading branch information
Showing
23 changed files
with
3,965 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/WP_Markdown_Importer.php'; | ||
require_once __DIR__ . '/WP_Markdown_To_Blocks.php'; | ||
require_once __DIR__ . '/WP_Markdown_Consumer.php'; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; |
This file contains 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 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
8 changes: 8 additions & 0 deletions
8
packages/playground/data-liberation/src/Data_Liberation_Exception.php
This file contains 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,8 @@ | ||
<?php | ||
|
||
/** | ||
* Represents an error that occurs during the data liberation process. | ||
*/ | ||
class Data_Liberation_Exception extends RuntimeException { | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
packages/playground/data-liberation/src/WP_Data_Liberation_HTML_Processor.php
This file contains 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,53 @@ | ||
<?php | ||
|
||
class WP_Data_Liberation_HTML_Processor extends WP_HTML_Processor { | ||
|
||
public function get_inner_html() { | ||
if ( '#tag' !== $this->get_token_type() ) { | ||
return false; | ||
} | ||
|
||
if ( $this->is_tag_closer() ) { | ||
return false; | ||
} | ||
|
||
if ( false === WP_HTML_Tag_Processor::set_bookmark( 'tag-start' ) ) { | ||
return false; | ||
} | ||
|
||
$this->skip_to_closer(); | ||
|
||
if ( false === WP_HTML_Tag_Processor::set_bookmark( 'tag-end' ) ) { | ||
WP_HTML_Tag_Processor::release_bookmark( 'tag-start' ); | ||
return false; | ||
} | ||
|
||
$inner_html_start = $this->bookmarks['tag-start']->start + $this->bookmarks['tag-start']->length; | ||
$inner_html_end = $this->bookmarks['tag-end']->start - $inner_html_start; | ||
|
||
WP_HTML_Tag_Processor::seek( 'tag-start' ); | ||
WP_HTML_Tag_Processor::release_bookmark( 'tag-start' ); | ||
WP_HTML_Tag_Processor::release_bookmark( 'tag-end' ); | ||
|
||
return substr( | ||
$this->html, | ||
$inner_html_start, | ||
$inner_html_end | ||
); | ||
} | ||
|
||
public function skip_to_closer() { | ||
$starting_depth = $this->get_current_depth(); | ||
while ( $this->next_token() ) { | ||
if ( | ||
$this->get_token_type() === '#tag' && | ||
$this->is_tag_closer() && | ||
$this->get_current_depth() === $starting_depth - 1 | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ayground/data-liberation/src/data-format-consumers/WP_Annotated_Block_Markup_Consumer.php
This file contains 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,62 @@ | ||
<?php | ||
/** | ||
* Converts a metadata-annotated block markup into block markup+metadata pair. | ||
* | ||
* Example: | ||
* | ||
* <meta name="post_title" content="My first post"> | ||
* <!-- wp:paragraph {"className":"my-class"} --> | ||
* <p class="my-class">Hello world!</p> | ||
* <!-- /wp:paragraph --> | ||
* | ||
* Becomes: | ||
* | ||
* <!-- wp:paragraph --> | ||
* <p>Hello <b>world</b>!</p> | ||
* <!-- /wp:paragraph --> | ||
* | ||
* With the following metadata: | ||
* | ||
* array( | ||
* 'post_title' => array( 'My first post' ), | ||
* ) | ||
*/ | ||
class WP_Annotated_Block_Markup_Consumer implements WP_Data_Format_Consumer { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $original_html; | ||
|
||
/** | ||
* @var WP_Consumed_Block_Markup | ||
*/ | ||
private $result; | ||
|
||
public function __construct( $original_html ) { | ||
$this->original_html = $original_html; | ||
} | ||
|
||
public function consume() { | ||
if ( ! $this->result ) { | ||
$block_markup = ''; | ||
$metadata = array(); | ||
foreach ( parse_blocks( $this->original_html ) as $block ) { | ||
if ( $block['blockName'] === null ) { | ||
$html_converter = new WP_Markup_Processor_Consumer( WP_HTML_Processor::create_fragment( $block['innerHTML'] ) ); | ||
$result = $html_converter->consume(); | ||
$block_markup .= $result->get_block_markup() . "\n"; | ||
$metadata = array_merge( $metadata, $result->get_all_metadata() ); | ||
} else { | ||
$block_markup .= serialize_block( $block ) . "\n"; | ||
} | ||
} | ||
$this->result = new WP_Blocks_With_Metadata( | ||
$block_markup, | ||
$metadata | ||
); | ||
} | ||
|
||
return $this->result; | ||
} | ||
} |
This file contains 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
16 changes: 16 additions & 0 deletions
16
packages/playground/data-liberation/src/data-format-consumers/WP_Data_Format_Consumer.php
This file contains 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,16 @@ | ||
<?php | ||
|
||
/** | ||
* Represents a {Data Format} -> Block Markup + Metadata consumer. | ||
* | ||
* Used by the Data Liberation importers to accept data formatted as HTML, Markdown, etc. | ||
* and convert them to WordPress posts. | ||
*/ | ||
interface WP_Data_Format_Consumer { | ||
/** | ||
* Converts the input document specified in the constructor to block markup. | ||
* | ||
* @return WP_Blocks_With_Metadata The consumed block markup and metadata. | ||
*/ | ||
public function consume(); | ||
} |
Oops, something went wrong.