PHP code example of ubertech-za / html-to-asciidoc

1. Go to this page and download the library: Download ubertech-za/html-to-asciidoc 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/ */

    

ubertech-za / html-to-asciidoc example snippets


use UbertechZa\HtmlToAsciidoc\HtmlConverter;

$converter = new HtmlConverter();

$html = '
<h1>Welcome to AsciiDoc</h1>
<p>This is a <strong>bold</strong> statement with a <a href="https://asciidoc.org">link</a>.</p>
<ul>
    <li>First item</li>
    <li>Second item</li>
</ul>
';

$asciidoc = $converter->convert($html);
echo $asciidoc;

use UbertechZa\HtmlToAsciidoc\HtmlConverter;

$converter = new HtmlConverter();
$asciidoc = $converter->convert('<h1>Hello World</h1>');
// Result: = Hello World

use UbertechZa\HtmlToAsciidoc\HtmlToAsciiDocConverter;

$converter = new HtmlToAsciiDocConverter();
$asciidoc = $converter->convert('<p>Hello <em>world</em>!</p>');
// Result: Hello _world_!

use UbertechZa\HtmlToAsciidoc\Facades\HtmlToAsciidoc;

$asciidoc = HtmlToAsciidoc::convert('<h1>Easy Conversion</h1>');
// Result: = Easy Conversion

// Chain methods for configuration
$asciidoc = HtmlToAsciidoc::setOptions(['hard_break' => true])
                          ->convert('<p>Line 1<br>Line 2</p>');

use UbertechZa\HtmlToAsciidoc\HtmlConverter;

$converter = new HtmlConverter([
    'header_style' => 'atx',        // Use = for headers (default)
    'hard_break' => false,          // Use + for line breaks (default)
    'list_item_style' => '*',       // Use * for unordered lists (default) 
    'remove_nodes' => 'script style', // Remove these tags completely
    'strip_tags' => false,          // Don't strip unknown tags (default)
    'suppress_errors' => true,      // Suppress HTML parsing errors (default)
]);

$asciidoc = $converter->convert($html);

$asciidoc = (new HtmlConverter())
    ->setOptions(['hard_break' => true])
    ->convert('<p>Line 1<br>Line 2</p>');

use UbertechZa\HtmlToAsciidoc\Environment;
use UbertechZa\HtmlToAsciidoc\HtmlConverter;

$environment = Environment::createDefaultEnvironment([
    'custom_option' => 'value'
]);

$converter = new HtmlConverter($environment);

use UbertechZa\HtmlToAsciidoc\Converter\ConverterInterface;
use UbertechZa\HtmlToAsciidoc\ElementInterface;

class CustomConverter implements ConverterInterface
{
    public function convert(ElementInterface $element): string
    {
        return 'custom output for ' . $element->getValue();
    }

    public function getSupportedTags(): array
    {
        return ['custom-tag'];
    }
}

$environment = Environment::createDefaultEnvironment();
$environment->addConverter(new CustomConverter());

$converter = new HtmlConverter($environment);

use UbertechZa\HtmlToAsciidoc\ConfigurationAwareInterface;
use UbertechZa\HtmlToAsciidoc\Configuration;

class ConfigurableConverter implements ConverterInterface, ConfigurationAwareInterface
{
    private $config;

    public function setConfig(Configuration $config): void
    {
        $this->config = $config;
    }

    public function convert(ElementInterface $element): string
    {
        $option = $this->config->getOption('my_option', 'default');
        // Use option in conversion logic
        return $element->getValue();
    }

    public function getSupportedTags(): array
    {
        return ['configurable-tag'];
    }
}

'aliases' => [
    // Other aliases...
    'HtmlToAsciidoc' => UbertechZa\HtmlToAsciidoc\Facades\HtmlToAsciidoc::class,
],

$asciidoc = HtmlToAsciidoc::convert('<h1>Global Usage</h1>');

use UbertechZa\HtmlToAsciidoc\HtmlToAsciiDocConverter;

class DocumentController extends Controller
{
    public function convert(HtmlToAsciiDocConverter $converter)
    {
        $html = request('html');
        $asciidoc = $converter->convert($html);
        
        return response()->json(['asciidoc' => $asciidoc]);
    }
}

use UbertechZa\HtmlToAsciidoc\Facades\HtmlToAsciidoc;

class DocumentController extends Controller
{
    public function convert()
    {
        $html = request('html');
        
        $asciidoc = HtmlToAsciidoc::setOptions([
                'hard_break' => true,
                'list_item_style' => '*'
            ])
            ->convert($html);
        
        return response()->json(['asciidoc' => $asciidoc]);
    }
}

try {
    $converter = new HtmlConverter();
    $result = $converter->convert('<p>Malformed HTML<div>nested improperly</p></div>');
    // Will still produce reasonable output
} catch (InvalidArgumentException $e) {
    // Only thrown for completely invalid HTML structure
    echo "Invalid HTML provided";
}
asciidoc
*Bold text*
*Also bold*
_Italic text_
_Also italic_
`Inline code`
html
<pre>
function hello() {
    return "Hello World";
}
</pre>
asciidoc
----
function hello() {
    return "Hello World";
}
----
bash
php artisan vendor:publish --provider="UbertechZa\HtmlToAsciidoc\HtmlToAsciiDocServiceProvider"
blade
@php
    $html = '<h1>Dynamic Content</h1><p>From your CMS</p>';
    $asciidoc = HtmlToAsciidoc::convert($html);
@endphp

<pre>{{ $asciidoc }}</pre>
bash
composer analyse