Skip to content

Commit

Permalink
Add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Neol3108 committed Apr 11, 2024
1 parent 4b66675 commit 62830f2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 12 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# WordPress Block Parser

Parse WordPress edit-context content to PHP objects easily.

## Installation
```bash
composer require recoded-dev/wordpress-block-parser
```

## Examples

### Parsing and replacing
```php
<?php

use Recoded\WordPressBlockParser\BlockParser;
use Recoded\WordPressBlockParser\Blocks\Block;
use Recoded\WordPressBlockParser\BlockReplacer;

$content = <<<HTML
<!-- wp:paragraph -->
Test
<!-- /wp:paragraph -->
HTML;

$parser = BlockParser::create($content);
$replacer = BlockReplacer::create($content);

foreach ($parser as $block) {
// $block->namespace
// $block->name
// $block->attributes

if ($block instanceof Block) {
// $block->content
}

$replacer->replace($block, 'Your replaced content');
}

echo (string) $replacer; // Your replaced content
```

## Contributing
Everyone is welcome to contribute. Feel free to PR your work once you think it's ready.
Or open a draft-PR if you want to get some opinions or further help.

I would like to keep this package relatively small and want to avoid bloat. The package
should remain extensible and unopinionated.
16 changes: 16 additions & 0 deletions src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

abstract class AbstractBlock
{
/**
* Create new AbstractBlock instance.
*
* @param string $namespace
* @param string $name
* @param array<string, mixed> $attributes
* @return void
*/
public function __construct(
public readonly string $namespace,
public readonly string $name,
public readonly array $attributes,
) {
//
}

/**
* Get the offset on which the block starts.
*
Expand Down
25 changes: 18 additions & 7 deletions src/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
use Recoded\WordPressBlockParser\Tokens\BlockClosing;
use Recoded\WordPressBlockParser\Tokens\BlockOpening;

/**
* @TODO find better name, block is too generic
*/
final class Block extends AbstractBlock
{
public readonly string $content;
public readonly BlockOpening $opening;
public readonly BlockClosing $closing;

/**
* Create new Block instance.
*
Expand All @@ -19,14 +26,18 @@ final class Block extends AbstractBlock
* @return void
*/
public function __construct(
public readonly string $namespace,
public readonly string $name,
public readonly array $attributes,
public readonly string $content,
public readonly BlockOpening $opening,
public readonly BlockClosing $closing,
string $namespace,
string $name,
array $attributes,
string $content,
BlockOpening $opening,
BlockClosing $closing,
) {
//
parent::__construct($namespace, $name, $attributes);

$this->content = $content;
$this->opening = $opening;
$this->closing = $closing;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/Blocks/SelfClosingBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

final class SelfClosingBlock extends AbstractBlock
{
public readonly SelfClosingBlockToken $token;

/**
* Create new SelfClosingBlock instance.
*
Expand All @@ -16,12 +18,14 @@ final class SelfClosingBlock extends AbstractBlock
* @return void
*/
public function __construct(
public readonly string $namespace,
public readonly string $name,
public readonly array $attributes,
public readonly SelfClosingBlockToken $token,
string $namespace,
string $name,
array $attributes,
SelfClosingBlockToken $token,
) {
//
parent::__construct($namespace, $name, $attributes);

$this->token = $token;
}

/**
Expand Down

0 comments on commit 62830f2

Please sign in to comment.