xref: /dokuwiki/inc/parserutils.php (revision d978e24c81cc608e308869a2099970bb53e115a2)
1c112d578Sandi<?php
2c112d578Sandi/**
3c112d578Sandi * Utilities for collecting data from config files
4c112d578Sandi *
5c112d578Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6c112d578Sandi * @author     Harry Fuecks <hfuecks@gmail.com>
7c112d578Sandi * @author     Andreas Gohr <andi@splitbrain.org>
8c112d578Sandi */
9c112d578Sandi
10c112d578Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
11c112d578Sandi
12c112d578Sandi  require_once(DOKU_INC.'inc/confutils.php');
13c112d578Sandi  require_once(DOKU_INC.'inc/pageutils.php');
14ee20e7d1Sandi  require_once(DOKU_INC.'inc/pluginutils.php');
15c112d578Sandi
16c112d578Sandi/**
17c112d578Sandi * Returns the parsed Wikitext in XHTML for the given id and revision.
18c112d578Sandi *
19c112d578Sandi * If $excuse is true an explanation is returned if the file
20c112d578Sandi * wasn't found
21c112d578Sandi *
22c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
23c112d578Sandi */
24c112d578Sandifunction p_wiki_xhtml($id, $rev='', $excuse=true){
25c112d578Sandi  $file = wikiFN($id,$rev);
26c112d578Sandi  $ret  = '';
27c112d578Sandi
28c112d578Sandi  //ensure $id is in global $ID (needed for parsing)
291e76272cSandi  global $ID;
303ff8773bSAndreas Gohr  $keep = $ID;
311e76272cSandi  $ID   = $id;
32c112d578Sandi
33c112d578Sandi  if($rev){
34c112d578Sandi    if(@file_exists($file)){
359dc2c2afSandi      $ret = p_render('xhtml',p_get_instructions(io_readfile($file)),$info); //no caching on old revisions
36c112d578Sandi    }elseif($excuse){
37c112d578Sandi      $ret = p_locale_xhtml('norev');
38c112d578Sandi    }
39c112d578Sandi  }else{
40c112d578Sandi    if(@file_exists($file)){
41c112d578Sandi      $ret = p_cached_xhtml($file);
42c112d578Sandi    }elseif($excuse){
43c112d578Sandi      $ret = p_locale_xhtml('newpage');
44c112d578Sandi    }
45c112d578Sandi  }
46c112d578Sandi
473ff8773bSAndreas Gohr  //restore ID (just in case)
483ff8773bSAndreas Gohr  $ID = $keep;
493ff8773bSAndreas Gohr
50c112d578Sandi  return $ret;
51c112d578Sandi}
52c112d578Sandi
53c112d578Sandi/**
546b7b33dcShfuecks * Returns starting summary for a page (e.g. the first few
556b7b33dcShfuecks * paragraphs), marked up in XHTML.
566b7b33dcShfuecks *
576b7b33dcShfuecks * If $excuse is true an explanation is returned if the file
586b7b33dcShfuecks * wasn't found
596b7b33dcShfuecks *
606b7b33dcShfuecks * @param string wiki page id
616b7b33dcShfuecks * @param reference populated with page title from heading or page id
628716966dSAndreas Gohr * @deprecated
638716966dSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com>
646b7b33dcShfuecks */
656b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
666b7b33dcShfuecks  $file = wikiFN($id,$rev);
676b7b33dcShfuecks  $ret  = '';
686b7b33dcShfuecks
696b7b33dcShfuecks  //ensure $id is in global $ID (needed for parsing)
706b7b33dcShfuecks  global $ID;
716b7b33dcShfuecks  $keep = $ID;
726b7b33dcShfuecks  $ID   = $id;
736b7b33dcShfuecks
746b7b33dcShfuecks  if($rev){
756b7b33dcShfuecks    if(@file_exists($file)){
766b7b33dcShfuecks      //no caching on old revisions
776b7b33dcShfuecks      $ins = p_get_instructions(io_readfile($file));
786b7b33dcShfuecks    }elseif($excuse){
796b7b33dcShfuecks      $ret = p_locale_xhtml('norev');
806b7b33dcShfuecks      //restore ID (just in case)
816b7b33dcShfuecks      $ID = $keep;
826b7b33dcShfuecks      return $ret;
836b7b33dcShfuecks    }
846b7b33dcShfuecks
856b7b33dcShfuecks  }else{
866b7b33dcShfuecks
876b7b33dcShfuecks    if(@file_exists($file)){
886b7b33dcShfuecks      // The XHTML for a summary is not cached so use the instruction cache
896b7b33dcShfuecks      $ins = p_cached_instructions($file);
906b7b33dcShfuecks    }elseif($excuse){
916b7b33dcShfuecks      $ret = p_locale_xhtml('newpage');
926b7b33dcShfuecks      //restore ID (just in case)
936b7b33dcShfuecks      $ID = $keep;
946b7b33dcShfuecks      return $ret;
956b7b33dcShfuecks    }
966b7b33dcShfuecks  }
976b7b33dcShfuecks
986b7b33dcShfuecks  $ret = p_render('xhtmlsummary',$ins,$info);
996b7b33dcShfuecks
1006b7b33dcShfuecks  if ( $info['sum_pagetitle'] ) {
1016b7b33dcShfuecks    $title = $info['sum_pagetitle'];
1026b7b33dcShfuecks  } else {
1036b7b33dcShfuecks    $title = $id;
1046b7b33dcShfuecks  }
1056b7b33dcShfuecks
1066b7b33dcShfuecks  $ID = $keep;
1076b7b33dcShfuecks  return $ret;
1086b7b33dcShfuecks}
1096b7b33dcShfuecks
1106b7b33dcShfuecks/**
111c112d578Sandi * Returns the specified local text in parsed format
112c112d578Sandi *
113c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
114c112d578Sandi */
115c112d578Sandifunction p_locale_xhtml($id){
116c112d578Sandi  //fetch parsed locale
117c112d578Sandi  $html = p_cached_xhtml(localeFN($id));
118c112d578Sandi  return $html;
119c112d578Sandi}
120c112d578Sandi
121c112d578Sandi/**
122c112d578Sandi * Returns the given file parsed to XHTML
123c112d578Sandi *
124c112d578Sandi * Uses and creates a cachefile
125c112d578Sandi *
126c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
1279dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
128c112d578Sandi */
129c112d578Sandifunction p_cached_xhtml($file){
130c112d578Sandi  global $conf;
13198407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
132c7532350SDrew Amato  $purge  = $conf['cachedir'].'/purgefile';
133c112d578Sandi
134c112d578Sandi  // check if cache can be used
135c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
136c112d578Sandi
137c112d578Sandi  if( @file_exists($file)                                             // does the source exist
138c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
139c112d578Sandi      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
140c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
1411094c798Sandi      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
142e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
143e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
144c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
145c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
146c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
147c112d578Sandi  {
148c112d578Sandi    //well then use the cache
149c112d578Sandi    $parsed = io_readfile($cache);
150f42d1c75SAndreas Gohr    if($conf['allowdebug']) $parsed .= "\n<!-- cachefile $cache used -->\n";
151c112d578Sandi  }else{
1529dc2c2afSandi    $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions
153c112d578Sandi
1549dc2c2afSandi    if($info['cache']){
155c112d578Sandi      io_saveFile($cache,$parsed); //save cachefile
156f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, but created -->\n";
157c112d578Sandi    }else{
158c112d578Sandi      @unlink($cache); //try to delete cachefile
159f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
160c112d578Sandi    }
161c112d578Sandi  }
162c112d578Sandi
163c112d578Sandi  return $parsed;
164c112d578Sandi}
165c112d578Sandi
166c112d578Sandi/**
167c112d578Sandi * Returns the render instructions for a file
168c112d578Sandi *
169c112d578Sandi * Uses and creates a serialized cache file
170c112d578Sandi *
171c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
172c112d578Sandi */
17337e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){
174c112d578Sandi  global $conf;
17598407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.i');
176c112d578Sandi
177c112d578Sandi  // check if cache can be used
178c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
179c112d578Sandi
18037e34a5eSandi  // cache forced?
18137e34a5eSandi  if($cacheonly){
18237e34a5eSandi    if($cachetime){
183e34c0709SAndreas Gohr      return unserialize(io_readfile($cache,false));
18437e34a5eSandi    }else{
185fd198316Sandi      return array();
18637e34a5eSandi    }
18737e34a5eSandi  }
18837e34a5eSandi
189c112d578Sandi  if( @file_exists($file)                                             // does the source exist
190c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
191c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
192e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
193e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
194c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
195c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
196c112d578Sandi  {
197c112d578Sandi    //well then use the cache
198e34c0709SAndreas Gohr    return unserialize(io_readfile($cache,false));
199c112d578Sandi  }elseif(@file_exists($file)){
200c112d578Sandi    // no cache - do some work
2016bbae538Sandi    $ins = p_get_instructions(io_readfile($file));
202c112d578Sandi    io_savefile($cache,serialize($ins));
203c112d578Sandi    return $ins;
204c112d578Sandi  }
205c112d578Sandi
206c112d578Sandi  return NULL;
207c112d578Sandi}
208c112d578Sandi
209c112d578Sandi/**
210c112d578Sandi * turns a page into a list of instructions
211c112d578Sandi *
212c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
213c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
214c112d578Sandi */
2156bbae538Sandifunction p_get_instructions($text){
216c112d578Sandi
217107b01d6Sandi  $modes = p_get_parsermodes();
218ee20e7d1Sandi
219c112d578Sandi  // Create the parser
220c112d578Sandi  $Parser = & new Doku_Parser();
221c112d578Sandi
222c112d578Sandi  // Add the Handler
223c112d578Sandi  $Parser->Handler = & new Doku_Handler();
224c112d578Sandi
225107b01d6Sandi  //add modes to parser
226107b01d6Sandi  foreach($modes as $mode){
227107b01d6Sandi    $Parser->addMode($mode['mode'],$mode['obj']);
228c112d578Sandi  }
229c112d578Sandi
230c112d578Sandi  // Do the parsing
231677844afSchris  trigger_event('PARSER_WIKITEXT_PREPROCESS', $text);
232a2d649c4Sandi  $p = $Parser->parse($text);
233ee20e7d1Sandi//  dbg($p);
234a2d649c4Sandi  return $p;
235c112d578Sandi}
236c112d578Sandi
237c112d578Sandi/**
23839a89382SEsther Brunner * returns the metadata of a page
23939a89382SEsther Brunner *
24039a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
24139a89382SEsther Brunner */
24239a89382SEsther Brunnerfunction p_get_metadata($id, $key=false, $render=false){
24339a89382SEsther Brunner  $file = metaFN($id, '.meta');
24439a89382SEsther Brunner
24539a89382SEsther Brunner  if (@file_exists($file)) $meta = unserialize(io_readFile($file, false));
24639a89382SEsther Brunner  else $meta = array();
24739a89382SEsther Brunner
24839a89382SEsther Brunner  // metadata has never been rendered before - do it!
24939a89382SEsther Brunner  if ($render && !$meta['description']['abstract']){
25039a89382SEsther Brunner    $meta = p_render_metadata($id, $meta);
25139a89382SEsther Brunner    io_saveFile($file, serialize($meta));
25239a89382SEsther Brunner  }
25339a89382SEsther Brunner
25439a89382SEsther Brunner  // filter by $key
25539a89382SEsther Brunner  if ($key){
25639a89382SEsther Brunner    list($key, $subkey) = explode(' ', $key, 2);
25739a89382SEsther Brunner    if (trim($subkey)) return $meta[$key][$subkey];
25839a89382SEsther Brunner    else return $meta[$key];
25939a89382SEsther Brunner  }
26039a89382SEsther Brunner
26139a89382SEsther Brunner  return $meta;
26239a89382SEsther Brunner}
26339a89382SEsther Brunner
26439a89382SEsther Brunner/**
26539a89382SEsther Brunner * sets metadata elements of a page
26639a89382SEsther Brunner *
26739a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
26839a89382SEsther Brunner */
26939a89382SEsther Brunnerfunction p_set_metadata($id, $data, $render=false){
27039a89382SEsther Brunner  if (!is_array($data)) return false;
27139a89382SEsther Brunner
27239a89382SEsther Brunner  $orig = p_get_metadata($id);
27339a89382SEsther Brunner
27439a89382SEsther Brunner  // render metadata first?
27539a89382SEsther Brunner  if ($render) $meta = p_render_metadata($id, $orig);
27639a89382SEsther Brunner  else $meta = $orig;
27739a89382SEsther Brunner
27839a89382SEsther Brunner  // now add the passed metadata
27939a89382SEsther Brunner  $protected = array('description', 'date', 'contributor');
28039a89382SEsther Brunner  foreach ($data as $key => $value){
28139a89382SEsther Brunner
28239a89382SEsther Brunner    // be careful with sub-arrays of $meta['relation']
28339a89382SEsther Brunner    if ($key == 'relation'){
28439a89382SEsther Brunner      foreach ($value as $subkey => $subvalue){
28539a89382SEsther Brunner        $meta[$key][$subkey] = array_merge($meta[$key][$subkey], $subvalue);
28639a89382SEsther Brunner      }
28739a89382SEsther Brunner
28839a89382SEsther Brunner    // be careful with some senisitive arrays of $meta
28939a89382SEsther Brunner    } elseif (in_array($key, $protected)){
29039a89382SEsther Brunner      if (is_array($value)){
29195dbfe57SAndreas Gohr        #FIXME not sure if this is the intended thing:
29295dbfe57SAndreas Gohr        if(!is_array($meta[$key])) $meta[$key] = array($meta[$key]);
29339a89382SEsther Brunner        $meta[$key] = array_merge($meta[$key], $value);
29439a89382SEsther Brunner      }
29539a89382SEsther Brunner
29639a89382SEsther Brunner    // no special treatment for the rest
29739a89382SEsther Brunner    } else {
29839a89382SEsther Brunner      $meta[$key] = $value;
29939a89382SEsther Brunner    }
30039a89382SEsther Brunner  }
30139a89382SEsther Brunner
30239a89382SEsther Brunner  // save only if metadata changed
30339a89382SEsther Brunner  if ($meta == $orig) return true;
30439a89382SEsther Brunner  return io_saveFile(metaFN($id, '.meta'), serialize($meta));
30539a89382SEsther Brunner}
30639a89382SEsther Brunner
30739a89382SEsther Brunner/**
30839a89382SEsther Brunner * renders the metadata of a page
30939a89382SEsther Brunner *
31039a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
31139a89382SEsther Brunner */
31239a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
31339a89382SEsther Brunner  require_once DOKU_INC."inc/parser/metadata.php";
31439a89382SEsther Brunner
31539a89382SEsther Brunner  // get instructions
31639a89382SEsther Brunner  $instructions = p_cached_instructions(wikiFN($id));
31739a89382SEsther Brunner
31839a89382SEsther Brunner  // set up the renderer
31939a89382SEsther Brunner  $renderer = & new Doku_Renderer_metadata();
32039a89382SEsther Brunner  $renderer->meta = $orig;
32139a89382SEsther Brunner
32239a89382SEsther Brunner  // loop through the instructions
32339a89382SEsther Brunner  foreach ($instructions as $instruction){
32439a89382SEsther Brunner    // execute the callback against the renderer
32539a89382SEsther Brunner    call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
32639a89382SEsther Brunner  }
32739a89382SEsther Brunner
32839a89382SEsther Brunner  return $renderer->meta;
32939a89382SEsther Brunner}
33039a89382SEsther Brunner
33139a89382SEsther Brunner/**
332107b01d6Sandi * returns all available parser syntax modes in correct order
333107b01d6Sandi *
334107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
335107b01d6Sandi */
336107b01d6Sandifunction p_get_parsermodes(){
337107b01d6Sandi  global $conf;
338107b01d6Sandi
339107b01d6Sandi  //reuse old data
340107b01d6Sandi  static $modes = null;
341107b01d6Sandi  if($modes != null){
342107b01d6Sandi    return $modes;
343107b01d6Sandi  }
344107b01d6Sandi
345107b01d6Sandi  //import parser classes and mode definitions
346107b01d6Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
347107b01d6Sandi
348107b01d6Sandi  // we now collect all syntax modes and their objects, then they will
349107b01d6Sandi  // be sorted and added to the parser in correct order
350107b01d6Sandi  $modes = array();
351107b01d6Sandi
352107b01d6Sandi  // add syntax plugins
353107b01d6Sandi  $pluginlist = plugin_list('syntax');
354107b01d6Sandi  if(count($pluginlist)){
355107b01d6Sandi    global $PARSER_MODES;
356107b01d6Sandi    $obj = null;
357107b01d6Sandi    foreach($pluginlist as $p){
358c90b2fb1Schris      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
359107b01d6Sandi      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
360107b01d6Sandi      //add to modes
361107b01d6Sandi      $modes[] = array(
362107b01d6Sandi                   'sort' => $obj->getSort(),
363107b01d6Sandi                   'mode' => "plugin_$p",
364107b01d6Sandi                   'obj'  => $obj,
365107b01d6Sandi                 );
366a46d0d65SAndreas Gohr      unset($obj); //remove the reference
367107b01d6Sandi    }
368107b01d6Sandi  }
369107b01d6Sandi
370107b01d6Sandi  // add default modes
371107b01d6Sandi  $std_modes = array('listblock','preformatted','notoc','nocache',
372107b01d6Sandi                     'header','table','linebreak','footnote','hr',
373107b01d6Sandi                     'unformatted','php','html','code','file','quote',
374e77ea1bcSAndreas Gohr                     'internallink','rss','media','externallink',
375e77ea1bcSAndreas Gohr                     'emaillink','windowssharelink','eol');
376e77ea1bcSAndreas Gohr  if($conf['typography']){
377e77ea1bcSAndreas Gohr    $std_modes[] = 'quotes';
378e77ea1bcSAndreas Gohr    $std_modes[] = 'multiplyentity';
379e77ea1bcSAndreas Gohr  }
380107b01d6Sandi  foreach($std_modes as $m){
381107b01d6Sandi    $class = "Doku_Parser_Mode_$m";
382107b01d6Sandi    $obj   = new $class();
383107b01d6Sandi    $modes[] = array(
384107b01d6Sandi                 'sort' => $obj->getSort(),
385107b01d6Sandi                 'mode' => $m,
386107b01d6Sandi                 'obj'  => $obj
387107b01d6Sandi               );
388107b01d6Sandi  }
389107b01d6Sandi
390107b01d6Sandi  // add formatting modes
391107b01d6Sandi  $fmt_modes = array('strong','emphasis','underline','monospace',
392107b01d6Sandi                     'subscript','superscript','deleted');
393107b01d6Sandi  foreach($fmt_modes as $m){
394107b01d6Sandi    $obj   = new Doku_Parser_Mode_formatting($m);
395107b01d6Sandi    $modes[] = array(
396107b01d6Sandi                 'sort' => $obj->getSort(),
397107b01d6Sandi                 'mode' => $m,
398107b01d6Sandi                 'obj'  => $obj
399107b01d6Sandi               );
400107b01d6Sandi  }
401107b01d6Sandi
402107b01d6Sandi  // add modes which need files
403107b01d6Sandi  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
404107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
405107b01d6Sandi  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
406107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
407107b01d6Sandi  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
408107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
409107b01d6Sandi
410107b01d6Sandi
411107b01d6Sandi  // add optional camelcase mode
412107b01d6Sandi  if($conf['camelcase']){
413107b01d6Sandi    $obj     = new Doku_Parser_Mode_camelcaselink();
414107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
415107b01d6Sandi  }
416107b01d6Sandi
417107b01d6Sandi  //sort modes
418107b01d6Sandi  usort($modes,'p_sort_modes');
419107b01d6Sandi
420107b01d6Sandi  return $modes;
421107b01d6Sandi}
422107b01d6Sandi
423107b01d6Sandi/**
424107b01d6Sandi * Callback function for usort
425107b01d6Sandi *
426107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
427107b01d6Sandi */
428107b01d6Sandifunction p_sort_modes($a, $b){
429107b01d6Sandi  if($a['sort'] == $b['sort']) return 0;
430107b01d6Sandi  return ($a['sort'] < $b['sort']) ? -1 : 1;
431107b01d6Sandi}
432107b01d6Sandi
433107b01d6Sandi/**
434ac83b9d8Sandi * Renders a list of instruction to the specified output mode
435c112d578Sandi *
4369dc2c2afSandi * In the $info array are informations from the renderer returned
4379dc2c2afSandi *
438c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
439c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
440c112d578Sandi */
4419dc2c2afSandifunction p_render($mode,$instructions,& $info){
442c112d578Sandi  if(is_null($instructions)) return '';
443c112d578Sandi
444c19c9173SBen Coburn  if ($mode=='wiki') { msg("Renderer for $mode not valid",-1); return null; } //FIXME!! remove this line when inc/parser/wiki.php works.
445c19c9173SBen Coburn
446c112d578Sandi  // Create the renderer
447ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
448ac83b9d8Sandi    msg("No renderer for $mode found",-1);
449ac83b9d8Sandi    return null;
450ac83b9d8Sandi  }
451ac83b9d8Sandi
452ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
453ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
4546b7b33dcShfuecks  if ( !class_exists($rclass) ) {
455c19c9173SBen Coburn    trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
456c19c9173SBen Coburn    msg("Renderer for $mode not valid",-1);
457c19c9173SBen Coburn    return null;
4586b7b33dcShfuecks  }
459ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
460c112d578Sandi
461c112d578Sandi  $Renderer->smileys = getSmileys();
462c112d578Sandi  $Renderer->entities = getEntities();
463c112d578Sandi  $Renderer->acronyms = getAcronyms();
464c112d578Sandi  $Renderer->interwiki = getInterwiki();
465c112d578Sandi  #$Renderer->badwords = getBadWords();
466c112d578Sandi
467c112d578Sandi  // Loop through the instructions
468c112d578Sandi  foreach ( $instructions as $instruction ) {
469c112d578Sandi      // Execute the callback against the Renderer
470c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
471c112d578Sandi  }
4729dc2c2afSandi
4739dc2c2afSandi  //set info array
4749dc2c2afSandi  $info = $Renderer->info;
4759dc2c2afSandi
476677844afSchris  // Post process and return the output
477677844afSchris  $data = array($mode,& $Renderer->doc);
478677844afSchris  trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
479c112d578Sandi  return $Renderer->doc;
480c112d578Sandi}
481c112d578Sandi
482bb0a59d4Sjan/**
483bb0a59d4Sjan * Gets the first heading from a file
484bb0a59d4Sjan *
48595dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
486bb0a59d4Sjan */
487bb0a59d4Sjanfunction p_get_first_heading($id){
488796bafb3SAndreas Gohr  global $conf;
489796bafb3SAndreas Gohr  if(!$conf['useheading']) return null;
490796bafb3SAndreas Gohr
49195dbfe57SAndreas Gohr  $meta = p_get_metadata($id);
49295dbfe57SAndreas Gohr  if($meta['title']) return $meta['title'];
493796bafb3SAndreas Gohr  return null;
494bb0a59d4Sjan}
495bb0a59d4Sjan
4968f7d700cSchris/**
4978f7d700cSchris * Wrapper for GeSHi Code Highlighter, provides caching of its output
4988f7d700cSchris *
4998f7d700cSchris * @author Christopher Smith <chris@jalakai.co.uk>
5008f7d700cSchris */
5018f7d700cSchrisfunction p_xhtml_cached_geshi($code, $language) {
5028f7d700cSchris  $cache = getCacheName($language.$code,".code");
5038f7d700cSchris
504*d978e24cSAndreas Gohr  if (@file_exists($cache) && !$_REQUEST['purge']) {
5058f7d700cSchris
5068f7d700cSchris    $highlighted_code = io_readFile($cache, false);
507852896daSAndreas Gohr    @touch($cache);
5088f7d700cSchris
5098f7d700cSchris  } else {
5108f7d700cSchris
5118f7d700cSchris    require_once(DOKU_INC . 'inc/geshi.php');
5128f7d700cSchris
5138f7d700cSchris    $geshi = new GeSHi($code, strtolower($language), DOKU_INC . 'inc/geshi');
5148f7d700cSchris    $geshi->set_encoding('utf-8');
5158f7d700cSchris    $geshi->enable_classes();
5168f7d700cSchris    $geshi->set_header_type(GESHI_HEADER_PRE);
5178f7d700cSchris    $geshi->set_overall_class("code $language");
5188f7d700cSchris    $geshi->set_link_target($conf['target']['extern']);
5198f7d700cSchris
5208f7d700cSchris    $highlighted_code = $geshi->parse_code();
5218f7d700cSchris
5228f7d700cSchris    io_saveFile($cache,$highlighted_code);
5238f7d700cSchris  }
5248f7d700cSchris
5258f7d700cSchris  return $highlighted_code;
5268f7d700cSchris}
5278f7d700cSchris
528c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
529