1<?php
2/**
3 * DokuWiki Plugin since (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  jno <jno@pisem.net>
7 *
8 * Syntax:     <since>ISO-date</since>
9 *
10 * Renders as:
11 *   <span class='since'> ... </span>
12 */
13
14// must be run within Dokuwiki
15if (!defined('DOKU_INC')) die();
16
17if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
18if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
19if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20
21require_once DOKU_PLUGIN.'syntax.php';
22
23class syntax_plugin_since_since extends DokuWiki_Syntax_Plugin {
24    public function getType() {
25        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
26        return 'protected';
27    }
28
29    public function getPType() {
30        // return 'FIXME: normal|block|stack';
31        return 'block';
32    }
33
34    public function getSort() {
35        return 195;
36    }
37
38
39    public function connectTo($mode) {
40      $this->Lexer->addEntryPattern('<since(?=[^\r\n]*?>.*?</since>)',$mode,'plugin_since_since');
41    }
42
43    public function postConnect() {
44      $this->Lexer->addExitPattern('</since>', 'plugin_since_since');
45    }
46
47    public function handle($match, $state, $pos, &$handler){
48        switch ($state) {
49          case DOKU_LEXER_ENTER:
50            $this->syntax = substr($match, 1);
51            return false;
52
53          case DOKU_LEXER_UNMATCHED:
54             // will include everything from <since ... to ... </since >
55             // e.g. ... [attr] > [content]
56             list($attr, $content) = preg_split('/>/u',$match,2);
57	     $content = explode(',',$content);
58	     // $attr reserved for future use
59
60             return array($this->syntax, trim($attr), $content);
61        }
62        return false;
63    }
64
65    public function render($mode, &$renderer, $data) {
66      if($mode != 'xhtml') return false;
67      $now = new DateTime();
68      // if (count($data) == 1) {
69        list($syntax, $attr, $content) = $data;
70        if ($syntax != 'since') return false;
71	$SinceDate = '';
72	foreach($content as $date) {
73	  $d = date_create( trim($date) );
74	  if( $d === FALSE ) {
75	    $SinceDate .= "&lt;bad date '$date'&gt;";
76	    break;
77	  }
78	  $interval = $d->diff($now);
79	  $s = $interval->format('%R');
80	  $s1 = ($s == '-') ? 'in&nbsp;' : '';
81	  $s2 = ($s == '-') ? '' : '&nbsp;ago';
82	  $SinceDate .= trim($date).'&nbsp;'.'<span class="since">'
83		      .$s1.$interval->format('%yy %mm %dd').$s2
84		      .'</span>';
85	}
86	$renderer->doc .= $SinceDate;
87        return true;
88      // }
89      //return false;
90    }
91}
92
93// vim:ts=4:sw=4:et:
94