1<?php
2/**
3 * Plugin Color: Sets new colors for text and background.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christopher Smith <chris@jalakai.co.uk>
7 */
8
9// must be run within DokuWiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_color extends DokuWiki_Syntax_Plugin {
17
18    function getType(){ return 'formatting'; }
19    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
20    function getSort(){ return 158; }
21    function connectTo($mode) { $this->Lexer->addEntryPattern('<color.*?>(?=.*?</color>)',$mode,'plugin_color'); }
22    function postConnect() { $this->Lexer->addExitPattern('</color>','plugin_color'); }
23
24
25    /**
26     * Handle the match
27     */
28    function handle($match, $state, $pos, Doku_Handler $handler){
29        switch ($state) {
30          case DOKU_LEXER_ENTER :
31              $str = substr($match, 6, -1);
32              if (false === strpbrk($str,':')){
33                 $m = explode('/', $str);
34              } else {
35                 $m = explode(':', $str);
36              }
37              $color = $this->_specToCSS('color', $m[0]);
38              $background = $this->_specToCSS('background-color',
39                                              isset($m[1]) ? $m[1] : null);
40              return [$state, [$color, $background]];
41
42          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
43          case DOKU_LEXER_EXIT :       return array($state, '');
44        }
45        return array();
46    }
47
48    /**
49     * Create output
50     */
51    function render($mode, Doku_Renderer $renderer, $data) {
52        if($mode == 'xhtml'){
53            list($state, $match) = $data;
54            switch ($state) {
55              case DOKU_LEXER_ENTER :
56                list($color, $background) = $match;
57                $renderer->doc .= "<span style='$color $background'>";
58                break;
59
60              case DOKU_LEXER_UNMATCHED :  $renderer->doc .= $renderer->_xmlEntities($match); break;
61              case DOKU_LEXER_EXIT :       $renderer->doc .= "</span>"; break;
62            }
63            return true;
64        }
65        if($mode == 'odt'){
66            list($state, $match) = $data;
67            switch ($state) {
68              case DOKU_LEXER_ENTER :
69                list($color, $background) = $match;
70                if (class_exists('ODTDocument')) {
71                    $renderer->_odtSpanOpenUseCSS (NULL, 'style="'.$color.$background.'"');
72                }
73                break;
74
75              case DOKU_LEXER_UNMATCHED :
76                $renderer->cdata($match);
77                break;
78
79              case DOKU_LEXER_EXIT :
80                if (class_exists('ODTDocument')) {
81                    $renderer->_odtSpanClose();
82                }
83                break;
84            }
85            return true;
86        }
87        if($mode == 'metadata'){
88            list($state, $match) = $data;
89            switch ($state) {
90              case DOKU_LEXER_UNMATCHED :
91                if ($renderer->capture) $renderer->cdata($match);
92                break;
93            }
94            return true;
95        }
96        return false;
97    }
98
99    // Build a CSS attribute:value pair.
100    function _specToCSS($attrib, $c) {
101        $c = trim($c);
102        return ((!empty($c) &&
103                 $this->_isValid($c)) ? $attrib.':'.$c.';'
104                                      : null);
105    }
106
107    // validate color value $c
108    // this is cut price validation - only to ensure there is nothing harmful
109    // just ensure that no illegal characters are included therein
110    // leave it to the browsers to ignore a faulty colour specification
111    function _isValid($c) {
112        return (false === strpbrk($c, '"\'<>&;'));
113    }
114}
115?>
116