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