xref: /dokuwiki/inc/parser/metadata.php (revision ce6b63d97068e71369bad95e7959d0110717bbfd)
139a89382SEsther Brunner<?php
239a89382SEsther Brunner/**
339a89382SEsther Brunner * Renderer for metadata
439a89382SEsther Brunner *
539a89382SEsther Brunner * @author Esther Brunner <wikidesign@gmail.com>
639a89382SEsther Brunner */
739a89382SEsther Brunner
839a89382SEsther Brunnerif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
939a89382SEsther Brunner
1039a89382SEsther Brunnerif ( !defined('DOKU_LF') ) {
1139a89382SEsther Brunner    // Some whitespace to help View > Source
1239a89382SEsther Brunner    define ('DOKU_LF',"\n");
1339a89382SEsther Brunner}
1439a89382SEsther Brunner
1539a89382SEsther Brunnerif ( !defined('DOKU_TAB') ) {
1639a89382SEsther Brunner    // Some whitespace to help View > Source
1739a89382SEsther Brunner    define ('DOKU_TAB',"\t");
1839a89382SEsther Brunner}
1939a89382SEsther Brunner
2039a89382SEsther Brunnerrequire_once DOKU_INC . 'inc/parser/renderer.php';
2139a89382SEsther Brunner
2239a89382SEsther Brunner/**
2339a89382SEsther Brunner * The Renderer
2439a89382SEsther Brunner */
2539a89382SEsther Brunnerclass Doku_Renderer_metadata extends Doku_Renderer {
2639a89382SEsther Brunner
2739a89382SEsther Brunner  var $doc  = '';
2839a89382SEsther Brunner  var $meta = array();
2939a89382SEsther Brunner
3039a89382SEsther Brunner  var $headers = array();
3139a89382SEsther Brunner  var $capture = true;
3239a89382SEsther Brunner  var $store   = '';
3339a89382SEsther Brunner
3439a89382SEsther Brunner  function document_start(){
3539a89382SEsther Brunner    //reset some variables
364aca4bf5SAndreas Gohr    $this->meta['title'] = '';
3739a89382SEsther Brunner    $this->meta['description']['abstract'] = '';
3839a89382SEsther Brunner    $this->meta['description']['tableofcontents'] = array();
3939a89382SEsther Brunner    $this->meta['relation']['haspart'] = array();
4039a89382SEsther Brunner    $this->meta['relation']['references'] = array();
4139a89382SEsther Brunner    $this->headers = array();
4239a89382SEsther Brunner  }
4339a89382SEsther Brunner
4439a89382SEsther Brunner  function document_end(){
4539a89382SEsther Brunner    if (!$this->meta['description']['abstract']){
4639a89382SEsther Brunner      // cut off too long abstracts
4739a89382SEsther Brunner      $this->doc = trim($this->doc);
4839a89382SEsther Brunner      if (strlen($this->doc) > 500)
4939a89382SEsther Brunner        $this->doc = substr($this->doc, 0, 500).'…';
5039a89382SEsther Brunner      $this->meta['description']['abstract'] = $this->doc;
5139a89382SEsther Brunner    }
5239a89382SEsther Brunner  }
5339a89382SEsther Brunner
5439a89382SEsther Brunner  function header($text, $level, $pos) {
5539a89382SEsther Brunner    global $conf;
5639a89382SEsther Brunner
57a4a2d4cfSAndreas Gohr    if (!$this->meta['title']) $this->meta['title'] = $text;
58a4a2d4cfSAndreas Gohr
5939a89382SEsther Brunner    // create a unique header id
6039a89382SEsther Brunner    $hid = $this->_headerToLink($text,'true');
6139a89382SEsther Brunner
6239a89382SEsther Brunner    //handle TOC
6339a89382SEsther Brunner    if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
6439a89382SEsther Brunner      // the TOC is one of our standard ul list arrays ;-)
6539a89382SEsther Brunner      $this->meta['description']['tableofcontents'][] = array(
6639a89382SEsther Brunner        'hid'   => $hid,
6739a89382SEsther Brunner        'title' => $text,
6839a89382SEsther Brunner        'type'  => 'ul',
6939a89382SEsther Brunner        'level' => $level-$conf['toptoclevel']+1
7039a89382SEsther Brunner      );
7139a89382SEsther Brunner    }
7239a89382SEsther Brunner
7339a89382SEsther Brunner    // add to summary
7439a89382SEsther Brunner    if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
7539a89382SEsther Brunner  }
7639a89382SEsther Brunner
7739a89382SEsther Brunner  function section_open($level){}
7839a89382SEsther Brunner  function section_close(){}
7939a89382SEsther Brunner
8039a89382SEsther Brunner  function cdata($text){
8139a89382SEsther Brunner    if ($this->capture) $this->doc .= $text;
8239a89382SEsther Brunner  }
8339a89382SEsther Brunner
8439a89382SEsther Brunner  function p_open(){
8539a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF;
8639a89382SEsther Brunner  }
8739a89382SEsther Brunner
8839a89382SEsther Brunner  function p_close(){
8939a89382SEsther Brunner    if ($this->capture){
9039a89382SEsther Brunner      if (strlen($this->doc) > 250) $this->capture = false;
9139a89382SEsther Brunner      else $this->doc .= DOKU_LF;
9239a89382SEsther Brunner    }
9339a89382SEsther Brunner  }
9439a89382SEsther Brunner
9539a89382SEsther Brunner  function linebreak(){
9639a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF;
9739a89382SEsther Brunner  }
9839a89382SEsther Brunner
9939a89382SEsther Brunner  function hr(){
10039a89382SEsther Brunner    if ($this->capture){
10139a89382SEsther Brunner      if (strlen($this->doc) > 250) $this->capture = false;
10239a89382SEsther Brunner      else $this->doc .= DOKU_LF.'----------'.DOKU_LF;
10339a89382SEsther Brunner    }
10439a89382SEsther Brunner  }
10539a89382SEsther Brunner
10639a89382SEsther Brunner  function strong_open(){}
10739a89382SEsther Brunner  function strong_close(){}
10839a89382SEsther Brunner
10939a89382SEsther Brunner  function emphasis_open(){}
11039a89382SEsther Brunner  function emphasis_close(){}
11139a89382SEsther Brunner
11239a89382SEsther Brunner  function underline_open(){}
11339a89382SEsther Brunner  function underline_close(){}
11439a89382SEsther Brunner
11539a89382SEsther Brunner  function monospace_open(){}
11639a89382SEsther Brunner  function monospace_close(){}
11739a89382SEsther Brunner
11839a89382SEsther Brunner  function subscript_open(){}
11939a89382SEsther Brunner  function subscript_close(){}
12039a89382SEsther Brunner
12139a89382SEsther Brunner  function superscript_open(){}
12239a89382SEsther Brunner  function superscript_close(){}
12339a89382SEsther Brunner
12439a89382SEsther Brunner  function deleted_open(){}
12539a89382SEsther Brunner  function deleted_close(){}
12639a89382SEsther Brunner
12739a89382SEsther Brunner  /**
12839a89382SEsther Brunner   * Callback for footnote start syntax
12939a89382SEsther Brunner   *
13039a89382SEsther Brunner   * All following content will go to the footnote instead of
13139a89382SEsther Brunner   * the document. To achieve this the previous rendered content
13239a89382SEsther Brunner   * is moved to $store and $doc is cleared
13339a89382SEsther Brunner   *
13439a89382SEsther Brunner   * @author Andreas Gohr <andi@splitbrain.org>
13539a89382SEsther Brunner   */
13639a89382SEsther Brunner  function footnote_open() {
13739a89382SEsther Brunner    if ($this->capture){
13839a89382SEsther Brunner      // move current content to store and record footnote
13939a89382SEsther Brunner      $this->store = $this->doc;
14039a89382SEsther Brunner      $this->doc   = '';
14139a89382SEsther Brunner    }
14239a89382SEsther Brunner  }
14339a89382SEsther Brunner
14439a89382SEsther Brunner  /**
14539a89382SEsther Brunner   * Callback for footnote end syntax
14639a89382SEsther Brunner   *
14739a89382SEsther Brunner   * All rendered content is moved to the $footnotes array and the old
14839a89382SEsther Brunner   * content is restored from $store again
14939a89382SEsther Brunner   *
15039a89382SEsther Brunner   * @author Andreas Gohr
15139a89382SEsther Brunner   */
15239a89382SEsther Brunner  function footnote_close() {
15339a89382SEsther Brunner    if ($this->capture){
15439a89382SEsther Brunner      // restore old content
15539a89382SEsther Brunner      $this->doc = $this->store;
15639a89382SEsther Brunner      $this->store = '';
15739a89382SEsther Brunner    }
15839a89382SEsther Brunner  }
15939a89382SEsther Brunner
16039a89382SEsther Brunner  function listu_open(){
16139a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF;
16239a89382SEsther Brunner  }
16339a89382SEsther Brunner
16439a89382SEsther Brunner  function listu_close(){
16539a89382SEsther Brunner    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
16639a89382SEsther Brunner  }
16739a89382SEsther Brunner
16839a89382SEsther Brunner  function listo_open(){
16939a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF;
17039a89382SEsther Brunner  }
17139a89382SEsther Brunner
17239a89382SEsther Brunner  function listo_close(){
17339a89382SEsther Brunner    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
17439a89382SEsther Brunner  }
17539a89382SEsther Brunner
17639a89382SEsther Brunner  function listitem_open($level){
17739a89382SEsther Brunner    if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
17839a89382SEsther Brunner  }
17939a89382SEsther Brunner
18039a89382SEsther Brunner  function listitem_close(){
18139a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF;
18239a89382SEsther Brunner  }
18339a89382SEsther Brunner
18439a89382SEsther Brunner  function listcontent_open(){}
18539a89382SEsther Brunner  function listcontent_close(){}
18639a89382SEsther Brunner
18739a89382SEsther Brunner  function unformatted($text){
18839a89382SEsther Brunner    if ($this->capture) $this->doc .= $text;
18939a89382SEsther Brunner  }
19039a89382SEsther Brunner
19139a89382SEsther Brunner  function php($text){}
19239a89382SEsther Brunner
19339a89382SEsther Brunner  function html($text){}
19439a89382SEsther Brunner
19539a89382SEsther Brunner  function preformatted($text){
19639a89382SEsther Brunner    if ($this->capture) $this->doc .= $text;
19739a89382SEsther Brunner  }
19839a89382SEsther Brunner
19939a89382SEsther Brunner  function file($text){
20039a89382SEsther Brunner    if ($this->capture){
20139a89382SEsther Brunner      $this->doc .= DOKU_LF.$text;
20239a89382SEsther Brunner      if (strlen($this->doc) > 250) $this->capture = false;
20339a89382SEsther Brunner      else $this->doc .= DOKU_LF;
20439a89382SEsther Brunner    }
20539a89382SEsther Brunner  }
20639a89382SEsther Brunner
20739a89382SEsther Brunner  function quote_open(){
20839a89382SEsther Brunner    if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'“';
20939a89382SEsther Brunner  }
21039a89382SEsther Brunner
21139a89382SEsther Brunner  function quote_close(){
21239a89382SEsther Brunner    if ($this->capture){
21339a89382SEsther Brunner      $this->doc .= '”';
21439a89382SEsther Brunner      if (strlen($this->doc) > 250) $this->capture = false;
21539a89382SEsther Brunner      else $this->doc .= DOKU_LF;
21639a89382SEsther Brunner    }
21739a89382SEsther Brunner  }
21839a89382SEsther Brunner
21939a89382SEsther Brunner  function code($text, $language = NULL){
22039a89382SEsther Brunner    if ($this->capture){
22139a89382SEsther Brunner      $this->doc .= DOKU_LF.$text;
22239a89382SEsther Brunner      if (strlen($this->doc) > 250) $this->capture = false;
22339a89382SEsther Brunner      else $this->doc .= DOKU_LF;
22439a89382SEsther Brunner    }
22539a89382SEsther Brunner  }
22639a89382SEsther Brunner
22739a89382SEsther Brunner  function acronym($acronym){
22839a89382SEsther Brunner    if ($this->capture) $this->doc .= $acronym;
22939a89382SEsther Brunner  }
23039a89382SEsther Brunner
23139a89382SEsther Brunner  function smiley($smiley){
23239a89382SEsther Brunner    if ($this->capture) $this->doc .= $smiley;
23339a89382SEsther Brunner  }
23439a89382SEsther Brunner
23539a89382SEsther Brunner  function entity($entity){
23639a89382SEsther Brunner    if ($this->capture) $this->doc .= $entity;
23739a89382SEsther Brunner  }
23839a89382SEsther Brunner
23939a89382SEsther Brunner  function multiplyentity($x, $y){
24039a89382SEsther Brunner    if ($this->capture) $this->doc .= $x.'×'.$y;
24139a89382SEsther Brunner  }
24239a89382SEsther Brunner
24339a89382SEsther Brunner  function singlequoteopening(){
24439a89382SEsther Brunner    if ($this->capture) $this->doc .= '‘';
24539a89382SEsther Brunner  }
24639a89382SEsther Brunner
24739a89382SEsther Brunner  function singlequoteclosing(){
24839a89382SEsther Brunner    if ($this->capture) $this->doc .= '’';
24939a89382SEsther Brunner  }
25039a89382SEsther Brunner
25139a89382SEsther Brunner  function doublequoteopening(){
25239a89382SEsther Brunner    if ($this->capture) $this->doc .= '“';
25339a89382SEsther Brunner  }
25439a89382SEsther Brunner
25539a89382SEsther Brunner  function doublequoteclosing(){
25639a89382SEsther Brunner    if ($this->capture) $this->doc .= '”';
25739a89382SEsther Brunner  }
25839a89382SEsther Brunner
25939a89382SEsther Brunner  function camelcaselink($link) {
26039a89382SEsther Brunner    $this->internallink($link, $link);
26139a89382SEsther Brunner  }
26239a89382SEsther Brunner
26339a89382SEsther Brunner  function locallink($hash, $name = NULL){}
26439a89382SEsther Brunner
26539a89382SEsther Brunner  /**
26639a89382SEsther Brunner   * keep track of internal links in $this->meta['relation']['references']
26739a89382SEsther Brunner   */
26839a89382SEsther Brunner  function internallink($id, $name = NULL){
26939a89382SEsther Brunner    global $ID;
27039a89382SEsther Brunner
27139a89382SEsther Brunner    $default = $this->_simpleTitle($id);
27239a89382SEsther Brunner
27339a89382SEsther Brunner    // first resolve and clean up the $id
27439a89382SEsther Brunner    resolve_pageid(getNS($ID), $id, $exists);
2753be6e394Schris    list($page, $hash) = split('#', $id, 2);
27639a89382SEsther Brunner
27739a89382SEsther Brunner    // set metadata
2783be6e394Schris    $this->meta['relation']['references'][$page] = $exists;
27939a89382SEsther Brunner    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
28039a89382SEsther Brunner    // p_set_metadata($id, $data);
28139a89382SEsther Brunner
28239a89382SEsther Brunner    // add link title to summary
28339a89382SEsther Brunner    if ($this->capture){
28439a89382SEsther Brunner      $name = $this->_getLinkTitle($name, $default, $id);
28539a89382SEsther Brunner      $this->doc .= $name;
28639a89382SEsther Brunner    }
28739a89382SEsther Brunner  }
28839a89382SEsther Brunner
28939a89382SEsther Brunner  function externallink($url, $name = NULL){
29039a89382SEsther Brunner    if ($this->capture){
29139a89382SEsther Brunner      if ($name) $this->doc .= $name;
29239a89382SEsther Brunner      else $this->doc .= '<'.$url.'>';
29339a89382SEsther Brunner    }
29439a89382SEsther Brunner  }
29539a89382SEsther Brunner
29639a89382SEsther Brunner  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
29739a89382SEsther Brunner    if ($this->capture){
29839a89382SEsther Brunner      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
29939a89382SEsther Brunner      $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
30039a89382SEsther Brunner      $this->doc .= $name;
30139a89382SEsther Brunner    }
30239a89382SEsther Brunner  }
30339a89382SEsther Brunner
30439a89382SEsther Brunner  function windowssharelink($url, $name = NULL){
30539a89382SEsther Brunner    if ($this->capture){
30639a89382SEsther Brunner      if ($name) $this->doc .= $name;
30739a89382SEsther Brunner      else $this->doc .= '<'.$url.'>';
30839a89382SEsther Brunner    }
30939a89382SEsther Brunner  }
31039a89382SEsther Brunner
31139a89382SEsther Brunner  function emaillink($address, $name = NULL){
31239a89382SEsther Brunner    if ($this->capture){
31339a89382SEsther Brunner      if ($name) $this->doc .= $name;
31439a89382SEsther Brunner      else $this->doc .= '<'.$address.'>';
31539a89382SEsther Brunner    }
31639a89382SEsther Brunner  }
31739a89382SEsther Brunner
31839a89382SEsther Brunner  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
31939a89382SEsther Brunner                         $height=NULL, $cache=NULL, $linking=NULL){
32039a89382SEsther Brunner    if ($this->capture && $title) $this->doc .= '['.$title.']';
32139a89382SEsther Brunner  }
32239a89382SEsther Brunner
32339a89382SEsther Brunner  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
32439a89382SEsther Brunner                         $height=NULL, $cache=NULL, $linking=NULL){
32539a89382SEsther Brunner    if ($this->capture && $title) $this->doc .= '['.$title.']';
32639a89382SEsther Brunner  }
32739a89382SEsther Brunner
328*ce6b63d9Schris  function rss($url,$params) {
329*ce6b63d9Schris    global $conf;
330*ce6b63d9Schris
331*ce6b63d9Schris    $this->meta['relation']['haspart'][$url] = true;
332*ce6b63d9Schris    $this->meta['date']['valid']['end'] =
333*ce6b63d9Schris         empty($this->meta['date']['valid']['end']) ?
334*ce6b63d9Schris             time() + $conf['rss_update'] :
335*ce6b63d9Schris             min($this->meta['date']['valid']['end'], time() + $conf['rss_update']);
336*ce6b63d9Schris  }
33739a89382SEsther Brunner
33839a89382SEsther Brunner  function table_open($maxcols = NULL, $numrows = NULL){}
33939a89382SEsther Brunner  function table_close(){}
34039a89382SEsther Brunner
34139a89382SEsther Brunner  function tablerow_open(){}
34239a89382SEsther Brunner  function tablerow_close(){}
34339a89382SEsther Brunner
34439a89382SEsther Brunner  function tableheader_open($colspan = 1, $align = NULL){}
34539a89382SEsther Brunner  function tableheader_close(){}
34639a89382SEsther Brunner
34739a89382SEsther Brunner  function tablecell_open($colspan = 1, $align = NULL){}
34839a89382SEsther Brunner  function tablecell_close(){}
34939a89382SEsther Brunner
35039a89382SEsther Brunner  //----------------------------------------------------------
35139a89382SEsther Brunner  // Utils
35239a89382SEsther Brunner
35339a89382SEsther Brunner  /**
35439a89382SEsther Brunner   * Removes any Namespace from the given name but keeps
35539a89382SEsther Brunner   * casing and special chars
35639a89382SEsther Brunner   *
35739a89382SEsther Brunner   * @author Andreas Gohr <andi@splitbrain.org>
35839a89382SEsther Brunner   */
35939a89382SEsther Brunner  function _simpleTitle($name){
36039a89382SEsther Brunner    global $conf;
36139a89382SEsther Brunner
36239a89382SEsther Brunner    if($conf['useslash']){
36339a89382SEsther Brunner        $nssep = '[:;/]';
36439a89382SEsther Brunner    }else{
36539a89382SEsther Brunner        $nssep = '[:;]';
36639a89382SEsther Brunner    }
36739a89382SEsther Brunner    $name = preg_replace('!.*'.$nssep.'!','',$name);
3683be6e394Schris    //if there is a hash we use the anchor name only
36939a89382SEsther Brunner    $name = preg_replace('!.*#!','',$name);
37039a89382SEsther Brunner    return $name;
37139a89382SEsther Brunner  }
37239a89382SEsther Brunner
37339a89382SEsther Brunner  /**
37439a89382SEsther Brunner   * Creates a linkid from a headline
37539a89382SEsther Brunner   *
37639a89382SEsther Brunner   * @param string  $title   The headline title
37739a89382SEsther Brunner   * @param boolean $create  Create a new unique ID?
37839a89382SEsther Brunner   * @author Andreas Gohr <andi@splitbrain.org>
37939a89382SEsther Brunner   */
38039a89382SEsther Brunner  function _headerToLink($title, $create=false) {
38139a89382SEsther Brunner    $title = str_replace(':','',cleanID($title,true)); //force ASCII
38239a89382SEsther Brunner    $title = ltrim($title,'0123456789._-');
38339a89382SEsther Brunner    if(empty($title)) $title='section';
38439a89382SEsther Brunner
38539a89382SEsther Brunner    if($create){
38639a89382SEsther Brunner      // make sure tiles are unique
38739a89382SEsther Brunner      $num = '';
38839a89382SEsther Brunner      while(in_array($title.$num,$this->headers)){
38939a89382SEsther Brunner        ($num) ? $num++ : $num = 1;
39039a89382SEsther Brunner      }
39139a89382SEsther Brunner      $title = $title.$num;
39239a89382SEsther Brunner      $this->headers[] = $title;
39339a89382SEsther Brunner    }
39439a89382SEsther Brunner
39539a89382SEsther Brunner    return $title;
39639a89382SEsther Brunner  }
39739a89382SEsther Brunner
39839a89382SEsther Brunner  /**
39939a89382SEsther Brunner   * Construct a title and handle images in titles
40039a89382SEsther Brunner   *
40139a89382SEsther Brunner   * @author Harry Fuecks <hfuecks@gmail.com>
40239a89382SEsther Brunner   */
40339a89382SEsther Brunner  function _getLinkTitle($title, $default, $id=NULL) {
40439a89382SEsther Brunner    global $conf;
40539a89382SEsther Brunner
40639a89382SEsther Brunner    $isImage = FALSE;
40739a89382SEsther Brunner    if (is_null($title)){
40839a89382SEsther Brunner      if ($conf['useheading'] && $id){
40939a89382SEsther Brunner        $heading = p_get_first_heading($id);
41039a89382SEsther Brunner        if ($heading) return $heading;
41139a89382SEsther Brunner      }
41239a89382SEsther Brunner      return $default;
41339a89382SEsther Brunner    } else if (is_string($title)){
41439a89382SEsther Brunner      return $title;
41539a89382SEsther Brunner    } else if (is_array($title)){
41639a89382SEsther Brunner      return '['.$title.']';
41739a89382SEsther Brunner    }
41839a89382SEsther Brunner  }
41939a89382SEsther Brunner
42039a89382SEsther Brunner}
42139a89382SEsther Brunner
42239a89382SEsther Brunner//Setup VIM: ex: et ts=4 enc=utf-8 :
423