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