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