1  <?php
2  /**
3   * DokuWiki Plugin sincetil (Syntax Component)
4   *
5   * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6   * @author  Paul Brownsea <pigzag@gmx.co.uk>
7   */
8
9   // must be run within Dokuwiki
10   if(!defined('DOKU_INC')) die();
11
12   /**
13    * All DokuWiki plugins to extend the parser/rendering mechanism
14    * need to inherit from this class
15    */
16   class syntax_plugin_sincetil extends DokuWiki_Syntax_Plugin {
17
18       public function getType(){ return 'formatting'; }
19       public function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
20       public function getSort(){ return 158; }
21       public function connectTo($mode) { $this->Lexer->addEntryPattern('<sincetil.*?>(?=.*?</sincetil>)',$mode,'plugin_sincetil'); }
22       public function postConnect() { $this->Lexer->addExitPattern('</sincetil>','plugin_sincetil'); }
23
24       /**
25        * Handle the match
26        */
27       public function handle($match, $state, $pos, Doku_Handler $handler){
28           switch ($state) {
29             case DOKU_LEXER_ENTER :
30                  return array($state, '');
31             case DOKU_LEXER_UNMATCHED :
32                  return array($state, $match);
33             case DOKU_LEXER_EXIT :
34                  return array($state, '');
35           }
36           return array();
37       }
38
39       /**
40        * Create output
41        */
42       public function render($mode, Doku_Renderer $renderer, $data) {
43           // $data is what the function handle() return'ed.
44           if($mode == 'xhtml'){
45               /** @var Doku_Renderer_xhtml $renderer */
46               list($state,$match) = $data;
47               switch ($state) {
48                   case DOKU_LEXER_ENTER :
49                       break;
50                   case DOKU_LEXER_UNMATCHED :
51                       $date = new DateTime ($match);
52                       $now  = new DateTime ();
53                       $diff = $now -> diff ($date);
54
55                       if ($diff->y>0){
56                         $format = "%y years";
57                       } elseif ($diff->m>0){
58                         $format = "%m months";
59                       } elseif ($diff->d>0){
60                         $format = "%d days";
61                       } elseif ($diff->h>0){
62                         $format = "%h hours";
63                       } elseif ($diff->i>0){
64                         $format = "%i minutes";
65                       } elseif ($diff->s>0){
66                         $format = "%s seconds";
67                       } else {
68                         $format = "now";
69                       }
70
71                       $renderer->doc .= $diff -> format($format);
72
73                       if ($diff -> invert) {
74                         $renderer->doc .= " ago";
75                       } else {
76                         $renderer->doc .= " to go";
77                       }
78                       break;
79                   case DOKU_LEXER_EXIT :
80                       break;
81               }
82               return true;
83           }
84           return false;
85       }
86   }
87