1<?php
2/**
3 * DokuWiki Plugin colortag (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:     <colortag>color,...,color</colortag>
9 *
10 * Renders as:
11 *   <table class='colortag'> ... </table>
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_colortag_colortag 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('<colortag(?=[^\r\n]*?>.*?</colortag>)',$mode,'plugin_colortag_colortag');
41    }
42
43    public function postConnect() {
44      $this->Lexer->addExitPattern('</colortag>', 'plugin_colortag_colortag');
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 <colortag ... to ... </colortag >
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      if (count($data) == 3) {
68        list($syntax, $attr, $content) = $data;
69        if ($syntax != 'colortag') return false;
70    $ColorTag = '<table class="colortag"><tbody><tr>';
71    foreach($content as $color) {
72      $c = trim($color);
73      $f = null;
74      $l = null;
75      $a = explode(':',$c);
76      if( count($a) == 3 ) {
77        $c = $a[0];
78        $f = $a[1];
79        $l = $a[2];
80      } elseif( count($a) == 2 ) {
81        $c = $a[0];
82        $f = $a[1];
83      } elseif( count($a) != 1 ) {
84        $c = '*invalid*';
85      }
86      if( !(   preg_match('/^[a-z]+$/',$c)
87          or preg_match('/^#[0-9a-f]{6,6}$/i',$c)
88          or preg_match('/^#[0-9a-f]{3,3}$/i',$c)
89      ) ) {
90        $c = 'white;color:red';
91        $f = 'X';
92      } elseif(!is_null($l)) {
93        if(    preg_match('/^[a-z]+$/',$l)
94          or preg_match('/^#[0-9a-f]{6,6}$/i',$l)
95          or preg_match('/^#[0-9a-f]{3,3}$/i',$l)
96        ) { $c .= ';color:'.$l; } else { $c = 'white;color:red'; $f = 'Z'; }
97      }
98      $f = is_null($f) ? '&nbsp;' : strip_tags($f);
99      $ColorTag .= '<td style="background-color:'.$c.'">'.$f.'</td>';
100    }
101    $ColorTag .= '</tr></tbody></table>';
102    $renderer->doc .= $ColorTag;
103        return true;
104      }
105      return false;
106    }
107}
108
109// vim:ts=4:sw=4:et:
110