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