DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:dokuwiki_parser_cli

This is an old revision of the document!


Parse DokuWiki from Command Line

The following script takes DokuWiki-formatted text (wikitext) as input, and outputs the resulting (XHTML) render content. It is useful for pipes and generating content dynamically. This was posted somewhere around the DokuWiki forums or IRC channel. Its authorship may be lost in time but as this is an useful script I've pasted it here in case it is needed.

dokucli.php
<?php
if ('cli' != php_sapi_name()) die();
 
ini_set('memory_limit','128M');
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
define('NOSESSION',1);
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'inc/parserutils.php');
 
 
$source = stream_get_contents(STDIN);
$info = array();
echo p_render('xhtml',p_get_instructions($source),$info);
?>

Requirements

  • PHP-CLI tools (eg.: php-cli in Debian)
  • A DokuWiki installation.
  • Save this script in your DokuWiki's bin/ folder (alongside the indexer).
  • (optional) Give the script execution permissions.

Usage

From the command line shell:

php $PATH_TO_DOKUWIKI/bin/dokucli.php < input.txt > output.html
... or
some_command_that_writes_wikicode input | php $PATH_TO_DOKUWIKI/bin/dokucli.php

Where:

  • $PATH_TO_DOKUWIKI is, of course, the Path to your DokuWiki.
  • input and output are optional (the script reads from the Standard Input and writes to the Standard Output)

If you execute the script without parameters (to type in to the standard inpit directly), remember that the input finishes with your platform's “end-of-file” marker, that is CTRL+D in Unix and CTRL-Z in Windows.


Added by ryan.chappelle as suggested in the IRC Channel.

If someone could discover the authorship of this code snippet in order to credit it accordingly, I at least would be very happy. — ryan.chappelle 2011/01/03 20:02

Shell Wrapper Script

Here's a shell script that I've found useful for passing documents to the PHP files. I named it doku2html and put it in the path. –Donald Merand 07.03.2011 - http://www.explo.org

You use it like this:

doku2html file1
#... or
doku2html file1 file2 file3...
#... or
some_command_that_writes_wikicode_input | doku2html
#... to output to a file instead of STDOUT:
doku2html file1 > output_file.html
#just make sure that doku2html is in your path somewhere!

If you want to make the doku2html file executable right from the command line, you could type:

chmod 775 doku2html

The advantage of using this script is that you can easily pass multiple files, which are concatenated with a blank space between them. Input that's not a valid file is ignored. If no parameters are passed, use STDIN just like the PHP script above.

doku2html
#!/bin/bash
#Author: Donald L. Merand
 
#path to the php file to run dokuwiki parsing from the CLI
phpcli=path_to_dokuwiki/bin/dokucli.php
 
#if parameters are not empty, assume files have been passed
if [ $# -gt 0 ]
then
	for file in "$@"
	do
		#ignore any non-files that are passed as parameters
		if [ -f "$file" ]
		then
			cat "$file"
			echo #line break between files
		fi
	done |
	#    ^ note the pipe
	#now make one big HTML document out of whatever is passed
	php $phpcli
else
	#read from stdin
	php $phpcli
fi
tips/dokuwiki_parser_cli.1309724774.txt.gz · Last modified: 2011-07-03 22:26 by 108.20.106.169

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki