Skip to content
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

WIP: Add dynamic block #6170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/create-php-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const parser = pegjs.generate(
{
plugins: [ phpegjs ],
phpegjs: {
allowedStartRules: [ 'Block_List', 'Rendered_Output' ],
parserNamespace: null,
parserGlobalNamePrefix: 'Gutenberg_PEG_',
mbstringAllowed: false,
Expand Down
34 changes: 34 additions & 0 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ if ( ! function_exists( 'peg_array_partition' ) ) {
}
}

if ( ! function_exists( 'peg_is_dynamic_block' ) ) {
function peg_is_dynamic_block( $block_name ) {
static $dynamic_blocks = null;

if ( null === $dynamic_blocks ) {
$dynamic_blocks = get_dynamic_block_names();
}

return in_array( $block_name, $dynamic_blocks );
}
}

if ( ! function_exists( 'peg_join_blocks' ) ) {
function peg_join_blocks( $pre, $tokens, $post ) {
$blocks = array();
Expand Down Expand Up @@ -168,6 +180,28 @@ Block_List
return joinBlocks( pre, ts, post );
}

Rendered_Output
= ts:Rendered_Token* { return ts.join( '' ) /** <?php return implode( '', $ts ); ?> **/ }

Rendered_Token
= Dynamic_Block
/ Block_Start { return '' /** <?php return ''; ?> **/ }
/ Block_End { return '' /** <?php return ''; ?> **/ }
/ Block_Void { return '' /** <?php return ''; ?> **/ }
/ .

Dynamic_Block
= s:Block_Void
& { return false /** <?php return peg_is_dynamic_block( $s['blockName'] ); ?> **/ }
{ return '' /** <?php return 'dynamic'; ?> **/ }
/ s:Dynamic_Block_Start c:(!Block_End .)+ e:Block_End
{ return '' /** <?php return 'dynamic'; ?> **/ }

Dynamic_Block_Start
= s:Block_Start
& { return false /** <?php return peg_is_dynamic_block( $s['blockName'] ); ?> **/ }
{ return s /** <?php return $s; ?> **/ }

Token
= Block_Void
/ Block_Balanced
Expand Down
Loading