forked from xBBCoder/xBBCode
/
Code.php
225 lines (195 loc) · 6.35 KB
/
Code.php
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
225
<?php
/******************************************************************************
* *
* Copyright (C) 2006-2007 Dmitriy Skorobogatov dima@pc.uz *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
******************************************************************************/
namespace Xbbcode\Tag;
/**
* Class Code
* Класс для тегов подсветки синтаксиса и для тегов [code] и [pre]
*/
class Code extends Tag
{
//const BR_RIGHT = 1;
const BEHAVIOUR = 'pre';
/* Альтернативные названия языков и их трансляция в обозначения GeSHi */
public $langSynonym = array(
'algol' => 'algol86',
'c++' => 'cpp',
'c#' => 'csharp',
'f++' => 'fsharp',
'html' => 'html4strict',
'html4' => 'html4strict',
'js' => 'javascript',
'ocaml' => 'ocaml-brief',
'oracle' => 'oracle8',
't-sql' => 'tsql',
'vb.net' => 'vbnet',
);
/**
* @var \GeSHi
*/
protected $geshi;
/* Конструктор класса */
public function __construct()
{
parent::__construct();
$this->geshi = new \GeSHi('', 'text');
$this->geshi->set_header_type(GESHI_HEADER_NONE);
}
/**
* Язык для подсветки
*
* @return Code
*/
protected function setLanguage()
{
switch ($this->getTagName()) {
case 'code':
$language = $this->attributes['code'];
break;
case 'pre':
$language = $this->attributes['pre'];
break;
default:
$language = $this->getTagName();
break;
}
if (!$language) {
$language = 'text';
}
if (isset($this->langSynonym[$language])) {
$language = $this->langSynonym[$language];
}
$this->geshi->set_language($language);
return $this;
}
/**
* Подсвечиваемый код
*
* @return Code
*/
protected function setSource()
{
$this->geshi->set_source($this->getTreeText());
return $this;
}
/**
* Ссылки на документацию
*
* @return Code
*/
protected function setLinks()
{
if (isset($this->attributes['links'])) {
if ('1' === $this->attributes['links'] || 'true' === $this->attributes['links']) {
$this->geshi->enable_keyword_links(true);
} else {
$this->geshi->enable_keyword_links(false);
}
} else {
$this->geshi->enable_keyword_links($this->getKeywordLinks());
}
return $this;
}
/**
* Нумерация строк
*
* @return Code
*/
protected function setNum()
{
if (isset($this->attributes['num'])) {
$this->geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
if ('' !== $this->attributes['num']) {
$num = (int) $this->attributes['num'];
$this->geshi->start_line_numbers_at($num);
}
}
return $this;
}
/**
* Величина табуляции
*
* @return Code
*/
protected function setTab()
{
if (isset($this->attributes['tab'])) {
$this->attributes['tab'] = (int) $this->attributes['tab'];
if ($this->attributes['tab']) {
$this->geshi->set_tab_width($this->attributes['tab']);
}
}
return $this;
}
/**
* Выделение строк
*
* @return Code
*/
protected function setExtra()
{
if (isset($this->attributes['extra'])) {
$extra = explode(',', $this->attributes['extra']);
$this->geshi->highlight_lines_extra($extra);
}
return $this;
}
/**
* Получаем заголовок
*
* @return string
*/
protected function getHeader()
{
if (isset($this->attributes['title'])) {
$title = $this->attributes['title'];
} else {
$title = $this->geshi->get_language_name();
}
return '<div class="bb_code_header"><span class="bb_code_lang">' . htmlspecialchars($title, ENT_NOQUOTES) . '</span></div>';
}
/**
* Получаем подвал
*
* @return string
*/
protected function getFooter()
{
if (isset($this->attributes['footer'])) {
return '<div class="bb_code_footer">' . htmlspecialchars($this->attributes['footer'], ENT_NOQUOTES) . '</div>';
}
return '';
}
/**
* Return html code
*
* @return string
*/
public function __toString()
{
$this->setLanguage();
$this->setSource();
$this->setNum();
$this->setTab();
$this->setExtra();
$this->setLinks();
return '<div class="bb_code">' . $this->getHeader() . '<code class="bb_code">' . $this->geshi->parse_code() . '</code>' . $this->getFooter() . '</div>';
}
}