1<?php
2/**
3 * Emphasis Plugin: Enables text highlighting with
4 *                  ::text::, :::text:::, ::::text::::
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Stefan Hechenberger <foss@stefanix.net>
8 * @author     Gerrit Uitslag <klapinklapin@gmail.com>
9 */
10
11/**
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
14 */
15class syntax_plugin_emphasis_font extends DokuWiki_Syntax_Plugin {
16    /** @var $odt_style_name */
17    protected $odt_style_name;
18
19    /** @var array $colorlist */
20    var $colorlist = array(
21        'color'             => array(),
22        'background-color'  => array()
23    );
24
25    /**
26     * @return string Syntax Mode type
27     */
28    function getType() {
29        return 'formatting';
30    }
31
32    /**
33     * @return int Sort order - Low numbers go before high numbers
34     */
35    function getSort() {
36        return 136;
37    }
38
39    /**
40     * @return array allowed Mode Types
41     */
42    function getAllowedTypes() {
43        return array('formatting', 'substition', 'disabled');
44    }
45
46    /**
47     * @return string normal|block|stack - how this plugin is handled regarding paragraphs
48     */
49    function getPType() {
50        return 'normal';
51    }
52
53    /**
54     * Connect lookup pattern to lexer.
55     *
56     * @param string $mode Parser mode
57     */
58    function connectTo($mode) {
59        $this->Lexer->addEntryPattern(':{2,}(?=.*?:{2,})', $mode, 'plugin_emphasis_font');
60    }
61
62    function postConnect() {
63        $this->Lexer->addExitPattern(':{2,}', 'plugin_emphasis_font');
64    }
65
66    /**
67     * Handle matches of the Emphasis syntax
68     *
69     * @param string          $match The match of the syntax
70     * @param int             $state The state of the handler
71     * @param int             $pos The position in the document
72     * @param Doku_Handler    $handler The handler
73     * @return false|array Data for the renderer
74     */
75    function handle($match, $state, $pos, Doku_Handler $handler) {
76        $data['match'] = $match;
77
78        switch($state) {
79            case DOKU_LEXER_ENTER:
80                $colortype = ($match[0] == ':' ? 'color':'background-color');
81
82                //fill colorlist from config
83                if(empty($this->colorlist[$colortype])) {
84                    $colors = explode(',', $this->getConf($colortype.'s'));
85                    foreach($colors as $color) {
86                        //clean up colorcodes
87                        $color = trim($color);
88                        if($color[0] == '#') $color = substr($color, 1);
89                        if(preg_match('/[^A-Fa-f0-9]/', $color)) continue;
90                        //length 3 or 6 chars
91                        $clen = strlen($color);
92                        if(!($clen == 3 || $clen == 6)) continue;
93                        $this->colorlist[$colortype][] = '#'.$color;
94                    }
95                }
96
97                //degree of emphasis
98                $maxdegree = count($this->colorlist[$colortype]);
99                $data['degree'] = strlen($match) - 1;
100                if($data['degree'] > $maxdegree) {
101                    $data['degree'] = $maxdegree;
102                }
103
104                //color lookup
105                $data['color'] = $this->colorlist[$colortype][$data['degree'] - 1];
106                $data['colortype'] = $colortype;
107
108                return array($state, $data);
109
110            case DOKU_LEXER_UNMATCHED:
111                // normal text
112                $handler->addCall('cdata', array($match), $pos);
113                return false;
114
115            case DOKU_LEXER_EXIT:
116                return array($state, $data);
117        }
118
119        return array();
120    }
121
122    /**
123     * Render xhtml output, latex output or metadata
124     *
125     * @param string         $format      Renderer mode (supported modes: xhtml, latex and metadata)
126     * @param Doku_Renderer  $renderer  The renderer
127     * @param array          $data    The data from the handler function
128     * @return bool If rendering was successful.
129     */
130    function render($format, Doku_Renderer $renderer, $data) {
131        list($state, $data) = $data;
132
133        if($format == 'xhtml') {
134            /** @var Doku_Renderer_xhtml $renderer */
135            switch($state) {
136                case DOKU_LEXER_ENTER:
137                    $renderer->doc .= '<span class="plugin_emphasis" style="'.$data['colortype'].': '.$data['color'].';">';
138                    return true;
139
140                case DOKU_LEXER_EXIT:
141                    $renderer->doc .= '</span>';
142                    return true;
143            }
144        }
145        if($format == 'odt'){
146            /** @var renderer_plugin_odt_page $renderer */
147            switch ($state) {
148                case DOKU_LEXER_ENTER:
149                    if (!class_exists('ODTDocument')) {
150                        $renderer->_odtSpanOpenUseCSSStyle ($data['colortype'].': '.$data['color'].';font-weight:bold;');
151                    } else {
152                        $renderer->_odtSpanOpenUseCSS (NULL, 'class="plugin_emphasis" style="'.$data['colortype'].': '.$data['color'].';"');
153                    }
154                    return true;
155
156                case DOKU_LEXER_EXIT:
157                    // Close the span.
158                    $renderer->_odtSpanClose();
159                    return true;
160            }
161        }
162        return false;
163    }
164
165}
166