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

Packages: Create new spec-parser package #7664

Merged
merged 3 commits into from
Jul 19, 2018
Merged
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 .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ build
build-module
coverage
node_modules
packages/block-serialization-spec-parser
Copy link
Member

@aduth aduth Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we ignore the entire package? Could we just ignore the singular file(s) which are generated? The test file, for example, includes a number of legitimate code style violations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this should be only one file. Let me fix it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I know what happened. I used toMatchInlineSnapshot which uses prettier behind the scenes. We can't really use it with the current setup unless we adopt calypso-prettier :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be addressed in #9166.

test/e2e/test-plugins
vendor
7 changes: 5 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ module.exports = {
message: 'Use @wordpress/blob as import path instead.',
},
{
selector: 'ImportDeclaration[source.value=/^blocks(\\u002F|$)/]',
message: 'Use @wordpress/blocks as import path instead.',
selector: 'ImportDeclaration[source.value=/^block-serialization-spec-parser(\\u002F|$)/]',
message: 'Use @wordpress/block-serialization-spec-parser as import path instead.',
},
{
selector: 'ImportDeclaration[source.value=/^blocks(\\u002F|$)/]',
message: 'Use @wordpress/blocks as import path instead.',
},{
selector: 'ImportDeclaration[source.value=/^components(\\u002F|$)/]',
message: 'Use @wordpress/components as import path instead.',
},
Expand Down
2 changes: 1 addition & 1 deletion bin/create-php-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const phpegjs = require( 'phpegjs' );
const fs = require( 'fs' );
const path = require( 'path' );

const peg = fs.readFileSync( 'blocks/api/post.pegjs', 'utf8' );
const peg = fs.readFileSync( 'packages/block-serialization-spec-parser/grammar.pegjs', 'utf8' );

const parser = pegjs.generate(
peg,
Expand Down
2 changes: 1 addition & 1 deletion bin/generate-public-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 grammarSource = fs.readFileSync( './packages/block-serialization-spec-parser/grammar.pegjs', 'utf8' );
const grammar = parser.parse( grammarSource );

function escape( text ) {
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { flow, castArray, mapValues, omit, stubFalse } from 'lodash';
import { autop } from '@wordpress/autop';
import { applyFilters } from '@wordpress/hooks';
import deprecated from '@wordpress/deprecated';
import { parse as grammarParse } from '@wordpress/block-serialization-spec-parser';

/**
* Internal dependencies
*/
import { parse as grammarParse } from './post-parser';
import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { createBlock } from './factory';
import { isValidBlock } from './validation';
Expand Down Expand Up @@ -365,7 +365,7 @@ export function createBlockWithFallback( blockNode ) {
*
* @return {Function} An implementation which parses the post content.
*/
export const createParse = ( parseImplementation ) =>
const createParse = ( parseImplementation ) =>
( content ) => parseImplementation( content ).reduce( ( memo, blockNode ) => {
const block = createBlockWithFallback( blockNode );
if ( block ) {
Expand Down
1 change: 0 additions & 1 deletion blocks/api/post-parser.js

This file was deleted.

1 change: 0 additions & 1 deletion blocks/api/post-parser.native.js

This file was deleted.

10 changes: 1 addition & 9 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import {
default as parsePegjs,
parseWithAttributeSchema,
toBooleanAttributeMatcher,
createParse,
} from '../parser';
import { parse as grammarParsePreGenerated } from '../post-grammar-parser-pre-generated';
import {
registerBlockType,
unregisterBlockType,
Expand Down Expand Up @@ -549,17 +547,11 @@ describe( 'block parser', () => {
} );
} );

describe( 'parse() of pegjs parser', () => {
describe( 'parse() of @wordpress/block-serialization-spec-parser', () => {
// run the test cases using the PegJS defined parser
testCases( parsePegjs );
} );

describe( 'parse() of pre-generated parser', () => {
// run the test cases using the pre-generated parser
const parsePreGenerated = createParse( grammarParsePreGenerated );
testCases( parsePreGenerated );
} );

// encapsulate the test cases so we can run them multiple time but with a different parse() function
function testCases( parse ) {
it( 'should parse the post content, including block attributes', () => {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/full-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { format } from 'util';
* WordPress dependencies
*/
import { getBlockTypes, parse, serialize } from '@wordpress/blocks';
import { parse as grammarParse } from '@wordpress/block-serialization-spec-parser';

/**
* Internal dependencies
*/
import { registerCoreBlocks } from '../';
import { parse as grammarParse } from '../../blocks/api/post.pegjs';

const fixturesDir = path.join( __dirname, 'fixtures' );

Expand Down
9 changes: 8 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ function gutenberg_register_scripts_and_styles() {
filemtime( gutenberg_dir_path() . 'build/dom/index.js' ),
true
);
wp_register_script(
'wp-block-serialization-spec-parser',
gutenberg_url( 'build/block-serialization-spec-parser/index.js' ),
array(),
filemtime( gutenberg_dir_path() . 'build/block-serialization-spec-parser/index.js' ),
true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this true in all registered scripts? I feel it's only necessary for edit-post?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it fixes some issues, I remember seeing PR from @pento which added them everywhere.

);
wp_add_inline_script(
'wp-dom',
gutenberg_get_script_polyfill( array(
Expand Down Expand Up @@ -290,7 +297,7 @@ function gutenberg_register_scripts_and_styles() {
wp_register_script(
'wp-blocks',
gutenberg_url( 'build/blocks/index.js' ),
array( 'wp-blob', 'wp-deprecated', 'wp-dom', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-shortcode', 'wp-data', 'lodash' ),
array( 'wp-blob', 'wp-deprecated', 'wp-dom', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-shortcode', 'wp-block-serialization-spec-parser', 'wp-data', 'lodash' ),
filemtime( gutenberg_dir_path() . 'build/blocks/index.js' ),
true
);
Expand Down
26 changes: 3 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@wordpress/api-fetch": "file:packages/api-fetch",
"@wordpress/autop": "file:packages/autop",
"@wordpress/blob": "file:packages/blob",
"@wordpress/block-serialization-spec-parser": "file:packages/block-serialization-spec-parser",
"@wordpress/components": "file:packages/components",
"@wordpress/compose": "file:packages/compose",
"@wordpress/core-data": "file:packages/core-data",
Expand Down Expand Up @@ -105,7 +106,6 @@
"node-sass": "4.9.2",
"path-type": "3.0.0",
"pegjs": "0.10.0",
"pegjs-loader": "0.5.4",
"phpegjs": "1.0.0-beta7",
"postcss-color-function": "4.0.1",
"postcss-loader": "2.1.3",
Expand Down Expand Up @@ -145,7 +145,7 @@
"scripts": {
"prebuild": "npm run check-engines",
"clean:packages": "rimraf ./packages/*/build ./packages/*/build-module ./packages/*/build-style",
"prebuild:packages": "npm run clean:packages && cross-env INCLUDE_PACKAGES=babel-plugin-import-jsx-pragma,postcss-themes SKIP_JSX_PRAGMA_TRANSFORM=1 node ./bin/packages/build.js",
"prebuild:packages": "npm run clean:packages && lerna run build && cross-env INCLUDE_PACKAGES=babel-plugin-import-jsx-pragma,postcss-themes SKIP_JSX_PRAGMA_TRANSFORM=1 node ./bin/packages/build.js",
"build:packages": "cross-env EXCLUDE_PACKAGES=babel-plugin-import-jsx-pragma,postcss-themes node ./bin/packages/build.js",
"build": "npm run build:packages && cross-env NODE_ENV=production webpack",
"check-engines": "check-node-version --package",
Expand Down
1 change: 1 addition & 0 deletions packages/block-serialization-spec-parser/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
26 changes: 26 additions & 0 deletions packages/block-serialization-spec-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @wordpress/block-serialization-spec-parser

This library contains the grammar file (`grammar.pegjs`) for WordPress posts which is a block serialization _specification_ which is used to generate the actual _parser_ which is also bundled in this package.

PEG parser generators are available in many languages, though different libraries may require some translation of this grammar into their syntax. For more information see:
* https://pegjs.org
* https://en.wikipedia.org/wiki/Parsing_expression_grammar

## Installation

Install the module

```bash
npm install @wordpress/block-serialization-spec-parser --save
```

## Usage

```js
import { parse } from '@wordpress/block-serialization-spec-parser';

parse( '<!-- wp:core/more --><!--more--><!-- /wp:core/more -->' );
// [{"attrs": null, "blockName": "core/more", "innerBlocks": [], "innerHTML": "<!--more-->"}]
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading