-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs: Auto-generate human-readable version of Gutenberg block grammar (…
…#6116) * Grammar: Add proof-of-concept rule display name for Block_Attributes * Add PEG.js grammar to parse Gutenberg block grammar * Docs: Grammar: Add autogenerated doc on Gutenberg block grammar * Docs: Grammar: Add bin/generate-public-grammar.js, reusing pegjs internals * Docs: Grammar: Render as HTML * Docs: Grammar: Remove shell-based docgen helper
- Loading branch information
Showing
4 changed files
with
155 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#!/usr/bin/env node | ||
const parser = require( '../node_modules/pegjs/lib/parser.js' ); | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const grammarSource = fs.readFileSync( './blocks/api/post.pegjs', 'utf8' ); | ||
const grammar = parser.parse( grammarSource ); | ||
|
||
function escape( text ) { | ||
return text | ||
.replace( /\t/g, '\\t' ) | ||
.replace( /\r/g, '\\r' ) | ||
.replace( /\n/g, '\\n' ) | ||
.replace( /\&/g, '&' ) | ||
.replace( /</g, '<' ); | ||
} | ||
|
||
function isGroup( expression ) { | ||
return [ | ||
'choice', | ||
'action', | ||
'labeled', | ||
'sequence', | ||
].indexOf( expression.type ) >= 0; | ||
} | ||
|
||
function flattenUnary( expression ) { | ||
const shouldWrap = isGroup( expression ); | ||
const inner = flatten( expression ); | ||
return shouldWrap ? '(' + inner + ')' : inner; | ||
} | ||
|
||
function flatten( expression ) { | ||
switch ( expression.type ) { | ||
// Terminal | ||
case 'any': | ||
return '.'; | ||
case 'rule_ref': | ||
return expression.name; | ||
case 'literal': | ||
return '"' + escape( expression.value ) + '"'; | ||
case 'class': | ||
return ( | ||
'[' + ( expression.inverted ? '^' : '' ) + | ||
expression.parts.map( ( part ) => | ||
escape( Array.isArray( part ) ? part.join( '-' ) : part ) | ||
).join( '' ) + | ||
']' + ( expression.ignoreCase ? 'i' : '' ) | ||
); | ||
|
||
// Unary | ||
case 'zero_or_more': | ||
return flattenUnary( expression.expression ) + '*'; | ||
case 'one_or_more': | ||
return flattenUnary( expression.expression ) + '+'; | ||
case 'optional': | ||
return flattenUnary( expression.expression ) + '?'; | ||
case 'simple_not': | ||
return '!' + flattenUnary( expression.expression ); | ||
|
||
// Other groups | ||
case 'sequence': | ||
return expression.elements.map( flatten ).join( ' ' ); | ||
case 'choice': | ||
const sep = expression.isRuleTop ? '\n / ' : ' / '; | ||
return expression.alternatives.map( flatten ).join( sep ); | ||
case 'group': | ||
return '(' + flatten( expression.expression ) + ')'; | ||
case 'text': | ||
// Avoid double parentheses | ||
const inner = flatten( expression.expression ); | ||
const shouldWrap = inner.indexOf( '(' ) !== 0; | ||
return shouldWrap ? '$(' + inner + ')' : '$' + inner; | ||
case 'action': | ||
case 'labeled': | ||
case 'named': | ||
return flatten( expression.expression ); | ||
|
||
// Top-level formatting | ||
case 'grammar': | ||
return `<dl>${ expression.rules.map( flatten ).join( '' ) }</dl>`; | ||
case 'rule': | ||
expression.expression.isRuleTop = true; | ||
const displayName = expression.expression.type === 'named' ? | ||
expression.expression.name : ''; | ||
return `<dt>${ displayName }</dt>` + | ||
`<dd><pre><header>${ expression.name }</header> = ` + | ||
`${ flatten( expression.expression ) }</pre></dd>`; | ||
|
||
default: | ||
throw new Error( JSON.stringify( expression ) ); | ||
} | ||
} | ||
|
||
fs.writeFileSync( | ||
path.join( __dirname, '..', 'docs', 'grammar.md' ), ` | ||
# The Gutenberg block grammar | ||
<style> | ||
dl { display: flex; flex-wrap: wrap; font-size: 110%; } | ||
dt, dd { flex: 40%; margin-bottom: 1em; } | ||
dt { text-align: right; font-style: italic; font-size: 105%; } | ||
dd header { font-weight: bold; } | ||
pre { margin: 0; } | ||
</style> | ||
${ flatten( grammar ) } | ||
` ); |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
# The Gutenberg block grammar | ||
|
||
<style> | ||
dl { display: flex; flex-wrap: wrap; font-size: 110%; } | ||
dt, dd { flex: 40%; margin-bottom: 1em; } | ||
dt { text-align: right; font-style: italic; font-size: 105%; } | ||
dd header { font-weight: bold; } | ||
pre { margin: 0; } | ||
</style> | ||
<dl><dt></dt><dd><pre><header>Block_List</header> = $(!Block .)* (Block $(!Block .)*)* $(.*)</pre></dd><dt></dt><dd><pre><header>Block</header> = Block_Void | ||
/ Block_Balanced</pre></dd><dt></dt><dd><pre><header>Block_Void</header> = "<!--" __ "wp:" Block_Name __ (Block_Attributes __)? "/-->"</pre></dd><dt></dt><dd><pre><header>Block_Balanced</header> = Block_Start (Block / $(!Block_End .))* Block_End</pre></dd><dt></dt><dd><pre><header>Block_Start</header> = "<!--" __ "wp:" Block_Name __ (Block_Attributes __)? "-->"</pre></dd><dt></dt><dd><pre><header>Block_End</header> = "<!--" __ "/wp:" Block_Name __ "-->"</pre></dd><dt></dt><dd><pre><header>Block_Name</header> = Namespaced_Block_Name | ||
/ Core_Block_Name</pre></dd><dt></dt><dd><pre><header>Namespaced_Block_Name</header> = $(Block_Name_Part "/" Block_Name_Part)</pre></dd><dt></dt><dd><pre><header>Core_Block_Name</header> = $(Block_Name_Part)</pre></dd><dt></dt><dd><pre><header>Block_Name_Part</header> = $([a-z] [a-z0-9_-]*)</pre></dd><dt>JSON-encoded attributes embedded in a block's opening comment</dt><dd><pre><header>Block_Attributes</header> = $("{" (!("}" __ "" "/"? "-->") .)* "}")</pre></dd><dt></dt><dd><pre><header>__</header> = [ \t\r\n]+</pre></dd></dl> |
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