Download the PHP package unzeroun/isocontent without Composer
On this page you can find all versions of the php package unzeroun/isocontent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download unzeroun/isocontent
More information about unzeroun/isocontent
Files in unzeroun/isocontent
Package isocontent
Short Description A library to transform web contents (say HTML) to an abstract AST that can be rendered everywhere
License MIT
Informations about the package isocontent
Isocontent
Isocontent is a PHP library that transforms rich‑text content (such as HTML) into a platform‑agnostic Abstract Syntax Tree (AST). The AST can then be rendered back to any target format — HTML, JSON, React Native, mobile views, or anything else you need.
Typical use‑case: accept content from a WYSIWYG editor, store it as a portable AST, and render it on any platform.
Installation
Requires PHP >= 8.2. The DOMParser needs ext-dom and ext-libxml; the JSONRenderer needs ext-json.
End‑to‑End Demo
The Isocontent service is the main entry point. Register parsers and renderers, then parse and render content in any supported format:
Core Concepts
Isocontent models content as a tree of nodes:
| Class | Description |
|---|---|
TextNode |
Leaf node holding a plain text value. |
BlockNode |
Structural element (paragraph, heading, link…) with optional children and typed arguments. |
NodeList |
Ordered collection of nodes — this is what parsers produce and renderers consume. |
Block Types
The DOMParser maps HTML elements to the following block types:
| Block Type | HTML Tag(s) | Arguments |
|---|---|---|
paragraph |
<p> |
— |
title |
<h1>–<h6> |
level (int 1–6) |
strong |
<strong> |
— |
emphasis |
<em> |
— |
inline_text |
<span> |
— |
link |
<a> |
href (string) |
list |
<ul>, <ol> |
ordered (bool) |
list_item |
<li> |
— |
quote |
<blockquote> |
— |
new_line |
<br> |
— |
stripped |
<del> |
— |
separator |
<hr> |
— |
subscript |
<sub> |
— |
superscript |
<sup> |
— |
code |
<code> |
— |
generic |
any other element | — |
Parsers
A parser reads input in a given format and builds an AST through the Builder.
DOMParser— parses HTML strings via PHP'sDOMDocument(format:html)ArrayParser— re‑hydrates an AST from a PHP array or decoded JSON (format:array)
Custom Parser
Implement the Parser interface:
Renderers
A renderer converts a NodeList into an output format.
HTMLRenderer— renders to HTML (format:html). Tag mapping is customizable.JSONRenderer— renders to a JSON string (format:json).TextDebugRenderer— renders an indented tree for debugging (format:text_debug).
Custom Renderer
Implement the Renderer interface:
Custom HTML Tag Mapping
The HTMLRenderer uses the Specification pattern to map block types to HTML tags. You can pass your own mapping to override the defaults:
Available specifications: BlockTypeMatch (match by type), BlockArgumentMatch (match by argument key/value), and AllMatch (compose with ->and()).
Symfony Integration
Register the bundle:
The bundle auto‑discovers all Parser and Renderer implementations (tagged isocontent.parser / isocontent.renderer) and registers a public Isocontent service. All built‑in parsers and renderers are autowired out of the box.
Two Symfony Form data transformers are also provided: ASTToStringTransformer (Node|NodeList ↔ rendered string) and ASTToArrayTransformer (Node|NodeList ↔ PHP array).
Twig Integration
A Twig filter is available to render AST directly in templates:
The filter accepts both NodeList objects and raw arrays.
Testing
CI runs on PHP 8.2, 8.3, 8.4, and 8.5 with both lowest and highest dependency versions.
Contributing
Contributions are welcome! Fork the repository, create a feature branch, ensure all checks pass, and open a pull request.
License
Isocontent is released under the MIT License.