xref: /dokuwiki/inc/parser/metadata.php (revision 886f80e7a9d4651ee3524837f0f5e430687ba6a5)
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->meta['date']['valid'] = array();
42    $this->headers = array();
43  }
44
45  function document_end(){
46    if (!$this->meta['description']['abstract']){
47      // cut off too long abstracts
48      $this->doc = trim($this->doc);
49      if (strlen($this->doc) > 500)
50        $this->doc = substr($this->doc, 0, 500).'…';
51      $this->meta['description']['abstract'] = $this->doc;
52    }
53  }
54
55  function header($text, $level, $pos) {
56    global $conf;
57
58    if (!$this->meta['title']) $this->meta['title'] = $text;
59
60    // create a unique header id
61    $hid = $this->_headerToLink($text,'true');
62
63    //handle TOC
64    if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
65      // the TOC is one of our standard ul list arrays ;-)
66      $this->meta['description']['tableofcontents'][] = array(
67        'hid'   => $hid,
68        'title' => $text,
69        'type'  => 'ul',
70        'level' => $level-$conf['toptoclevel']+1
71      );
72    }
73
74    // add to summary
75    if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
76  }
77
78  function section_open($level){}
79  function section_close(){}
80
81  function cdata($text){
82    if ($this->capture) $this->doc .= $text;
83  }
84
85  function p_open(){
86    if ($this->capture) $this->doc .= DOKU_LF;
87  }
88
89  function p_close(){
90    if ($this->capture){
91      if (strlen($this->doc) > 250) $this->capture = false;
92      else $this->doc .= DOKU_LF;
93    }
94  }
95
96  function linebreak(){
97    if ($this->capture) $this->doc .= DOKU_LF;
98  }
99
100  function hr(){
101    if ($this->capture){
102      if (strlen($this->doc) > 250) $this->capture = false;
103      else $this->doc .= DOKU_LF.'----------'.DOKU_LF;
104    }
105  }
106
107  function strong_open(){}
108  function strong_close(){}
109
110  function emphasis_open(){}
111  function emphasis_close(){}
112
113  function underline_open(){}
114  function underline_close(){}
115
116  function monospace_open(){}
117  function monospace_close(){}
118
119  function subscript_open(){}
120  function subscript_close(){}
121
122  function superscript_open(){}
123  function superscript_close(){}
124
125  function deleted_open(){}
126  function deleted_close(){}
127
128  /**
129   * Callback for footnote start syntax
130   *
131   * All following content will go to the footnote instead of
132   * the document. To achieve this the previous rendered content
133   * is moved to $store and $doc is cleared
134   *
135   * @author Andreas Gohr <andi@splitbrain.org>
136   */
137  function footnote_open() {
138    if ($this->capture){
139      // move current content to store and record footnote
140      $this->store = $this->doc;
141      $this->doc   = '';
142    }
143  }
144
145  /**
146   * Callback for footnote end syntax
147   *
148   * All rendered content is moved to the $footnotes array and the old
149   * content is restored from $store again
150   *
151   * @author Andreas Gohr
152   */
153  function footnote_close() {
154    if ($this->capture){
155      // restore old content
156      $this->doc = $this->store;
157      $this->store = '';
158    }
159  }
160
161  function listu_open(){
162    if ($this->capture) $this->doc .= DOKU_LF;
163  }
164
165  function listu_close(){
166    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
167  }
168
169  function listo_open(){
170    if ($this->capture) $this->doc .= DOKU_LF;
171  }
172
173  function listo_close(){
174    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
175  }
176
177  function listitem_open($level){
178    if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
179  }
180
181  function listitem_close(){
182    if ($this->capture) $this->doc .= DOKU_LF;
183  }
184
185  function listcontent_open(){}
186  function listcontent_close(){}
187
188  function unformatted($text){
189    if ($this->capture) $this->doc .= $text;
190  }
191
192  function php($text){}
193
194  function html($text){}
195
196  function preformatted($text){
197    if ($this->capture) $this->doc .= $text;
198  }
199
200  function file($text){
201    if ($this->capture){
202      $this->doc .= DOKU_LF.$text;
203      if (strlen($this->doc) > 250) $this->capture = false;
204      else $this->doc .= DOKU_LF;
205    }
206  }
207
208  function quote_open(){
209    if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'“';
210  }
211
212  function quote_close(){
213    if ($this->capture){
214      $this->doc .= '”';
215      if (strlen($this->doc) > 250) $this->capture = false;
216      else $this->doc .= DOKU_LF;
217    }
218  }
219
220  function code($text, $language = NULL){
221    if ($this->capture){
222      $this->doc .= DOKU_LF.$text;
223      if (strlen($this->doc) > 250) $this->capture = false;
224      else $this->doc .= DOKU_LF;
225    }
226  }
227
228  function acronym($acronym){
229    if ($this->capture) $this->doc .= $acronym;
230  }
231
232  function smiley($smiley){
233    if ($this->capture) $this->doc .= $smiley;
234  }
235
236  function entity($entity){
237    if ($this->capture) $this->doc .= $entity;
238  }
239
240  function multiplyentity($x, $y){
241    if ($this->capture) $this->doc .= $x.'×'.$y;
242  }
243
244  function singlequoteopening(){
245    if ($this->capture) $this->doc .= '‘';
246  }
247
248  function singlequoteclosing(){
249    if ($this->capture) $this->doc .= '’';
250  }
251
252  function doublequoteopening(){
253    if ($this->capture) $this->doc .= '“';
254  }
255
256  function doublequoteclosing(){
257    if ($this->capture) $this->doc .= '”';
258  }
259
260  function camelcaselink($link) {
261    $this->internallink($link, $link);
262  }
263
264  function locallink($hash, $name = NULL){}
265
266  /**
267   * keep track of internal links in $this->meta['relation']['references']
268   */
269  function internallink($id, $name = NULL){
270    global $ID;
271
272    $default = $this->_simpleTitle($id);
273
274    // first resolve and clean up the $id
275    resolve_pageid(getNS($ID), $id, $exists);
276    list($page, $hash) = split('#', $id, 2);
277
278    // set metadata
279    $this->meta['relation']['references'][$page] = $exists;
280    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
281    // p_set_metadata($id, $data);
282
283    // add link title to summary
284    if ($this->capture){
285      $name = $this->_getLinkTitle($name, $default, $id);
286      $this->doc .= $name;
287    }
288  }
289
290  function externallink($url, $name = NULL){
291    if ($this->capture){
292      if ($name) $this->doc .= $name;
293      else $this->doc .= '<'.$url.'>';
294    }
295  }
296
297  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
298    if ($this->capture){
299      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
300      $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
301      $this->doc .= $name;
302    }
303  }
304
305  function windowssharelink($url, $name = NULL){
306    if ($this->capture){
307      if ($name) $this->doc .= $name;
308      else $this->doc .= '<'.$url.'>';
309    }
310  }
311
312  function emaillink($address, $name = NULL){
313    if ($this->capture){
314      if ($name) $this->doc .= $name;
315      else $this->doc .= '<'.$address.'>';
316    }
317  }
318
319  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
320                         $height=NULL, $cache=NULL, $linking=NULL){
321    if ($this->capture && $title) $this->doc .= '['.$title.']';
322  }
323
324  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
325                         $height=NULL, $cache=NULL, $linking=NULL){
326    if ($this->capture && $title) $this->doc .= '['.$title.']';
327  }
328
329  function rss($url,$params) {
330    $this->meta['relation']['haspart'][$url] = true;
331   	$this->meta['date']['valid']['age'] = $params['refresh'];
332  }
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 anchor 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 :
419