summaryrefslogtreecommitdiffstats
path: root/paste/include/geshi/classes/class.geshistyler.php
blob: fa56478c5bcf91e7d74cc11a0ec280a06df21059 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
 * GeSHi - Generic Syntax Highlighter
 * 
 * For information on how to use GeSHi, please consult the documentation
 * found in the docs/ directory, or online at http://geshi.org/docs/
 * 
 *  This file is part of GeSHi.
 *
 *  GeSHi is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  GeSHi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GeSHi; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can view a copy of the GNU GPL in the COPYING file that comes
 * with GeSHi, in the docs/ directory.
 *
 * @package   core
 * @author    Nigel McNie <nigel@geshi.org>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright (C) 2005 Nigel McNie
 * @version   1.1.0
 * 
 */

/**
 * The GeSHiStyler class
 * 
 * @package core
 * @author  Nigel McNie <nigel@geshi.org>
 * @since   1.1.0
 * @version   1.1.0
 */
class GeSHiStyler
{
    /**
     * @var string
     */
    var $charset;

    /**
     * @var string
     */    
    var $fileExtension;
    
    /**
     * @var array
     */
    var $_styleData = array();
    
    /**
     * @var array
     */
    var $_parseData;
           
    /**
     * @var int
     */
    var $_parseDataPointer = 0;
    
    /**
     * @var array
     */
    var $_contextCacheData = array();
        
    function setStyle ($context_name, $style, $start_name = 'start', $end_name = 'end')
    {
        // @todo [blocking 1.1.1] Why is this called sometimes with blank data?
        geshi_dbg('GeSHiStyler::setStyle(' . $context_name . ', ' . $style . ')', GESHI_DBG_PARSE);
        $this->_styleData[$context_name] = $style;
        /*if (!isset($this->_styleData["$context_name/$start_name"])) {
           $this->_styleData["$context_name/$start_name"] = $style;
        }
        if (!isset($this->_styleData["$context_name/$end_name"])) {
           $this->_styleData["$context_name/$end_name"] = $style;
        }*/
    }
    
    /*function setStartStyle ($context_name, $style)
    {
        $this->_styleData["$context_name/$this->_startName"] = $style;
    }
    
    function setStartName ($name)
    {
        $this->_startName = $name;
    }*/
    
    function removeStyleData ($context_name, $context_start_name = 'start', $context_end_name = 'end')
    {
        unset($this->_styleData[$context_name]);
        unset($this->_styleData["$context_name/$context_start_name"]);
        unset($this->_styleData["$context_name/$context_end_name"]);
        geshi_dbg('  removed style data for ' . $context_name, GESHI_DBG_PARSE);
    }
    
    /*function setEndStyle ($context_name, $style)
    {
        $this->_styleData["$context_name/$this->_endName"] = $style;
    }
    
    function setEndName ($name)
    {
        $this->_endName = $name;
    }*/
    
    function getStyle ($context_name)
    {
        if (isset($this->_styleData[$context_name])) {
            return $this->_styleData[$context_name];
        }
        // If style for starter/ender requested and we got here, use the default
        if ('/end' == substr($context_name, -4)) {
            $this->_styleData[$context_name] = $this->_styleData[substr($context_name, 0, -4)];
            return $this->_styleData[$context_name]; 
        }
        if ('/start' == substr($context_name, -6)) {
            $this->_styleData[$context_name] = $this->_styleData[substr($context_name, 0, -6)];
            return $this->_styleData[$context_name]; 
        }
         
        //@todo [blocking 1.1.5] Make the default style for otherwise unstyled elements configurable
        $this->_styleData[$context_name] = 'color:#000;';
        return 'color:#000;';
    }
    /*
    function getStyleStart ($context_name)
    {
        if (isset($this->_styleData["$context_name/$this->_startName"])) {
            return $this->_styleData["$context_name/$this->_startName"];
        }
        $this->_styleData["$context_name/$this->_startName"] = $this->getStyle($context_name);
        return $this->_styleData["$context_name/$this->_startName"];
    }
    
    function getStyleEnd ($context_name) 
    {
        if (isset($this->_styleData["$context_name/$this->_endName"])) {
            return $this->_styleData["$context_name/$this->_endName"];
        }
        $this->_styleData["$context_name/$this->_endName"] = $this->getStyle($context_name);
        return $this->_styleData["$context_name/$this->_endName"];
    }*/
    /*
    function startIsUnique ($context_name)
    {
        return (isset($this->_styleData["$context_name/$this->_startName"])
            && '' != $this->_styleData["$context_name/$this->_startName"]
            && $this->_styleData["$context_name/$this->_startName"] != $this->_styleData[$context_name]);
    } 

    function endIsUnique ($context_name)
    {
        $r = (isset($this->_styleData["$context_name/$this->_endName"])
            && '' != $this->_styleData["$context_name/$this->_endName"]
            && $this->_styleData["$context_name/$this->_endName"] != $this->_styleData[$context_name]);
        geshi_dbg('GeSHiStyler::endIsUnique(' . $context_name . ') = ' . $r, GESHI_DBG_PARSE);
        return $r;
    } 
    */
    function resetParseData ()
    {
        $this->_parseData        = null;
        $this->_parseDataPointer = 0;
    }

    /**
     * This method adds parse data. It tries to merge it also if two
     * consecutive contexts with the same name add parse data (which is
     * very possible).
     */    
    function addParseData ($code, $context_name, $url = '')
    {
        if ($context_name == $this->_parseData[$this->_parseDataPointer][1]) {
            // same context, same URL
            $this->_parseData[$this->_parseDataPointer][0] .= $code;
        } else {
            $this->_parseData[++$this->_parseDataPointer] = array($code, $context_name, $url);
        }
    }
    
    function addParseDataStart ($code, $context_name, $start_name = 'start')
    {
    	$this->addParseData($code, "$context_name/$start_name");
    }
    
    function addParseDataEnd ($code, $context_name, $end_name = 'end')
    {
    	$this->addParseData($code, "$context_name/$end_name");
    }
    
    function getParseData ()
    {
        return $this->_parseData;
    }
    
    /**
     * Sets cache data
     */
    function setCacheData ($cached_file_name, $cache_str)
    {
        $this->_contextCacheData[$cached_file_name] = $cache_str;
    }
    
    /**
     * Gets cache data
     */
    function getCacheData ($cached_file_name)
    {
        return isset($this->_contextCacheData[$cached_file_name]) ?
            $this->_contextCacheData[$cached_file_name] : null;
    }
}

?>