xref: /dokuwiki/bin/render.php (revision 214dce0ff011f87cd6a09c3ae89d17cf4cefc7a4)
1d421dc12SAndreas Gohr#!/usr/bin/php
2d421dc12SAndreas Gohr<?php
3d421dc12SAndreas Gohr/**
4d421dc12SAndreas Gohr * A simple commandline tool to render some DokuWiki syntax with a given
5d421dc12SAndreas Gohr * renderer.
6d421dc12SAndreas Gohr *
7d421dc12SAndreas Gohr * This may not work for plugins that expect a certain environment to be
8d421dc12SAndreas Gohr * set up before rendering, but should work for most or even all standard
9d421dc12SAndreas Gohr * DokuWiki markup
10d421dc12SAndreas Gohr *
11d421dc12SAndreas Gohr * @license GPL2
12d421dc12SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
13d421dc12SAndreas Gohr */
14d421dc12SAndreas Gohrif ('cli' != php_sapi_name()) die();
15d421dc12SAndreas Gohr
16d421dc12SAndreas Gohrini_set('memory_limit','128M');
17d421dc12SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
18d421dc12SAndreas Gohrdefine('NOSESSION',1);
19d421dc12SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
20d421dc12SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
21d421dc12SAndreas Gohrrequire_once(DOKU_INC.'inc/parserutils.php');
22d421dc12SAndreas Gohrrequire_once(DOKU_INC.'inc/cliopts.php');
23d421dc12SAndreas Gohr
24d421dc12SAndreas Gohr// handle options
25d421dc12SAndreas Gohr$short_opts = 'hr:';
26d421dc12SAndreas Gohr$long_opts  = array('help','renderer:');
27d421dc12SAndreas Gohr$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts);
28d421dc12SAndreas Gohrif ( $OPTS->isError() ) {
29d421dc12SAndreas Gohr    fwrite( STDERR, $OPTS->getMessage() . "\n");
30d421dc12SAndreas Gohr    _usage();
31d421dc12SAndreas Gohr    exit(1);
32d421dc12SAndreas Gohr}
33d421dc12SAndreas Gohr$RENDERER = 'xhtml';
34d421dc12SAndreas Gohrforeach ($OPTS->options as $key => $val) {
35d421dc12SAndreas Gohr    switch ($key) {
36d421dc12SAndreas Gohr        case 'h':
37d421dc12SAndreas Gohr        case 'help':
38d421dc12SAndreas Gohr            _usage();
39d421dc12SAndreas Gohr            exit;
40d421dc12SAndreas Gohr        case 'r':
41d421dc12SAndreas Gohr        case 'renderer':
42d421dc12SAndreas Gohr            $RENDERER = $val;
43d421dc12SAndreas Gohr    }
44d421dc12SAndreas Gohr}
45d421dc12SAndreas Gohr
46d421dc12SAndreas Gohr
47d421dc12SAndreas Gohr// do the action
48d421dc12SAndreas Gohr$source = stream_get_contents(STDIN);
49d421dc12SAndreas Gohr$info = array();
50d421dc12SAndreas Gohr$result = p_render($RENDERER,p_get_instructions($source),$info);
51d421dc12SAndreas Gohrif(is_null($result)) die("No such renderer $RENDERER\n");
52*214dce0fSAndreas Gohrecho $result;
53d421dc12SAndreas Gohr
54d421dc12SAndreas Gohr/**
55d421dc12SAndreas Gohr * Print usage info
56d421dc12SAndreas Gohr */
57d421dc12SAndreas Gohrfunction _usage(){
58d421dc12SAndreas Gohr    print "Usage: render.php <options>
59d421dc12SAndreas Gohr
60d421dc12SAndreas Gohr    Reads DokuWiki syntax from STDIN and renders it with the given renderer
61d421dc12SAndreas Gohr    to STDOUT
62d421dc12SAndreas Gohr
63d421dc12SAndreas Gohr    OPTIONS
64d421dc12SAndreas Gohr        -h, --help                 show this help and exit
65d421dc12SAndreas Gohr        -r, --renderer <renderer>  the render mode (default: xhtml)
66d421dc12SAndreas Gohr";
67d421dc12SAndreas Gohr}
68