1<?php
2/**
3 * cellbg Plugin: Allows user-defined colored cells in tables
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     dr4Ke <dr4ke@dr4ke.net>
7 * @link       http://git.dr4ke.net/?p=dr4Ke/forks/dokuwiki-cells-bg.git
8 * @version    1.0
9 *
10 * Derived from the highlight plugin from : http://www.dokuwiki.org/plugin:highlight
11 * and : http://www.staddle.net/wiki/plugins/highlight
12 */
13
14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'syntax.php');
17
18/**
19 * All DokuWiki plugins to extend the parser/rendering mechanism
20 * need to inherit from this class
21 */
22class syntax_plugin_cellbg extends DokuWiki_Syntax_Plugin {
23
24    function getInfo(){  // return some info
25        return array(
26            'author' => 'dr4Ke',
27            'email'  => 'dr4ke@dr4ke.net',
28            'date'   => '2013-10-09',
29            'name'   => 'Cells background color plugin',
30            'desc'   => 'Sets background of a cell with a specific color',
31            'url'    => 'http://www.dokuwiki.org/plugin:cellbg',
32        );
33    }
34
35     // What kind of syntax are we?
36    function getType(){ return 'formatting'; }
37
38    // What kind of syntax do we allow (optional)
39    function getAllowedTypes() {
40        return array('formatting', 'substition', 'disabled');
41    }
42
43   // What about paragraphs? (optional)
44   function getPType(){ return 'normal'; }
45
46    // Where to sort in?
47    function getSort(){ return 200; }
48
49
50    // Connect pattern to lexer
51    function connectTo($mode) {
52      $this->Lexer->addSpecialPattern('^@#?[0-9a-zA-Z]*:(?=[^\n]*\|[[:space:]]*\n)',$mode,'plugin_cellbg');
53    }
54    function postConnect() {
55      //$this->Lexer->addExitPattern(':','plugin_cellbg');
56    }
57
58
59    // Handle the match
60    function handle($match, $state, $pos, Doku_Handler $handler){
61        switch ($state) {
62          case DOKU_LEXER_ENTER :
63            break;
64          case DOKU_LEXER_MATCHED :
65            break;
66          case DOKU_LEXER_UNMATCHED :
67            //return array($state, $match);
68            break;
69          case DOKU_LEXER_EXIT :
70            break;
71          case DOKU_LEXER_SPECIAL :
72            preg_match("/@([^:]*)/", $match, $color); // get the color
73            if ( $this->_isValid($color[1]) ) return array($state, $color[1], $match);
74            break;
75        }
76        return array($state, "yellow", $match);
77    }
78
79    // Create output
80    function render($mode, Doku_Renderer $renderer, $data) {
81        if($mode == 'xhtml'){
82          list($state, $color, $text) = $data;
83          switch ($state) {
84            case DOKU_LEXER_ENTER :
85              break;
86            case DOKU_LEXER_MATCHED :
87              break;
88            case DOKU_LEXER_UNMATCHED :
89              //$renderer->doc .= $renderer->_xmlEntities($color);
90              break;
91            case DOKU_LEXER_EXIT :
92              break;
93            case DOKU_LEXER_SPECIAL :
94              if (preg_match('/(<td[^<>]*)>[[:space:]]*$/', $renderer->doc) != 0) {
95                $renderer->doc = preg_replace('/(<td[^<>]*)>[[:space:]]*$/', '\1 bgcolor='.$color.'>', $renderer->doc);
96              } else {
97                $renderer->doc .= $text;
98              }
99              break;
100          }
101          return true;
102        }
103        return false;
104    }
105
106    // validate color value $c
107    // this is cut price validation - only to ensure the basic format is
108    // correct and there is nothing harmful
109    // three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
110    function _isValid($c) {
111
112        $c = trim($c);
113
114        $pattern = "/
115            (^[a-zA-Z]+$)|                                #colorname - not verified
116            (^\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$)|        #colorvalue
117            (^rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)$)     #rgb triplet
118            /x";
119
120        return (preg_match($pattern, $c));
121
122    }
123}
124
125//Setup VIM: ex: et ts=4 sw=4 enc=utf-8 :
126