summaryrefslogtreecommitdiff
path: root/inc/parser/metadata.php
blob: f3cea82cd4e53d619354a1f3cbe1045c3ba8a60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
 * Renderer for metadata
 *
 * @author Esther Brunner <wikidesign@gmail.com>
 */

if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../../').'/');

if ( !defined('DOKU_LF') ) {
    // Some whitespace to help View > Source
    define ('DOKU_LF',"\n");
}

if ( !defined('DOKU_TAB') ) {
    // Some whitespace to help View > Source
    define ('DOKU_TAB',"\t");
}

require_once DOKU_INC . 'inc/parser/renderer.php';

/**
 * The Renderer
 */
class Doku_Renderer_metadata extends Doku_Renderer {

  var $doc  = '';
  var $meta = array();
  var $persistent = array();

  var $headers = array();
  var $capture = true;
  var $store   = '';

  function getFormat(){
    return 'metadata';
  }

  function document_start(){
    // reset metadata to persistent values
    $this->meta = $this->persistent;
  }

  function document_end(){
    // store internal info in metadata (notoc,nocache)
    $this->meta['internal'] = $this->info;

    if (!$this->meta['description']['abstract']){
      // cut off too long abstracts
      $this->doc = trim($this->doc);
      if (strlen($this->doc) > 500)
        $this->doc = substr($this->doc, 0, 500).'…';
      $this->meta['description']['abstract'] = $this->doc;
    }
  }

  function toc_additem($id, $text, $level) {
    global $conf;

    //only add items within configured levels
    if($level >= $conf['toptoclevel'] && $level <= $conf['maxtoclevel']){
      // the TOC is one of our standard ul list arrays ;-)
      $this->meta['description']['tableofcontents'][] = array(
        'hid'   => $id,
        'title' => $text,
        'type'  => 'ul',
        'level' => $level-$conf['toptoclevel']+1
      );
    }

  }

  function header($text, $level, $pos) {

    if (!$this->meta['title']) $this->meta['title'] = $text;

    // add the header to the TOC
    $hid = $this->_headerToLink($text,'true');
    $this->toc_additem($hid, $text, $level);

    // add to summary
    if ($this->capture && ($level > 1)) $this->doc .= DOKU_LF.$text.DOKU_LF;
  }

  function section_open($level){}
  function section_close(){}

  function cdata($text){
    if ($this->capture) $this->doc .= $text;
  }

  function p_open(){
    if ($this->capture) $this->doc .= DOKU_LF;
  }

  function p_close(){
    if ($this->capture){
      if (strlen($this->doc) > 250) $this->capture = false;
      else $this->doc .= DOKU_LF;
    }
  }

  function linebreak(){
    if ($this->capture) $this->doc .= DOKU_LF;
  }

  function hr(){
    if ($this->capture){
      if (strlen($this->doc) > 250) $this->capture = false;
      else $this->doc .= DOKU_LF.'----------'.DOKU_LF;
    }
  }

  function strong_open(){}
  function strong_close(){}

  function emphasis_open(){}
  function emphasis_close(){}

  function underline_open(){}
  function underline_close(){}

  function monospace_open(){}
  function monospace_close(){}

  function subscript_open(){}
  function subscript_close(){}

  function superscript_open(){}
  function superscript_close(){}

  function deleted_open(){}
  function deleted_close(){}

  /**
   * Callback for footnote start syntax
   *
   * All following content will go to the footnote instead of
   * the document. To achieve this the previous rendered content
   * is moved to $store and $doc is cleared
   *
   * @author Andreas Gohr <andi@splitbrain.org>
   */
  function footnote_open() {
    if ($this->capture){
      // move current content to store and record footnote
      $this->store = $this->doc;
      $this->doc   = '';
    }
  }

  /**
   * Callback for footnote end syntax
   *
   * All rendered content is moved to the $footnotes array and the old
   * content is restored from $store again
   *
   * @author Andreas Gohr
   */
  function footnote_close() {
    if ($this->capture){
      // restore old content
      $this->doc = $this->store;
      $this->store = '';
    }
  }

  function listu_open(){
    if ($this->capture) $this->doc .= DOKU_LF;
  }

  function listu_close(){
    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
  }

  function listo_open(){
    if ($this->capture) $this->doc .= DOKU_LF;
  }

  function listo_close(){
    if ($this->capture && (strlen($this->doc) > 250)) $this->capture = false;
  }

  function listitem_open($level){
    if ($this->capture) $this->doc .= str_repeat(DOKU_TAB, $level).'* ';
  }

  function listitem_close(){
    if ($this->capture) $this->doc .= DOKU_LF;
  }

  function listcontent_open(){}
  function listcontent_close(){}

  function unformatted($text){
    if ($this->capture) $this->doc .= $text;
  }

  function php($text){}

  function phpblock($text){}

  function html($text){}

  function htmlblock($text){}

  function preformatted($text){
    if ($this->capture) $this->doc .= $text;
  }

  function file($text){
    if ($this->capture){
      $this->doc .= DOKU_LF.$text;
      if (strlen($this->doc) > 250) $this->capture = false;
      else $this->doc .= DOKU_LF;
    }
  }

  function quote_open(){
    if ($this->capture) $this->doc .= DOKU_LF.DOKU_TAB.'"';
  }

  function quote_close(){
    if ($this->capture){
      $this->doc .= '"';
      if (strlen($this->doc) > 250) $this->capture = false;
      else $this->doc .= DOKU_LF;
    }
  }

  function code($text, $language = NULL){
    if ($this->capture){
      $this->doc .= DOKU_LF.$text;
      if (strlen($this->doc) > 250) $this->capture = false;
      else $this->doc .= DOKU_LF;
    }
  }

  function acronym($acronym){
    if ($this->capture) $this->doc .= $acronym;
  }

  function smiley($smiley){
    if ($this->capture) $this->doc .= $smiley;
  }

  function entity($entity){
    if ($this->capture) $this->doc .= $entity;
  }

  function multiplyentity($x, $y){
    if ($this->capture) $this->doc .= $x.'×'.$y;
  }

  function singlequoteopening(){
    global $lang;
    if ($this->capture) $this->doc .= $lang['singlequoteopening'];
  }

  function singlequoteclosing(){
    global $lang;
    if ($this->capture) $this->doc .= $lang['singlequoteclosing'];
  }

  function apostrophe() {
    global $lang;
    if ($this->capture) $this->doc .= $lang['apostrophe'];
  }

  function doublequoteopening(){
    global $lang;
    if ($this->capture) $this->doc .= $lang['doublequoteopening'];
  }

  function doublequoteclosing(){
    global $lang;
    if ($this->capture) $this->doc .= $lang['doublequoteclosing'];
  }

  function camelcaselink($link) {
    $this->internallink($link, $link);
  }

  function locallink($hash, $name = NULL){}

  /**
   * keep track of internal links in $this->meta['relation']['references']
   */
  function internallink($id, $name = NULL){
    global $ID;

    $default = $this->_simpleTitle($id);

    // first resolve and clean up the $id
    resolve_pageid(getNS($ID), $id, $exists);
    list($page, $hash) = split('#', $id, 2);

    // set metadata
    $this->meta['relation']['references'][$page] = $exists;
    // $data = array('relation' => array('isreferencedby' => array($ID => true)));
    // p_set_metadata($id, $data);

    // add link title to summary
    if ($this->capture){
      $name = $this->_getLinkTitle($name, $default, $id);
      $this->doc .= $name;
    }
  }

  function externallink($url, $name = NULL){
    if ($this->capture){
      if ($name) $this->doc .= $name;
      else $this->doc .= '<'.$url.'>';
    }
  }

  function interwikilink($match, $name = NULL, $wikiName, $wikiUri){
    if ($this->capture){
      list($wikiUri, $hash) = explode('#', $wikiUri, 2);
      $name = $this->_getLinkTitle($name, $wikiName.'>'.$wikiUri);
      $this->doc .= $name;
    }
  }

  function windowssharelink($url, $name = NULL){
    if ($this->capture){
      if ($name) $this->doc .= $name;
      else $this->doc .= '<'.$url.'>';
    }
  }

  function emaillink($address, $name = NULL){
    if ($this->capture){
      if ($name) $this->doc .= $name;
      else $this->doc .= '<'.$address.'>';
    }
  }

  function internalmedia($src, $title=NULL, $align=NULL, $width=NULL,
                         $height=NULL, $cache=NULL, $linking=NULL){
    if ($this->capture && $title) $this->doc .= '['.$title.']';
  }

  function externalmedia($src, $title=NULL, $align=NULL, $width=NULL,
                         $height=NULL, $cache=NULL, $linking=NULL){
    if ($this->capture && $title) $this->doc .= '['.$title.']';
  }

  function rss($url,$params) {
    $this->meta['relation']['haspart'][$url] = true;

    $this->meta['date']['valid']['age'] =
            isset($this->meta['date']['valid']['age']) ?
                min($this->meta['date']['valid']['age'],$params['refresh']) :
                $params['refresh'];
  }

  function table_open($maxcols = NULL, $numrows = NULL){}
  function table_close(){}

  function tablerow_open(){}
  function tablerow_close(){}

  function tableheader_open($colspan = 1, $align = NULL){}
  function tableheader_close(){}

  function tablecell_open($colspan = 1, $align = NULL){}
  function tablecell_close(){}

  //----------------------------------------------------------
  // Utils

  /**
   * Removes any Namespace from the given name but keeps
   * casing and special chars
   *
   * @author Andreas Gohr <andi@splitbrain.org>
   */
  function _simpleTitle($name){
    global $conf;

    if(is_array($name)) return '';

    if($conf['useslash']){
        $nssep = '[:;/]';
    }else{
        $nssep = '[:;]';
    }
    $name = preg_replace('!.*'.$nssep.'!','',$name);
    //if there is a hash we use the anchor name only
    $name = preg_replace('!.*#!','',$name);
    return $name;
  }

  /**
   * Creates a linkid from a headline
   *
   * @param string  $title   The headline title
   * @param boolean $create  Create a new unique ID?
   * @author Andreas Gohr <andi@splitbrain.org>
   */
  function _headerToLink($title, $create=false) {
    $title = str_replace(':','',cleanID($title));
    $title = ltrim($title,'0123456789._-');
    if(empty($title)) $title='section';

    if($create){
      // make sure tiles are unique
      $num = '';
      while(in_array($title.$num,$this->headers)){
        ($num) ? $num++ : $num = 1;
      }
      $title = $title.$num;
      $this->headers[] = $title;
    }

    return $title;
  }

  /**
   * Construct a title and handle images in titles
   *
   * @author Harry Fuecks <hfuecks@gmail.com>
   */
  function _getLinkTitle($title, $default, $id=NULL) {
    global $conf;

    $isImage = false;
    if (is_null($title)){
      if ($conf['useheading'] && $id){
        $heading = p_get_first_heading($id,false);
        if ($heading) return $heading;
      }
      return $default;
    } else if (is_string($title)){
      return $title;
    } else if (is_array($title)){
      return '['.$title.']';
    }
  }

}

//Setup VIM: ex: et ts=4 enc=utf-8 :