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