xref: /dokuwiki/bin/render.php (revision cbfa4829d9bcd40d1cc3b9220fe78fa37c385c02)
1*cbfa4829SPhy#!/usr/bin/env php
2d421dc12SAndreas Gohr<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7b0b7909bSAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
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 */
22cbeaa4a0SAndreas Gohrclass RenderCLI extends CLI {
23d421dc12SAndreas 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     */
30cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
31b0b7909bSAndreas Gohr        $options->setHelp(
32b0b7909bSAndreas Gohr            'A simple commandline tool to render some DokuWiki syntax with a given renderer.' .
33b0b7909bSAndreas Gohr            "\n\n" .
34b0b7909bSAndreas Gohr            'This may not work for plugins that expect a certain environment to be ' .
35b0b7909bSAndreas Gohr            'set up before rendering, but should work for most or even all standard ' .
36b0b7909bSAndreas Gohr            'DokuWiki markup'
37b0b7909bSAndreas Gohr        );
38b0b7909bSAndreas Gohr        $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode');
39d421dc12SAndreas Gohr    }
40d421dc12SAndreas Gohr
41b0b7909bSAndreas Gohr    /**
42b0b7909bSAndreas Gohr     * Your main program
43b0b7909bSAndreas Gohr     *
44b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
45b0b7909bSAndreas Gohr     *
46cbeaa4a0SAndreas Gohr     * @param Options $options
47b0b7909bSAndreas Gohr     * @throws DokuCLI_Exception
48b0b7909bSAndreas Gohr     * @return void
49b0b7909bSAndreas Gohr     */
50cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
51b0b7909bSAndreas Gohr        $renderer = $options->getOpt('renderer', 'xhtml');
52d421dc12SAndreas Gohr
53d421dc12SAndreas Gohr        // do the action
54d421dc12SAndreas Gohr        $source = stream_get_contents(STDIN);
55d421dc12SAndreas Gohr        $info = array();
56b0b7909bSAndreas Gohr        $result = p_render($renderer, p_get_instructions($source), $info);
57b0b7909bSAndreas Gohr        if(is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer");
58214dce0fSAndreas Gohr        echo $result;
59d421dc12SAndreas Gohr    }
60b0b7909bSAndreas Gohr}
61b0b7909bSAndreas Gohr
62b0b7909bSAndreas Gohr// Main
63b0b7909bSAndreas Gohr$cli = new RenderCLI();
64b0b7909bSAndreas Gohr$cli->run();
65