1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter; |
17
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Formatter\CliFormatter; |
19
|
|
|
use Kadet\Highlighter\Formatter\FormatterInterface; |
20
|
|
|
use Kadet\Highlighter\Formatter\HtmlFormatter; |
21
|
|
|
use Kadet\Highlighter\Language\GreedyLanguage; |
22
|
|
|
use Kadet\Highlighter\Language\Language; |
23
|
|
|
use Kadet\Highlighter\Language\PlainText; |
24
|
|
|
use Kadet\Highlighter\Utils\Singleton; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* KeyLighter helper class, used to simplify usage. |
28
|
|
|
* |
29
|
|
|
* @package Kadet\Highlighter |
30
|
|
|
*/ |
31
|
|
|
class KeyLighter |
32
|
|
|
{ |
33
|
|
|
use Singleton; |
34
|
|
|
|
35
|
|
|
const VERSION = '0.8.0-dev'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Registered aliases |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $_languages = [ |
43
|
|
|
'name' => [], |
44
|
|
|
'mime' => [], |
45
|
|
|
'extension' => [] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** @var FormatterInterface */ |
49
|
|
|
private $_formatter = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* |
54
|
|
|
* @return Language |
55
|
|
|
*/ |
56
|
2 |
|
public function getLanguage($name) |
57
|
|
|
{ |
58
|
2 |
|
$embedded = []; |
59
|
2 |
|
if (($pos = strpos($name, '>')) !== false) { |
60
|
1 |
|
$embedded[] = self::getLanguage(trim(substr($name, $pos + 1))); |
61
|
1 |
|
$name = trim(substr($name, 0, $pos)); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
2 |
|
return $this->languageByName($name, [ |
65
|
|
|
'embedded' => $embedded |
66
|
2 |
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function languageByName($name, $params = []) |
70
|
|
|
{ |
71
|
4 |
|
return isset($this->_languages['name'][$name]) ? |
72
|
4 |
|
$this->_languages['name'][$name]($params) : |
73
|
4 |
|
new PlainText($params); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function languageByMime($mime, $params = []) |
77
|
|
|
{ |
78
|
1 |
|
return isset($this->_languages['mime'][$mime]) ? |
79
|
1 |
|
$this->_languages['mime'][$mime]($params) : |
80
|
1 |
|
new PlainText($params); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function languageByExt($filename, $params = []) |
84
|
|
|
{ |
85
|
1 |
|
foreach($this->_languages['extension'] as $mask => $class) { |
86
|
1 |
|
if(fnmatch($mask, $filename)) { |
87
|
1 |
|
return $class($params); |
88
|
|
|
} |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
return new PlainText($params); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param callable|string $language |
96
|
|
|
* @param array[string] $aliases |
97
|
|
|
* |
98
|
|
|
* @deprecated Will be removed in 1.0 |
99
|
|
|
*/ |
100
|
|
|
public function registerLanguage($language, $aliases) |
101
|
|
|
{ |
102
|
|
|
$this->register($language, ['name' => $aliases]); |
103
|
|
|
} |
104
|
|
|
|
105
|
11 |
|
public function setDefaultFormatter(FormatterInterface $formatter) |
106
|
|
|
{ |
107
|
11 |
|
$this->_formatter = $formatter; |
108
|
11 |
|
} |
109
|
|
|
|
110
|
3 |
|
public function registeredLanguages($by = 'name') |
111
|
|
|
{ |
112
|
|
|
return array_map(function ($e) { |
113
|
3 |
|
return get_class($e()); |
114
|
3 |
|
}, $this->_languages[$by]); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
public function getDefaultFormatter() |
118
|
|
|
{ |
119
|
1 |
|
return $this->_formatter; |
120
|
|
|
} |
121
|
|
|
|
122
|
4 |
|
public function highlight($source, $language, FormatterInterface $formatter = null) |
123
|
|
|
{ |
124
|
4 |
|
$formatter = $formatter ?: $this->getDefaultFormatter(); |
125
|
|
|
|
126
|
4 |
|
if (!$language instanceof GreedyLanguage) { |
127
|
1 |
|
$language = $this->getLanguage($language); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
4 |
|
return $formatter->format($language->parse($source)); |
131
|
|
|
} |
132
|
|
|
|
133
|
11 |
|
public function __construct() |
134
|
|
|
{ |
135
|
11 |
|
$this->setDefaultFormatter( |
136
|
11 |
|
php_sapi_name() === 'cli' ? new CliFormatter() : new HtmlFormatter() |
137
|
11 |
|
); |
138
|
11 |
|
} |
139
|
|
|
|
140
|
1 |
|
public function init() |
141
|
|
|
{ |
142
|
1 |
|
foreach(include __DIR__.'/Config/aliases.php' as $alias) { |
143
|
1 |
|
$class = $alias[0]; |
144
|
1 |
|
unset($alias[0]); |
145
|
|
|
|
146
|
1 |
|
$this->register($class, $alias); |
147
|
1 |
|
} |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string|callable $class |
152
|
|
|
* @param array $options |
153
|
|
|
*/ |
154
|
10 |
|
public function register($class, array $options) |
155
|
|
|
{ |
156
|
10 |
|
if(!is_callable($class) && is_subclass_of($class, Language::class)) { |
|
|
|
|
157
|
10 |
|
$class = function($arguments = []) use ($class) { |
158
|
9 |
|
return new $class($arguments); |
159
|
10 |
|
}; |
160
|
10 |
|
} |
161
|
|
|
|
162
|
10 |
|
foreach($options as $name => $values) { |
163
|
10 |
|
$this->_languages[$name] = array_merge($this->_languages[$name], array_fill_keys($values, $class)); |
164
|
10 |
|
} |
165
|
10 |
|
} |
166
|
|
|
} |
167
|
|
|
|