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