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