PHP code example of unzeroun / isocontent

1. Go to this page and download the library: Download unzeroun/isocontent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unzeroun / isocontent example snippets


use Isocontent\Isocontent;
use Isocontent\Parser\DOMParser;
use Isocontent\Renderer\HTMLRenderer;
use Isocontent\Renderer\JSONRenderer;

$isocontent = new Isocontent(
    parsers:   [new DOMParser()],
    renderers: [new HTMLRenderer(), new JSONRenderer()],
);

// 1. Parse HTML into an AST
$ast = $isocontent->buildAST(
    '<h1>Hello</h1><p>This is <strong>rich</strong> content with a <a href="https://example.com">link</a>.</p>',
    'html',
);

// 2. Inspect the AST as a portable array (suitable for JSON storage / API responses)
$ast->toArray();
// [
//     [
//         'type'       => 'block',
//         'block_type' => 'title',
//         'arguments'  => ['level' => 1],
//         'children'   => [
//             ['type' => 'text', 'value' => 'Hello'],
//         ],
//     ],
//     [
//         'type'       => 'block',
//         'block_type' => 'paragraph',
//         'arguments'  => [],
//         'children'   => [
//             ['type' => 'text', 'value' => 'This is '],
//             [
//                 'type'       => 'block',
//                 'block_type' => 'strong',
//                 'arguments'  => [],
//                 'children'   => [
//                     ['type' => 'text', 'value' => 'rich'],
//                 ],
//             ],
//             ['type' => 'text', 'value' => ' content with a '],
//             [
//                 'type'       => 'block',
//                 'block_type' => 'link',
//                 'arguments'  => ['href' => 'https://example.com'],
//                 'children'   => [
//                     ['type' => 'text', 'value' => 'link'],
//                 ],
//             ],
//             ['type' => 'text', 'value' => '.'],
//         ],
//     ],
// ]

// 3. Render back to HTML
$isocontent->render($ast, 'html');
// '<h1>Hello</h1><p>This is <strong>rich</strong> content with a <a>link</a>.</p>'

// 4. Render to JSON
$isocontent->render($ast, 'json');
// The same array structure above, encoded as a JSON string

use Isocontent\AST\Builder;
use Isocontent\Parser\Parser;

final class MarkdownParser implements Parser
{
    public function supportsFormat(string $format): bool
    {
        return 'markdown' === $format;
    }

    public function parse(Builder $builder, mixed $input): void
    {
        // Build the AST using $builder->addTextNode() / $builder->addBlockNode()
    }
}

use Isocontent\AST\NodeList;
use Isocontent\Renderer\Renderer;

final class ReactNativeRenderer implements Renderer
{
    public function supportsFormat(string $format): bool
    {
        return 'react_native' === $format;
    }

    public function render(NodeList $ast): mixed
    {
        // Walk the AST and produce your output
    }
}

use Isocontent\Renderer\HTMLRenderer;
use Isocontent\Specs\BlockTypeMatch;
use Isocontent\Specs\BlockArgumentMatch;

$renderer = new HTMLRenderer([
    [(new BlockTypeMatch('title'))->and(new BlockArgumentMatch('level', 1)), 'h1'],
    [(new BlockTypeMatch('title'))->and(new BlockArgumentMatch('level', 2)), 'h2'],
    [new BlockTypeMatch('paragraph'), 'p'],
    [new BlockTypeMatch('strong'), 'b'],        // <b> instead of <strong>
    [new BlockTypeMatch('emphasis'), 'i'],       // <i> instead of <em>
    [new BlockTypeMatch('inline_text'), 'font'], // <font> instead of <span>
]);

// config/bundles.php
return [
    // ...
    Isocontent\Bridge\Symfony\Bundle\IsocontentBundle::class => ['all' => true],
];

use Isocontent\Isocontent;

final class ContentController
{
    public function __construct(private readonly Isocontent $isocontent) {}

    public function show(): Response
    {
        $ast  = $this->isocontent->buildAST($html, 'html');
        $json = $this->isocontent->render($ast, 'json');
        // ...
    }
}