xref: /dokuwiki/bin/render.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1cbfa4829SPhy#!/usr/bin/env php
2d421dc12SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7b1f206e1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
8b0b7909bSAndreas Gohrdefine('NOSESSION', 1);
9b0b7909bSAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
10b0b7909bSAndreas Gohr
11d421dc12SAndreas Gohr/**
12d421dc12SAndreas Gohr * A simple commandline tool to render some DokuWiki syntax with a given
13d421dc12SAndreas Gohr * renderer.
14d421dc12SAndreas Gohr *
15d421dc12SAndreas Gohr * This may not work for plugins that expect a certain environment to be
16d421dc12SAndreas Gohr * set up before rendering, but should work for most or even all standard
17d421dc12SAndreas Gohr * DokuWiki markup
18d421dc12SAndreas Gohr *
19d421dc12SAndreas Gohr * @license GPL2
20d421dc12SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
21d421dc12SAndreas Gohr */
228c7c53b0SAndreas Gohrclass RenderCLI extends CLI
238c7c53b0SAndreas Gohr{
24b0b7909bSAndreas Gohr    /**
25b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
26b0b7909bSAndreas Gohr     *
27cbeaa4a0SAndreas Gohr     * @param Options $options
28b0b7909bSAndreas Gohr     * @return void
29b0b7909bSAndreas Gohr     */
30*d868eb89SAndreas Gohr    protected function setup(Options $options)
31*d868eb89SAndreas Gohr    {
32b0b7909bSAndreas Gohr        $options->setHelp(
33b0b7909bSAndreas Gohr            'A simple commandline tool to render some DokuWiki syntax with a given renderer.' .
34b0b7909bSAndreas Gohr            "\n\n" .
35b0b7909bSAndreas Gohr            'This may not work for plugins that expect a certain environment to be ' .
36b0b7909bSAndreas Gohr            'set up before rendering, but should work for most or even all standard ' .
37b0b7909bSAndreas Gohr            'DokuWiki markup'
38b0b7909bSAndreas Gohr        );
39b0b7909bSAndreas Gohr        $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode');
40d421dc12SAndreas Gohr    }
41d421dc12SAndreas Gohr
42b0b7909bSAndreas Gohr    /**
43b0b7909bSAndreas Gohr     * Your main program
44b0b7909bSAndreas Gohr     *
45b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
46b0b7909bSAndreas Gohr     *
47cbeaa4a0SAndreas Gohr     * @param Options $options
48b0b7909bSAndreas Gohr     * @throws DokuCLI_Exception
49b0b7909bSAndreas Gohr     * @return void
50b0b7909bSAndreas Gohr     */
51*d868eb89SAndreas Gohr    protected function main(Options $options)
52*d868eb89SAndreas Gohr    {
53b0b7909bSAndreas Gohr        $renderer = $options->getOpt('renderer', 'xhtml');
54d421dc12SAndreas Gohr
55d421dc12SAndreas Gohr        // do the action
56d421dc12SAndreas Gohr        $source = stream_get_contents(STDIN);
57b1f206e1SAndreas Gohr        $info = [];
58b0b7909bSAndreas Gohr        $result = p_render($renderer, p_get_instructions($source), $info);
59b0b7909bSAndreas Gohr        if (is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer");
60214dce0fSAndreas Gohr        echo $result;
61d421dc12SAndreas Gohr    }
62b0b7909bSAndreas Gohr}
63b0b7909bSAndreas Gohr
64b0b7909bSAndreas Gohr// Main
65b0b7909bSAndreas Gohr$cli = new RenderCLI();
66b0b7909bSAndreas Gohr$cli->run();
67