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