xref: /dokuwiki/inc/parser/metadata.php (revision ca47e84675e9106c6e64fa7af42fddaf91ea54fa)
1<?php
2/**
3 * Renderer for metadata
4 *
5 * @author Esther Brunner <wikidesign@gmail.com>
6 */
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
9
10if ( !defined('DOKU_LF') ) {
11    // Some whitespace to help View > Source
12    define ('DOKU_LF',"\n");
13}
14
15if ( !defined('DOKU_TAB') ) {
16    // Some whitespace to help View > Source
17    define ('DOKU_TAB',"\t");
18}
19
20require_once DOKU_INC . 'inc/parser/renderer.php';
21
22/**
23 * The Renderer
24 */
25class Doku_Renderer_metadata extends Doku_Renderer {
26
27  var $doc  = '';
28  var $meta = array();
29  var $persistent = array();
30
31  var $headers = array();
32  var $capture = true;
33  var $store   = '';
34
35  function getFormat(){
36    return 'metadata';
37  }
38
39  function document_start(){
40    // reset metadata to persistent values
41    $this->meta = $this->persistent;
42  }
43
44  function document_end(){
45    if (!$this->meta['description']['abstract']){
46      // cut off too long abstracts
47      $this->doc = trim($this->doc);
48      if (strlen($this->doc) > 500)
49        $this->doc = substr($this->doc, 0, 500).'…';
50      $this->meta['description']['abstract'] = $this->doc;
51    }
52  }
53
54  function toc_additem($id, $text, $level) {
55    global $conf;
56
57    //only add items within configured levels
58    if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
59      // the TOC is one of our standard ul list arrays ;-)
60      $this->meta['description']['tableofcontents'][] = array(
61        'hid'   => $id,
62        'title' => $text,
63        'type'  => 'ul',
64        'level' => $level-$conf['toptoclevel']+1
65      );
66    }
67
68  }
69
70  function header($text, $level, $pos) {
71
72    if (!$this->meta['title']) $this->meta['title'] = $text;
73
74    // add the header to the TOC
75    $hid = $this->_headerToLink($text,'true');
76    $this->toc_additem($hid, $text, $level);
77
78    // add to summary
79    if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
80  }
81
82  function section_open($level){}
83  function section_close(){}
84
85  function cdata($text){
86    if ($this->capture) $this->doc .= $text;
87  }
88
89  function p_open(){
90    if ($this->capture) $this->doc .= DOKU_LF;
91  }
92
93  function p_close(){
94    if ($this->capture){
95      if (strlen($this->doc) > 250) $this->capture = false;
96      else $this->doc .= DOKU_LF;
97    }
98  }
99
100  function linebreak(){
101    if ($this->capture) $this->doc .= DOKU_LF;
102  }
103
104  function hr(){
105    if ($this->capture){
106      if (strlen($this->doc) > 250) $this->capture = false;
107      else $this->doc .= DOKU_LF.'----------'.DOKU_LF;
108    }
109  }
110
111  function strong_open(){}
112  function strong_close(){}
113
114  function emphasis_open(){}
115  function emphasis_close(){}
116
117  function underline_open(){}
118  function underline_close(){}
119
120  function monospace_open(){}
121  function monospace_close(){}
122
123  function subscript_open(){}
124  function subscript_close(){}
125
126  function superscript_open(){}
127  function superscript_close(){}
128
129  function deleted_open(){}
130  function deleted_close(){}
131
132  /**
133   * Callback for footnote start syntax
134   *
135   * All following content will go to the footnote instead of
136   * the document. To achieve this the previous rendered content
137   * is moved to $store and $doc is cleared
138   *
139   * @author Andreas Gohr <andi@splitbrain.org>
140   */
141  function footnote_open() {
142    if ($this->capture){
143      // move current content to store and record footnote
144      $this->store = $this->doc;
145      $this->doc   = '';
146    }
147  }
148
149  /**
150   * Callback for footnote end syntax
151   *
152   * All rendered content is moved to the $footnotes array and the old
153   * content is restored from $store again
154   *
155   * @author Andreas Gohr
156   */
157  function footnote_close() {
158    if ($this->capture){
159      // restore old content
160      $this->doc = $this->store;
161      $this->store = '';
162    }
163  }
164
165  function listu_open(){
166    if ($this->capture) $this->doc .= DOKU_LF;
167  }
168
169  function listu_close(){
170    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
171  }
172
173  function listo_open(){
174    if ($this->capture) $this->doc .= DOKU_LF;
175  }
176
177  function listo_close(){
178    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
179  }
180
181  function listitem_open($level){
182    if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
183  }
184
185  function listitem_close(){
186    if ($this->capture) $this->doc .= DOKU_LF;
187  }
188
189  function listcontent_open(){}
190  function listcontent_close(){}
191
192  function unformatted($text){
193    if ($this->capture) $this->doc .= $text;
194  }
195
196  function php($text){}
197
198  function html($text){}
199
200  function preformatted($text){
201    if ($this->capture) $this->doc .= $text;
202  }
203
204  function file($text){
205    if ($this->capture){
206      $this->doc .= DOKU_LF.$text;
207      if (strlen($this->doc) > 250) $this->capture = false;
208      else $this->doc .= DOKU_LF;
209    }
210  }
211
212  function quote_open(){
213    if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"';
214  }
215
216  function quote_close(){
217    if ($this->capture){
218      $this->doc .= '"';
219      if (strlen($this->doc) > 250) $this->capture = false;
220      else $this->doc .= DOKU_LF;
221    }
222  }
223
224  function code($text, $language = NULL){
225    if ($this->capture){
226      $this->doc .= DOKU_LF.$text;
227      if (strlen($this->doc) > 250) $this->capture = false;
228      else $this->doc .= DOKU_LF;
229    }
230  }
231
232  function acronym($acronym){
233    if ($this->capture) $this->doc .= $acronym;
234  }
235
236  function smiley($smiley){
237    if ($this->capture) $this->doc .= $smiley;
238  }
239
240  function entity($entity){
241    if ($this->capture) $this->doc .= $entity;
242  }
243
244  function multiplyentity($x, $y){
245    if ($this->capture) $this->doc .= $x.'×'.$y;
246  }
247
248  function singlequoteopening(){
249    global $lang;
250    if ($this->capture) $this->doc .= $lang['singlequoteopening'];
251  }
252
253  function singlequoteclosing(){
254    global $lang;
255    if ($this->capture) $this->doc .= $lang['singlequoteclosing'];
256  }
257
258  function doublequoteopening(){
259    global $lang;
260    if ($this->capture) $this->doc .= $lang['doublequoteopening'];
261  }
262
263  function doublequoteclosing(){
264    global $lang;
265    if ($this->capture) $this->doc .= $lang['doublequoteclosing'];
266  }
267
268  function camelcaselink($link) {
269    $this->internallink($link, $link);
270  }
271
272  function locallink($hash, $name = NULL){}
273
274  /**
275   * keep track of internal links in $this->meta['relation']['references']
276   */
277  function internallink($id, $name = NULL){
278    global $ID;
279
280    $default = $this->_simpleTitle($id);
281
282    // first resolve and clean up the $id
283    resolve_pageid(getNS($ID), $id, $exists);
284    list($page, $hash) = split('#', $id, 2);
285
286    // set metadata
287    $this->meta['relation']['references'][$page] = $exists;
288    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
289    // p_set_metadata($id, $data);
290
291    // add link title to summary
292    if ($this->capture){
293      $name = $this->_getLinkTitle($name, $default, $id);
294      $this->doc .= $name;
295    }
296  }
297
298  function externallink($url, $name = NULL){
299    if ($this->capture){
300      if ($name) $this->doc .= $name;
301      else $this->doc .= '<'.$url.'>';
302    }
303  }
304
305  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
306    if ($this->capture){
307      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
308      $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
309      $this->doc .= $name;
310    }
311  }
312
313  function windowssharelink($url, $name = NULL){
314    if ($this->capture){
315      if ($name) $this->doc .= $name;
316      else $this->doc .= '<'.$url.'>';
317    }
318  }
319
320  function emaillink($address, $name = NULL){
321    if ($this->capture){
322      if ($name) $this->doc .= $name;
323      else $this->doc .= '<'.$address.'>';
324    }
325  }
326
327  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
328                         $height=NULL, $cache=NULL, $linking=NULL){
329    if ($this->capture && $title) $this->doc .= '['.$title.']';
330  }
331
332  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
333                         $height=NULL, $cache=NULL, $linking=NULL){
334    if ($this->capture && $title) $this->doc .= '['.$title.']';
335  }
336
337  function rss($url,$params) {
338    $this->meta['relation']['haspart'][$url] = true;
339
340    $this->meta['date']['valid']['age'] =
341            isset($this->meta['date']['valid']['age']) ?
342                min($this->meta['date']['valid']['age'],$params['refresh']) :
343                $params['refresh'];
344  }
345
346  function table_open($maxcols = NULL, $numrows = NULL){}
347  function table_close(){}
348
349  function tablerow_open(){}
350  function tablerow_close(){}
351
352  function tableheader_open($colspan = 1, $align = NULL){}
353  function tableheader_close(){}
354
355  function tablecell_open($colspan = 1, $align = NULL){}
356  function tablecell_close(){}
357
358  //----------------------------------------------------------
359  // Utils
360
361  /**
362   * Removes any Namespace from the given name but keeps
363   * casing and special chars
364   *
365   * @author Andreas Gohr <andi@splitbrain.org>
366   */
367  function _simpleTitle($name){
368    global $conf;
369
370    if(is_array($name)) return '';
371
372    if($conf['useslash']){
373        $nssep = '[:;/]';
374    }else{
375        $nssep = '[:;]';
376    }
377    $name = preg_replace('!.*'.$nssep.'!','',$name);
378    //if there is a hash we use the anchor name only
379    $name = preg_replace('!.*#!','',$name);
380    return $name;
381  }
382
383  /**
384   * Creates a linkid from a headline
385   *
386   * @param string  $title   The headline title
387   * @param boolean $create  Create a new unique ID?
388   * @author Andreas Gohr <andi@splitbrain.org>
389   */
390  function _headerToLink($title, $create=false) {
391    $title = str_replace(':','',cleanID($title));
392    $title = ltrim($title,'0123456789._-');
393    if(empty($title)) $title='section';
394
395    if($create){
396      // make sure tiles are unique
397      $num = '';
398      while(in_array($title.$num,$this->headers)){
399        ($num) ? $num++ : $num = 1;
400      }
401      $title = $title.$num;
402      $this->headers[] = $title;
403    }
404
405    return $title;
406  }
407
408  /**
409   * Construct a title and handle images in titles
410   *
411   * @author Harry Fuecks <hfuecks@gmail.com>
412   */
413  function _getLinkTitle($title, $default, $id=NULL) {
414    global $conf;
415
416    $isImage = false;
417    if (is_null($title)){
418      if ($conf['useheading'] && $id){
419        $heading = p_get_first_heading($id,false);
420        if ($heading) return $heading;
421      }
422      return $default;
423    } else if (is_string($title)){
424      return $title;
425    } else if (is_array($title)){
426      return '['.$title.']';
427    }
428  }
429
430}
431
432//Setup VIM: ex: et ts=4 enc=utf-8 :
433