1  <?php
2  /**
3   * DokuWiki Plugin pixelyear (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_pixelyear 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('<pixelyear.*?>(?=.*?</pixelyear>)',$mode,'plugin_pixelyear'); }
22       public function postConnect() { $this->Lexer->addExitPattern('</pixelyear>','plugin_pixelyear'); }
23
24
25       /**
26        * Handle the match
27        */
28       public function handle($match, $state, $pos, Doku_Handler $handler){
29           switch ($state) {
30             case DOKU_LEXER_ENTER :
31                  return array($state, '');
32             case DOKU_LEXER_UNMATCHED :
33                  return array($state, $match);
34             case DOKU_LEXER_EXIT :
35                  return array($state, '');
36           }
37           return array();
38       }
39
40       /**
41        * Create output
42        */
43       public function render($mode, Doku_Renderer $renderer, $data) {
44           // $data is what the function handle() return'ed.
45           if($mode == 'xhtml'){
46               /** @var Doku_Renderer_xhtml $renderer */
47               list($state,$match) = $data;
48               switch ($state) {
49                   case DOKU_LEXER_ENTER :
50                       $renderer->doc .= "<table>";
51                       break;
52
53                   case DOKU_LEXER_UNMATCHED :
54                       $daysinmonth=array(
55                         1 =>31,
56                         2 =>28,
57                         3 =>31,
58                         4 =>30,
59                         5 =>31,
60                         6 =>30,
61                         7 =>31,
62                         8 =>31,
63                         9 =>30,
64                         10 =>31,
65                         11 =>30,
66                         12 =>31,
67                       );
68                       $colours = array (
69                         0 => 'silver',
70                         1 => 'red',
71                         2 => 'orange',
72                         3 => 'yellow',
73                         4 => 'lime',
74                         5 => 'green'
75                       );
76                       $rows = explode ("\n", $match);
77                       $dates = array();
78                       foreach ($rows as $row) {
79                         list ($month, $day, $pixel) = explode ("|", $row);
80                         if ($month>=1&&$month<=12) {
81                           if ($day>=1&&$day<=31) {
82                             if ($pixel>=1 and $pixel<=5) {
83                               $dates[intval($month)."-".intval($day)]=$pixel;
84                             }
85                           }
86                         }
87                       }
88                       for ($m=1;$m<=12;$m++){
89                         $renderer->doc .= "<tr>";
90                         for ($d=1;$d<=$daysinmonth[$m];$d++){
91                           if (isset ($dates[$m."-".$d])) {
92                             $colour = $colours [$dates[$m."-".$d]];
93                           } else {
94                             $colour = "silver";
95                           }
96                           $renderer->doc .= "<td style='background-color:{$colour}'>&nbsp;</td>";
97                         }
98                         $renderer->doc .= "</tr>";
99                       }
100                       break;
101                   case DOKU_LEXER_EXIT :
102                       $renderer->doc .= "</table>";
103                       break;
104               }
105               return true;
106           }
107           return false;
108       }
109   }
110