xref: /dokuwiki/inc/parserutils.php (revision 95dbfe5721704a800c0f108c048ab3cb5e13de3b)
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
626b7b33dcShfuecks * @author Andreas Gohr <hfuecks@gmail.com>
636b7b33dcShfuecks */
646b7b33dcShfuecksfunction p_wiki_xhtml_summary($id, &$title, $rev='', $excuse=true){
656b7b33dcShfuecks  $file = wikiFN($id,$rev);
666b7b33dcShfuecks  $ret  = '';
676b7b33dcShfuecks
686b7b33dcShfuecks  //ensure $id is in global $ID (needed for parsing)
696b7b33dcShfuecks  global $ID;
706b7b33dcShfuecks  $keep = $ID;
716b7b33dcShfuecks  $ID   = $id;
726b7b33dcShfuecks
736b7b33dcShfuecks  if($rev){
746b7b33dcShfuecks    if(@file_exists($file)){
756b7b33dcShfuecks      //no caching on old revisions
766b7b33dcShfuecks      $ins = p_get_instructions(io_readfile($file));
776b7b33dcShfuecks    }elseif($excuse){
786b7b33dcShfuecks      $ret = p_locale_xhtml('norev');
796b7b33dcShfuecks      //restore ID (just in case)
806b7b33dcShfuecks      $ID = $keep;
816b7b33dcShfuecks      return $ret;
826b7b33dcShfuecks    }
836b7b33dcShfuecks
846b7b33dcShfuecks  }else{
856b7b33dcShfuecks
866b7b33dcShfuecks    if(@file_exists($file)){
876b7b33dcShfuecks      // The XHTML for a summary is not cached so use the instruction cache
886b7b33dcShfuecks      $ins = p_cached_instructions($file);
896b7b33dcShfuecks    }elseif($excuse){
906b7b33dcShfuecks      $ret = p_locale_xhtml('newpage');
916b7b33dcShfuecks      //restore ID (just in case)
926b7b33dcShfuecks      $ID = $keep;
936b7b33dcShfuecks      return $ret;
946b7b33dcShfuecks    }
956b7b33dcShfuecks  }
966b7b33dcShfuecks
976b7b33dcShfuecks  $ret = p_render('xhtmlsummary',$ins,$info);
986b7b33dcShfuecks
996b7b33dcShfuecks  if ( $info['sum_pagetitle'] ) {
1006b7b33dcShfuecks    $title = $info['sum_pagetitle'];
1016b7b33dcShfuecks  } else {
1026b7b33dcShfuecks    $title = $id;
1036b7b33dcShfuecks  }
1046b7b33dcShfuecks
1056b7b33dcShfuecks  $ID = $keep;
1066b7b33dcShfuecks  return $ret;
1076b7b33dcShfuecks}
1086b7b33dcShfuecks
1096b7b33dcShfuecks/**
110c112d578Sandi * Returns the specified local text in parsed format
111c112d578Sandi *
112c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
113c112d578Sandi */
114c112d578Sandifunction p_locale_xhtml($id){
115c112d578Sandi  //fetch parsed locale
116c112d578Sandi  $html = p_cached_xhtml(localeFN($id));
117c112d578Sandi  return $html;
118c112d578Sandi}
119c112d578Sandi
120c112d578Sandi/**
121c112d578Sandi * Returns the given file parsed to XHTML
122c112d578Sandi *
123c112d578Sandi * Uses and creates a cachefile
124c112d578Sandi *
125c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
1269dc2c2afSandi * @todo   rewrite to use mode instead of hardcoded XHTML
127c112d578Sandi */
128c112d578Sandifunction p_cached_xhtml($file){
129c112d578Sandi  global $conf;
13098407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.xhtml');
131c7532350SDrew Amato  $purge  = $conf['cachedir'].'/purgefile';
132c112d578Sandi
133c112d578Sandi  // check if cache can be used
134c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
135c112d578Sandi
136c112d578Sandi  if( @file_exists($file)                                             // does the source exist
137c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
138c112d578Sandi      && ((time() - $cachetime) < $conf['cachetime'])                 // and is cachefile young enough
139c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
1401094c798Sandi      && ($cachetime > @filemtime($purge))                            // and newer than the purgefile
141e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
142e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
143c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/xhtml.php'))   // newer than the renderer
144c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
145c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
146c112d578Sandi  {
147c112d578Sandi    //well then use the cache
148c112d578Sandi    $parsed = io_readfile($cache);
149f42d1c75SAndreas Gohr    if($conf['allowdebug']) $parsed .= "\n<!-- cachefile $cache used -->\n";
150c112d578Sandi  }else{
1519dc2c2afSandi    $parsed = p_render('xhtml', p_cached_instructions($file),$info); //try to use cached instructions
152c112d578Sandi
1539dc2c2afSandi    if($info['cache']){
154c112d578Sandi      io_saveFile($cache,$parsed); //save cachefile
155f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, but created -->\n";
156c112d578Sandi    }else{
157c112d578Sandi      @unlink($cache); //try to delete cachefile
158f42d1c75SAndreas Gohr      if($conf['allowdebug']) $parsed .= "\n<!-- no cachefile used, caching forbidden -->\n";
159c112d578Sandi    }
160c112d578Sandi  }
161c112d578Sandi
162c112d578Sandi  return $parsed;
163c112d578Sandi}
164c112d578Sandi
165c112d578Sandi/**
166c112d578Sandi * Returns the render instructions for a file
167c112d578Sandi *
168c112d578Sandi * Uses and creates a serialized cache file
169c112d578Sandi *
170c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
171c112d578Sandi */
17237e34a5eSandifunction p_cached_instructions($file,$cacheonly=false){
173c112d578Sandi  global $conf;
17498407a7aSandi  $cache  = getCacheName($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.i');
175c112d578Sandi
176c112d578Sandi  // check if cache can be used
177c112d578Sandi  $cachetime = @filemtime($cache); // 0 if not exists
178c112d578Sandi
17937e34a5eSandi  // cache forced?
18037e34a5eSandi  if($cacheonly){
18137e34a5eSandi    if($cachetime){
182e34c0709SAndreas Gohr      return unserialize(io_readfile($cache,false));
18337e34a5eSandi    }else{
184fd198316Sandi      return array();
18537e34a5eSandi    }
18637e34a5eSandi  }
18737e34a5eSandi
188c112d578Sandi  if( @file_exists($file)                                             // does the source exist
189c112d578Sandi      && $cachetime > @filemtime($file)                               // cache is fresh
190c112d578Sandi      && !isset($_REQUEST['purge'])                                   // no purge param was set
191e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'dokuwiki.php'))      // newer than the config file
192e7cb32dcSAndreas Gohr      && ($cachetime > @filemtime(DOKU_CONF.'local.php'))         // newer than the local config file
193c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/parser.php'))  // newer than the parser
194c112d578Sandi      && ($cachetime > @filemtime(DOKU_INC.'inc/parser/handler.php')))// newer than the handler
195c112d578Sandi  {
196c112d578Sandi    //well then use the cache
197e34c0709SAndreas Gohr    return unserialize(io_readfile($cache,false));
198c112d578Sandi  }elseif(@file_exists($file)){
199c112d578Sandi    // no cache - do some work
2006bbae538Sandi    $ins = p_get_instructions(io_readfile($file));
201c112d578Sandi    io_savefile($cache,serialize($ins));
202c112d578Sandi    return $ins;
203c112d578Sandi  }
204c112d578Sandi
205c112d578Sandi  return NULL;
206c112d578Sandi}
207c112d578Sandi
208c112d578Sandi/**
209c112d578Sandi * turns a page into a list of instructions
210c112d578Sandi *
211c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
212c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
213c112d578Sandi */
2146bbae538Sandifunction p_get_instructions($text){
215c112d578Sandi
216107b01d6Sandi  $modes = p_get_parsermodes();
217ee20e7d1Sandi
218c112d578Sandi  // Create the parser
219c112d578Sandi  $Parser = & new Doku_Parser();
220c112d578Sandi
221c112d578Sandi  // Add the Handler
222c112d578Sandi  $Parser->Handler = & new Doku_Handler();
223c112d578Sandi
224107b01d6Sandi  //add modes to parser
225107b01d6Sandi  foreach($modes as $mode){
226107b01d6Sandi    $Parser->addMode($mode['mode'],$mode['obj']);
227c112d578Sandi  }
228c112d578Sandi
229c112d578Sandi  // Do the parsing
230a2d649c4Sandi  $p    = $Parser->parse($text);
231ee20e7d1Sandi//  dbg($p);
232a2d649c4Sandi  return $p;
233c112d578Sandi}
234c112d578Sandi
235c112d578Sandi/**
23639a89382SEsther Brunner * returns the metadata of a page
23739a89382SEsther Brunner *
23839a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
23939a89382SEsther Brunner */
24039a89382SEsther Brunnerfunction p_get_metadata($id, $key=false, $render=false){
24139a89382SEsther Brunner  $file = metaFN($id, '.meta');
24239a89382SEsther Brunner
24339a89382SEsther Brunner  if (@file_exists($file)) $meta = unserialize(io_readFile($file, false));
24439a89382SEsther Brunner  else $meta = array();
24539a89382SEsther Brunner
24639a89382SEsther Brunner  // metadata has never been rendered before - do it!
24739a89382SEsther Brunner  if ($render && !$meta['description']['abstract']){
24839a89382SEsther Brunner    $meta = p_render_metadata($id, $meta);
24939a89382SEsther Brunner    io_saveFile($file, serialize($meta));
25039a89382SEsther Brunner  }
25139a89382SEsther Brunner
25239a89382SEsther Brunner  // filter by $key
25339a89382SEsther Brunner  if ($key){
25439a89382SEsther Brunner    list($key, $subkey) = explode(' ', $key, 2);
25539a89382SEsther Brunner    if (trim($subkey)) return $meta[$key][$subkey];
25639a89382SEsther Brunner    else return $meta[$key];
25739a89382SEsther Brunner  }
25839a89382SEsther Brunner
25939a89382SEsther Brunner  return $meta;
26039a89382SEsther Brunner}
26139a89382SEsther Brunner
26239a89382SEsther Brunner/**
26339a89382SEsther Brunner * sets metadata elements of a page
26439a89382SEsther Brunner *
26539a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
26639a89382SEsther Brunner */
26739a89382SEsther Brunnerfunction p_set_metadata($id, $data, $render=false){
26839a89382SEsther Brunner  if (!is_array($data)) return false;
26939a89382SEsther Brunner
27039a89382SEsther Brunner  $orig = p_get_metadata($id);
27139a89382SEsther Brunner
27239a89382SEsther Brunner  // render metadata first?
27339a89382SEsther Brunner  if ($render) $meta = p_render_metadata($id, $orig);
27439a89382SEsther Brunner  else $meta = $orig;
27539a89382SEsther Brunner
27639a89382SEsther Brunner  // now add the passed metadata
27739a89382SEsther Brunner  $protected = array('description', 'date', 'contributor');
27839a89382SEsther Brunner  foreach ($data as $key => $value){
27939a89382SEsther Brunner
28039a89382SEsther Brunner    // be careful with sub-arrays of $meta['relation']
28139a89382SEsther Brunner    if ($key == 'relation'){
28239a89382SEsther Brunner      foreach ($value as $subkey => $subvalue){
28339a89382SEsther Brunner        $meta[$key][$subkey] = array_merge($meta[$key][$subkey], $subvalue);
28439a89382SEsther Brunner      }
28539a89382SEsther Brunner
28639a89382SEsther Brunner    // be careful with some senisitive arrays of $meta
28739a89382SEsther Brunner    } elseif (in_array($key, $protected)){
28839a89382SEsther Brunner      if (is_array($value)){
289*95dbfe57SAndreas Gohr        #FIXME not sure if this is the intended thing:
290*95dbfe57SAndreas Gohr        if(!is_array($meta[$key])) $meta[$key] = array($meta[$key]);
29139a89382SEsther Brunner        $meta[$key] = array_merge($meta[$key], $value);
29239a89382SEsther Brunner      }
29339a89382SEsther Brunner
29439a89382SEsther Brunner    // no special treatment for the rest
29539a89382SEsther Brunner    } else {
29639a89382SEsther Brunner      $meta[$key] = $value;
29739a89382SEsther Brunner    }
29839a89382SEsther Brunner  }
29939a89382SEsther Brunner
30039a89382SEsther Brunner  // save only if metadata changed
30139a89382SEsther Brunner  if ($meta == $orig) return true;
30239a89382SEsther Brunner  return io_saveFile(metaFN($id, '.meta'), serialize($meta));
30339a89382SEsther Brunner}
30439a89382SEsther Brunner
30539a89382SEsther Brunner/**
30639a89382SEsther Brunner * renders the metadata of a page
30739a89382SEsther Brunner *
30839a89382SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
30939a89382SEsther Brunner */
31039a89382SEsther Brunnerfunction p_render_metadata($id, $orig){
31139a89382SEsther Brunner  require_once DOKU_INC."inc/parser/metadata.php";
31239a89382SEsther Brunner
31339a89382SEsther Brunner  // get instructions
31439a89382SEsther Brunner  $instructions = p_cached_instructions(wikiFN($id));
31539a89382SEsther Brunner
31639a89382SEsther Brunner  // set up the renderer
31739a89382SEsther Brunner  $renderer = & new Doku_Renderer_metadata();
31839a89382SEsther Brunner  $renderer->meta = $orig;
31939a89382SEsther Brunner
32039a89382SEsther Brunner  // loop through the instructions
32139a89382SEsther Brunner  foreach ($instructions as $instruction){
32239a89382SEsther Brunner    // execute the callback against the renderer
32339a89382SEsther Brunner    call_user_func_array(array(&$renderer, $instruction[0]), $instruction[1]);
32439a89382SEsther Brunner  }
32539a89382SEsther Brunner
32639a89382SEsther Brunner  return $renderer->meta;
32739a89382SEsther Brunner}
32839a89382SEsther Brunner
32939a89382SEsther Brunner/**
330107b01d6Sandi * returns all available parser syntax modes in correct order
331107b01d6Sandi *
332107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
333107b01d6Sandi */
334107b01d6Sandifunction p_get_parsermodes(){
335107b01d6Sandi  global $conf;
336107b01d6Sandi
337107b01d6Sandi  //reuse old data
338107b01d6Sandi  static $modes = null;
339107b01d6Sandi  if($modes != null){
340107b01d6Sandi    return $modes;
341107b01d6Sandi  }
342107b01d6Sandi
343107b01d6Sandi  //import parser classes and mode definitions
344107b01d6Sandi  require_once DOKU_INC . 'inc/parser/parser.php';
345107b01d6Sandi
346107b01d6Sandi  // we now collect all syntax modes and their objects, then they will
347107b01d6Sandi  // be sorted and added to the parser in correct order
348107b01d6Sandi  $modes = array();
349107b01d6Sandi
350107b01d6Sandi  // add syntax plugins
351107b01d6Sandi  $pluginlist = plugin_list('syntax');
352107b01d6Sandi  if(count($pluginlist)){
353107b01d6Sandi    global $PARSER_MODES;
354107b01d6Sandi    $obj = null;
355107b01d6Sandi    foreach($pluginlist as $p){
356c90b2fb1Schris      if(!$obj =& plugin_load('syntax',$p)) continue; //attempt to load plugin into $obj
357107b01d6Sandi      $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
358107b01d6Sandi      //add to modes
359107b01d6Sandi      $modes[] = array(
360107b01d6Sandi                   'sort' => $obj->getSort(),
361107b01d6Sandi                   'mode' => "plugin_$p",
362107b01d6Sandi                   'obj'  => $obj,
363107b01d6Sandi                 );
364a46d0d65SAndreas Gohr      unset($obj); //remove the reference
365107b01d6Sandi    }
366107b01d6Sandi  }
367107b01d6Sandi
368107b01d6Sandi  // add default modes
369107b01d6Sandi  $std_modes = array('listblock','preformatted','notoc','nocache',
370107b01d6Sandi                     'header','table','linebreak','footnote','hr',
371107b01d6Sandi                     'unformatted','php','html','code','file','quote',
372e77ea1bcSAndreas Gohr                     'internallink','rss','media','externallink',
373e77ea1bcSAndreas Gohr                     'emaillink','windowssharelink','eol');
374e77ea1bcSAndreas Gohr  if($conf['typography']){
375e77ea1bcSAndreas Gohr    $std_modes[] = 'quotes';
376e77ea1bcSAndreas Gohr    $std_modes[] = 'multiplyentity';
377e77ea1bcSAndreas Gohr  }
378107b01d6Sandi  foreach($std_modes as $m){
379107b01d6Sandi    $class = "Doku_Parser_Mode_$m";
380107b01d6Sandi    $obj   = new $class();
381107b01d6Sandi    $modes[] = array(
382107b01d6Sandi                 'sort' => $obj->getSort(),
383107b01d6Sandi                 'mode' => $m,
384107b01d6Sandi                 'obj'  => $obj
385107b01d6Sandi               );
386107b01d6Sandi  }
387107b01d6Sandi
388107b01d6Sandi  // add formatting modes
389107b01d6Sandi  $fmt_modes = array('strong','emphasis','underline','monospace',
390107b01d6Sandi                     'subscript','superscript','deleted');
391107b01d6Sandi  foreach($fmt_modes as $m){
392107b01d6Sandi    $obj   = new Doku_Parser_Mode_formatting($m);
393107b01d6Sandi    $modes[] = array(
394107b01d6Sandi                 'sort' => $obj->getSort(),
395107b01d6Sandi                 'mode' => $m,
396107b01d6Sandi                 'obj'  => $obj
397107b01d6Sandi               );
398107b01d6Sandi  }
399107b01d6Sandi
400107b01d6Sandi  // add modes which need files
401107b01d6Sandi  $obj     = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
402107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj'  => $obj );
403107b01d6Sandi  $obj     = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
404107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj'  => $obj );
405107b01d6Sandi  $obj     = new Doku_Parser_Mode_entity(array_keys(getEntities()));
406107b01d6Sandi  $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj'  => $obj );
407107b01d6Sandi
408107b01d6Sandi
409107b01d6Sandi  // add optional camelcase mode
410107b01d6Sandi  if($conf['camelcase']){
411107b01d6Sandi    $obj     = new Doku_Parser_Mode_camelcaselink();
412107b01d6Sandi    $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj'  => $obj );
413107b01d6Sandi  }
414107b01d6Sandi
415107b01d6Sandi  //sort modes
416107b01d6Sandi  usort($modes,'p_sort_modes');
417107b01d6Sandi
418107b01d6Sandi  return $modes;
419107b01d6Sandi}
420107b01d6Sandi
421107b01d6Sandi/**
422107b01d6Sandi * Callback function for usort
423107b01d6Sandi *
424107b01d6Sandi * @author Andreas Gohr <andi@splitbrain.org>
425107b01d6Sandi */
426107b01d6Sandifunction p_sort_modes($a, $b){
427107b01d6Sandi  if($a['sort'] == $b['sort']) return 0;
428107b01d6Sandi  return ($a['sort'] < $b['sort']) ? -1 : 1;
429107b01d6Sandi}
430107b01d6Sandi
431107b01d6Sandi/**
432ac83b9d8Sandi * Renders a list of instruction to the specified output mode
433c112d578Sandi *
4349dc2c2afSandi * In the $info array are informations from the renderer returned
4359dc2c2afSandi *
436c112d578Sandi * @author Harry Fuecks <hfuecks@gmail.com>
437c112d578Sandi * @author Andreas Gohr <andi@splitbrain.org>
438c112d578Sandi */
4399dc2c2afSandifunction p_render($mode,$instructions,& $info){
440c112d578Sandi  if(is_null($instructions)) return '';
441c112d578Sandi
442c19c9173SBen Coburn  if ($mode=='wiki') { msg("Renderer for $mode not valid",-1); return null; } //FIXME!! remove this line when inc/parser/wiki.php works.
443c19c9173SBen Coburn
444c112d578Sandi  // Create the renderer
445ac83b9d8Sandi  if(!@file_exists(DOKU_INC."inc/parser/$mode.php")){
446ac83b9d8Sandi    msg("No renderer for $mode found",-1);
447ac83b9d8Sandi    return null;
448ac83b9d8Sandi  }
449ac83b9d8Sandi
450ac83b9d8Sandi  require_once DOKU_INC."inc/parser/$mode.php";
451ac83b9d8Sandi  $rclass = "Doku_Renderer_$mode";
4526b7b33dcShfuecks  if ( !class_exists($rclass) ) {
453c19c9173SBen Coburn    trigger_error("Unable to resolve render class $rclass",E_USER_WARNING);
454c19c9173SBen Coburn    msg("Renderer for $mode not valid",-1);
455c19c9173SBen Coburn    return null;
4566b7b33dcShfuecks  }
457ac83b9d8Sandi  $Renderer = & new $rclass(); #FIXME any way to check for class existance?
458c112d578Sandi
459c112d578Sandi  $Renderer->smileys = getSmileys();
460c112d578Sandi  $Renderer->entities = getEntities();
461c112d578Sandi  $Renderer->acronyms = getAcronyms();
462c112d578Sandi  $Renderer->interwiki = getInterwiki();
463c112d578Sandi  #$Renderer->badwords = getBadWords();
464c112d578Sandi
465c112d578Sandi  // Loop through the instructions
466c112d578Sandi  foreach ( $instructions as $instruction ) {
467c112d578Sandi      // Execute the callback against the Renderer
468c112d578Sandi      call_user_func_array(array(&$Renderer, $instruction[0]),$instruction[1]);
469c112d578Sandi  }
4709dc2c2afSandi
4719dc2c2afSandi  //set info array
4729dc2c2afSandi  $info = $Renderer->info;
4739dc2c2afSandi
474c112d578Sandi  // Return the output
475c112d578Sandi  return $Renderer->doc;
476c112d578Sandi}
477c112d578Sandi
478bb0a59d4Sjan/**
479bb0a59d4Sjan * Gets the first heading from a file
480bb0a59d4Sjan *
481*95dbfe57SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
482bb0a59d4Sjan */
483bb0a59d4Sjanfunction p_get_first_heading($id){
484*95dbfe57SAndreas Gohr  $meta = p_get_metadata($id);
485*95dbfe57SAndreas Gohr  if($meta['title']) return $meta['title'];
486bb0a59d4Sjan  return NULL;
487bb0a59d4Sjan}
488bb0a59d4Sjan
489c112d578Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
490