#!/usr/bin/php
<?php 
function patchFile($filepath)
{
    $file = file_get_contents($filepath);
    // Execute PHP and replace output
    $text = preg_replace_callback('#(```md\\n(.*?)\\n```\\s+```html).*?(\\n```)#s', function ($m) {
        global $parser, $renderer;
        $xml = $parser->parse($m[2]);
        $html = $renderer->render($xml);
        return $m[1] . "\n" . $html . $m[3];
    }, $file);
    if ($text !== $file) {
        echo "Patching {$filepath}\n";
        file_put_contents($filepath, $text);
    }
}
include __DIR__ . '/../src/autoloader.php';
$configurator = new s9e\TextFormatter\Configurator();
$configurator->Litedown;
extract($configurator->finalize());
patchFile(__DIR__ . '/../src/Plugins/Litedown/Syntax.md');
patchFile(__DIR__ . '/../docs/Plugins/Litedown/Syntax.md');
die("Done.\n");
Example #2
0
{
    $dirpath = realpath($dirpath);
    array_map('patchDir', glob($dirpath . '/*', GLOB_ONLYDIR));
    array_map('patchFile', glob($dirpath . '/*.md'));
}
function patchFile($filepath)
{
    $file = file_get_contents($filepath);
    // Execute the lone PHP in 02_Expert.md
    if (strpos($filepath, '02_Expert.md')) {
        $text = preg_replace_callback('#```php([^`]+)\\n```\\s+(?!```html|<pre>)#s', function ($m) {
            eval($m[1]);
            return $m[0];
        }, $file);
    }
    // Execute PHP and replace output
    $text = preg_replace_callback('#(```php([^`]+)\\n```\\s+(?:```\\w*|<pre>)).*?(\\n(?:```|</pre>)(?:\\n|$))#s', function ($m) {
        $php = preg_replace('/\\$configurator =.*/', "\$0\n\$configurator->registeredVars['cacheDir'] = " . var_export(__DIR__ . '/../tests/.cache', true) . ";\n", $m[2]);
        ob_start();
        eval($php);
        return $m[1] . "\n" . rtrim(ob_get_clean(), "\n") . $m[3];
    }, $file);
    if ($text !== $file) {
        echo "Patching {$filepath}\n";
        file_put_contents($filepath, $text);
    }
}
patchDir(__DIR__ . '/../src/Plugins/');
patchDir(__DIR__ . '/../docs/Cookbook/');
patchFile(__DIR__ . '/../docs/StorageFormat.md');
die("Done.\n");