decodelabs/idiom

Lightwight text to HTML parser

v0.2.2 2023-09-26 10:48 UTC

This package is auto-updated.

Last update: 2024-04-26 11:18:16 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Lightweight text to HTML parser - tags optional

Idiom provides a simple and easy to use text parser that can convert natural language with an optional subset of HTML tags to full markup ready for consumption.

Get news and updates on the DecodeLabs blog.

Installation

Install the library via composer:

composer require decodelabs/idiom

Usage

Parse a block of text with optional HTML tags into rendered HTML:

use DecodeLabs\Idiom\Parser;

$content = <<<CONTENT
Idiom allows you to write naturally, converting paragraphs, lines and spaces to the relevant HTML elements.

You can include <strong>tags</strong> where necessary.

<ul>
    <li>Block elements like this can be used if extended mode is enabled</li>
    <li>Alternatively, content can be rendered as inline HTML with inline mode</li>
</ul>
CONTENT;

$parser = new Parser();
$parser->setExtended(true);
echo $parser->parse($content);

Metamorph

Idiom also provides a Metamorph Handler so that it can be used via its simplified interface:

use DecodeLabs\Metamorph;

echo Metamorph::{'idiom.extended'}($content);

Output

The parsed HTML provided by Idiom is wrapped in a Markup interface from the Tagged library such that output is handled correctly in all rendering contexts.

Safety

Please note, Idiom does not yet exhaustively treat input for malicious entities - this parser should only be used with content considered safe.

Licensing

Idiom is licensed under the MIT License. See LICENSE for the full license text.