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