xref: /dokuwiki/inc/parser/metadata.php (revision cde6a01b90b199cd9d42a8e23a61d467992b6d67)
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    if ($this->capture) $this->doc .= '‘';
246  }
247
248  function singlequoteclosing(){
249    if ($this->capture) $this->doc .= '’';
250  }
251
252  function doublequoteopening(){
253    if ($this->capture) $this->doc .= '“';
254  }
255
256  function doublequoteclosing(){
257    if ($this->capture) $this->doc .= '”';
258  }
259
260  function camelcaselink($link) {
261    $this->internallink($link, $link);
262  }
263
264  function locallink($hash, $name = NULL){}
265
266  /**
267   * keep track of internal links in $this->meta['relation']['references']
268   */
269  function internallink($id, $name = NULL){
270    global $ID;
271
272    $default = $this->_simpleTitle($id);
273
274    // first resolve and clean up the $id
275    resolve_pageid(getNS($ID), $id, $exists);
276    list($page, $hash) = split('#', $id, 2);
277
278    // set metadata
279    $this->meta['relation']['references'][$page] = $exists;
280    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
281    // p_set_metadata($id, $data);
282
283    // add link title to summary
284    if ($this->capture){
285      $name = $this->_getLinkTitle($name, $default, $id);
286      $this->doc .= $name;
287    }
288  }
289
290  function externallink($url, $name = NULL){
291    if ($this->capture){
292      if ($name) $this->doc .= $name;
293      else $this->doc .= '<'.$url.'>';
294    }
295  }
296
297  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
298    if ($this->capture){
299      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
300      $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
301      $this->doc .= $name;
302    }
303  }
304
305  function windowssharelink($url, $name = NULL){
306    if ($this->capture){
307      if ($name) $this->doc .= $name;
308      else $this->doc .= '<'.$url.'>';
309    }
310  }
311
312  function emaillink($address, $name = NULL){
313    if ($this->capture){
314      if ($name) $this->doc .= $name;
315      else $this->doc .= '<'.$address.'>';
316    }
317  }
318
319  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
320                         $height=NULL, $cache=NULL, $linking=NULL){
321    if ($this->capture && $title) $this->doc .= '['.$title.']';
322  }
323
324  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
325                         $height=NULL, $cache=NULL, $linking=NULL){
326    if ($this->capture && $title) $this->doc .= '['.$title.']';
327  }
328
329  function rss($url,$params) {
330    $this->meta['relation']['haspart'][$url] = true;
331
332    $this->meta['date']['valid']['age'] =
333            isset($this->meta['date']['valid']['age']) ?
334                min($this->meta['date']['valid']['age'],$params['refresh']) :
335                $params['refresh'];
336  }
337
338  function table_open($maxcols = NULL, $numrows = NULL){}
339  function table_close(){}
340
341  function tablerow_open(){}
342  function tablerow_close(){}
343
344  function tableheader_open($colspan = 1, $align = NULL){}
345  function tableheader_close(){}
346
347  function tablecell_open($colspan = 1, $align = NULL){}
348  function tablecell_close(){}
349
350  //----------------------------------------------------------
351  // Utils
352
353  /**
354   * Removes any Namespace from the given name but keeps
355   * casing and special chars
356   *
357   * @author Andreas Gohr <andi@splitbrain.org>
358   */
359  function _simpleTitle($name){
360    global $conf;
361
362    if($conf['useslash']){
363        $nssep = '[:;/]';
364    }else{
365        $nssep = '[:;]';
366    }
367    $name = preg_replace('!.*'.$nssep.'!','',$name);
368    //if there is a hash we use the anchor name only
369    $name = preg_replace('!.*#!','',$name);
370    return $name;
371  }
372
373  /**
374   * Creates a linkid from a headline
375   *
376   * @param string  $title   The headline title
377   * @param boolean $create  Create a new unique ID?
378   * @author Andreas Gohr <andi@splitbrain.org>
379   */
380  function _headerToLink($title, $create=false) {
381    $title = str_replace(':','',cleanID($title));
382    $title = ltrim($title,'0123456789._-');
383    if(empty($title)) $title='section';
384
385    if($create){
386      // make sure tiles are unique
387      $num = '';
388      while(in_array($title.$num,$this->headers)){
389        ($num) ? $num++ : $num = 1;
390      }
391      $title = $title.$num;
392      $this->headers[] = $title;
393    }
394
395    return $title;
396  }
397
398  /**
399   * Construct a title and handle images in titles
400   *
401   * @author Harry Fuecks <hfuecks@gmail.com>
402   */
403  function _getLinkTitle($title, $default, $id=NULL) {
404    global $conf;
405
406    $isImage = false;
407    if (is_null($title)){
408      if ($conf['useheading'] && $id){
409        $heading = p_get_first_heading($id,false);
410        if ($heading) return $heading;
411      }
412      return $default;
413    } else if (is_string($title)){
414      return $title;
415    } else if (is_array($title)){
416      return '['.$title.']';
417    }
418  }
419
420}
421
422//Setup VIM: ex: et ts=4 enc=utf-8 :
423