*/ if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_asciidocjs extends DokuWiki_Syntax_Plugin { public $scriptid = 0; /** * Get the type of syntax this plugin defines. * * @param none * @return String 'substition' (i.e. 'substitution'). * @public * @static */ function getType(){ return 'protected'; } /** * Where to sort in? * * @param none * @return Integer 6. * @public * @static */ function getSort(){ return 1; } /** * Connect lookup pattern to lexer. * * @param $aMode String The desired rendermode. * @return none * @public * @see render() */ function connectTo($mode) { $this->Lexer->addEntryPattern('',$mode,'plugin_asciidocjs'); $this->Lexer->addEntryPattern('//--asciidoc--//',$mode,'plugin_asciidocjs'); } function postConnect() { $this->Lexer->addExitPattern('','plugin_asciidocjs'); } /** * Handler to prepare matched data for the rendering process. * *

* The $aState parameter gives the type of pattern * which triggered the call to this method: *

* @param $aAscdoc String The asciidoc text to convert. */ function run_asciidoctor($node,$ascdoc,$save_mode) { if ($node==''){ return ''; } $html = ''; $return_value = 1; $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") // stderr ); $cwd = DOKU_PLUGIN.'asciidocjs'; $env = array(); $CMD=$node." asciidoc.js ".$save_mode; $process = proc_open($CMD, $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0],$ascdoc); fclose($pipes[0]); $html=stream_get_contents($pipes[1]); fclose($pipes[1]); $error=stream_get_contents($pipes[2]); fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); } if ($return_value==0) { return $html; } else { return ""; } } /** * Handler to prepare matched data for the rendering process. * *

* The $aState parameter gives the type of pattern * which triggered the call to this method: *

*
*
DOKU_LEXER_ENTER
*
a pattern set by addEntryPattern()
*
DOKU_LEXER_MATCHED
*
a pattern set by addPattern()
*
DOKU_LEXER_EXIT
*
a pattern set by addExitPattern()
*
DOKU_LEXER_SPECIAL
*
a pattern set by addSpecialPattern()
*
DOKU_LEXER_UNMATCHED
*
ordinary text encountered within the plugin's syntax mode * which doesn't match any pattern.
*
* @param $aMatch String The text matched by the patterns. * @param $aState Integer The lexer state for the match. * @param $aPos Integer The character position of the matched text. * @param $aHandler Object Reference to the Doku_Handler object. * @return Integer The current lexer state for the match. * @public * @see render() * @static */ function handle($match, $state, $pos, Doku_Handler $handler){ switch ($state) { case DOKU_LEXER_ENTER : $data =''; if ($this->scriptid==0){ $data .= ''.PHP_EOL; $data .= ''.PHP_EOL; } return array($state, $data, ''); case DOKU_LEXER_MATCHED : break; case DOKU_LEXER_UNMATCHED : $data =''; if ($this->getConf('save_mode')=='server'){ $data.=''; $data.=$this->run_asciidoctor($this->getConf('exec_node'),$match,$this->getConf('save_mode')); $data.=''; } else { $SID="asciidoc_c".strval($this->scriptid); $DID="asciidoc_t".strval($this->scriptid); $this->scriptid+=1; $data.='
'.PHP_EOL; $data.=''.PHP_EOL; $data.=''; } return array($state, $data, $match); case DOKU_LEXER_EXIT : return array($state, '', ''); case DOKU_LEXER_SPECIAL : break; } return array(); } /** * Handle the actual output creation. * *

* The method checks for the given $aFormat and returns * FALSE when a format isn't supported. $aRenderer * contains a reference to the renderer object which is currently * handling the rendering. The contents of $aData is the * return value of the handle() method. *

* @param $aFormat String The output format to generate. * @param $aRenderer Object A reference to the renderer object. * @param $aData Array The data created by the handle() * method. * @return Boolean TRUE if rendered successfully, or * FALSE otherwise. * @public * @see handle() */ function render($mode, Doku_Renderer $renderer, $data) { if($mode == 'xhtml'){ if(is_a($renderer,'renderer_plugin_dw2pdf')){ // this is the PDF export, render simple HTML here $renderer->doc .= $this->run_asciidoctor($this->getConf('exec_node'),$data[2],$this->getConf('save_mode')); }else{ // this is normal XHTML for Browsers, be fancy here $renderer->doc .= $data[1]; } return true; } return false; } } ?>