xref: /dokuwiki/bin/render.php (revision d868eb89f182718a31113373a6272670bd7f8012)
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{
24d421dc12SAndreas Gohr
25b0b7909bSAndreas Gohr    /**
26b0b7909bSAndreas Gohr     * Register options and arguments on the given $options object
27b0b7909bSAndreas Gohr     *
28cbeaa4a0SAndreas Gohr     * @param Options $options
29b0b7909bSAndreas Gohr     * @return void
30b0b7909bSAndreas Gohr     */
31*d868eb89SAndreas Gohr    protected function setup(Options $options)
32*d868eb89SAndreas Gohr    {
33b0b7909bSAndreas Gohr        $options->setHelp(
34b0b7909bSAndreas Gohr            'A simple commandline tool to render some DokuWiki syntax with a given renderer.' .
35b0b7909bSAndreas Gohr            "\n\n" .
36b0b7909bSAndreas Gohr            'This may not work for plugins that expect a certain environment to be ' .
37b0b7909bSAndreas Gohr            'set up before rendering, but should work for most or even all standard ' .
38b0b7909bSAndreas Gohr            'DokuWiki markup'
39b0b7909bSAndreas Gohr        );
40b0b7909bSAndreas Gohr        $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode');
41d421dc12SAndreas Gohr    }
42d421dc12SAndreas Gohr
43b0b7909bSAndreas Gohr    /**
44b0b7909bSAndreas Gohr     * Your main program
45b0b7909bSAndreas Gohr     *
46b0b7909bSAndreas Gohr     * Arguments and options have been parsed when this is run
47b0b7909bSAndreas Gohr     *
48cbeaa4a0SAndreas Gohr     * @param Options $options
49b0b7909bSAndreas Gohr     * @throws DokuCLI_Exception
50b0b7909bSAndreas Gohr     * @return void
51b0b7909bSAndreas Gohr     */
52*d868eb89SAndreas Gohr    protected function main(Options $options)
53*d868eb89SAndreas Gohr    {
54b0b7909bSAndreas Gohr        $renderer = $options->getOpt('renderer', 'xhtml');
55d421dc12SAndreas Gohr
56d421dc12SAndreas Gohr        // do the action
57d421dc12SAndreas Gohr        $source = stream_get_contents(STDIN);
58b1f206e1SAndreas Gohr        $info = [];
59b0b7909bSAndreas Gohr        $result = p_render($renderer, p_get_instructions($source), $info);
60b0b7909bSAndreas Gohr        if(is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer");
61214dce0fSAndreas Gohr        echo $result;
62d421dc12SAndreas Gohr    }
63b0b7909bSAndreas Gohr}
64b0b7909bSAndreas Gohr
65b0b7909bSAndreas Gohr// Main
66b0b7909bSAndreas Gohr$cli = new RenderCLI();
67b0b7909bSAndreas Gohr$cli->run();
68