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