xref: /dokuwiki/inc/parser/metadata.php (revision ce6b63d97068e71369bad95e7959d0110717bbfd)
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($page, $hash) = split('#', $id, 2);
276
277    // set metadata
278    $this->meta['relation']['references'][$page] = $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,$params) {
329    global $conf;
330
331    $this->meta['relation']['haspart'][$url] = true;
332    $this->meta['date']['valid']['end'] =
333         empty($this->meta['date']['valid']['end']) ?
334             time() + $conf['rss_update'] :
335             min($this->meta['date']['valid']['end'], time() + $conf['rss_update']);
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,true)); //force ASCII
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);
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