From bdf09125c45151dc18a93e723586dbb1071c5e0c Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 28 Sep 2020 16:59:46 +0200 Subject: [PATCH] set page context in Handler Under some circumstances it is useful to know if the current parsing process is running in a page context or not. One of the examples is the struct plugin's use of the PARSER_HANDLER_DONE event to automatically inject its own output instruction. Currently this instruction is injected in each and every processing and only on rendering is decided if there is anything to output. This patch injects a page hint into the Handler when the handler was created from our p_wiki_xhtml() method (and only then). Plugins can use the getPage() method to check the context if needed. Note: currently p_wiki_xhtml() also modifies the global $ID variable which might also used as context hint (but will also be set during on-the-fly renders, eg. the preview intro text). It would probably be a good idea to check if it would be better when all handle methods rely on getPage() instead of global $ID instead. This would be a major change in parsing though and needs serious testing while this addition has basically no consequences currently. --- inc/parser/handler.php | 26 ++++++++++++++++++++++++++ inc/parserutils.php | 21 +++++++++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/inc/parser/handler.php b/inc/parser/handler.php index a3609609c..3538c78b0 100644 --- a/inc/parser/handler.php +++ b/inc/parser/handler.php @@ -30,6 +30,9 @@ class Doku_Handler { /** @var bool should blocks be rewritten? FIXME seems to always be true */ protected $rewriteBlocks = true; + /** @var string|null the currently processed page, null for on the fly renders */ + protected $page = null; + /** * Doku_Handler constructor. */ @@ -37,6 +40,29 @@ class Doku_Handler { $this->callWriter = new CallWriter($this); } + /** + * Set the currently processed page + * + * @param string $id + */ + public function setPage($id) + { + $this->page = $id; + } + + /** + * Get the currently processed page + * + * This may return null when the parsing/handling process is called "on the fly", eg. to + * render a wiki text snippet somewhere. + * + * @return string|null + */ + public function getPage() + { + return $this->page; + } + /** * Add a new call by passing it to the current CallWriter * diff --git a/inc/parserutils.php b/inc/parserutils.php index 846be54db..ffbcc872c 100644 --- a/inc/parserutils.php +++ b/inc/parserutils.php @@ -80,7 +80,7 @@ function p_wiki_xhtml($id, $rev='', $excuse=true,$date_at=''){ if($rev || $date_at){ if(file_exists($file)){ //no caching on old revisions - $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev)),$info,$date_at); + $ret = p_render('xhtml',p_get_instructions(io_readWikiPage($file,$id,$rev), $id),$info,$date_at); }elseif($excuse){ $ret = p_locale_xhtml('norev'); } @@ -193,27 +193,32 @@ function p_cached_instructions($file,$cacheonly=false,$id='') { /** * turns a page into a list of instructions * - * @author Harry Fuecks + * @param string $text raw wiki syntax text + * @param string $id optional hint on the currently processed page + * @return array a list of instruction arrays * @author Andreas Gohr * - * @param string $text raw wiki syntax text - * @return array a list of instruction arrays + * @author Harry Fuecks */ -function p_get_instructions($text){ +function p_get_instructions($text, $id=''){ $modes = p_get_parsermodes(); // Create the parser and handler - $Parser = new Parser(new Doku_Handler()); + $handler = new Doku_Handler(); + if($id !== '') { + $handler->setPage($id); + } + $parser = new Parser($handler); //add modes to parser foreach($modes as $mode){ - $Parser->addMode($mode['mode'],$mode['obj']); + $parser->addMode($mode['mode'],$mode['obj']); } // Do the parsing Event::createAndTrigger('PARSER_WIKITEXT_PREPROCESS', $text); - $p = $Parser->parse($text); + $p = $parser->parse($text); // dbg($p); return $p; }