|
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\Formatter; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
|
19
|
|
|
use Kadet\Highlighter\Parser\Tokens; |
|
20
|
|
|
use Kadet\Highlighter\Utils\ArrayHelper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class LateXFormatter |
|
24
|
|
|
* |
|
25
|
|
|
* @package Kadet\Highlighter\Formatter |
|
26
|
|
|
*/ |
|
27
|
|
|
class LaTeXFormatter implements FormatterInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private $_styles; |
|
30
|
|
|
|
|
31
|
1 |
|
public function __construct($styles = false) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->_styles = $styles ?: include __DIR__.'/../Styles/LaTeX/Default.php'; |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function format(Tokens $tokens) |
|
37
|
|
|
{ |
|
38
|
|
|
$source = $tokens->getSource(); |
|
39
|
|
|
|
|
40
|
|
|
$result = ''; |
|
41
|
|
|
$last = 0; |
|
42
|
|
|
|
|
43
|
|
|
/** @var Token $token */ |
|
44
|
|
|
foreach ($tokens as $token) { |
|
45
|
|
|
list($openTag, $closeTag) = $this->getOpenCloseTags($token); |
|
46
|
|
|
$result .= $this->escape(substr($source, $last, $token->pos - $last)); |
|
47
|
|
|
$result .= $token->isStart() ? $openTag : $closeTag; |
|
48
|
|
|
|
|
49
|
|
|
$last = $token->pos; |
|
50
|
|
|
} |
|
51
|
|
|
$result .= substr($source, $last); |
|
52
|
|
|
|
|
53
|
|
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function escape($token) |
|
57
|
|
|
{ |
|
58
|
|
|
$replace = [ |
|
59
|
|
|
'\\' => '\\textbackslash{}', |
|
60
|
|
|
'{' => '\\{', |
|
61
|
|
|
'}' => '\\}', |
|
62
|
|
|
// When there is a \ in the source, it gets translated to |
|
63
|
|
|
// \textasciibackslash{}, but then the { and } are escaped to \{ |
|
64
|
|
|
// and \}. This substitution reverts this. |
|
65
|
|
|
'\\textbackslash\\{\\}' => '\\textbackslash{}', |
|
66
|
|
|
'%' => '\\%', |
|
67
|
|
|
'_' => '\\_', |
|
68
|
|
|
'^' => '\\textasciicircum{}', |
|
69
|
|
|
'~' => '\\textasciitilde{}', |
|
70
|
|
|
'$' => '\\$', |
|
71
|
|
|
'&' => '\\&', |
|
72
|
|
|
'<' => '\\textless{}', |
|
73
|
|
|
'>' => '\\textgreater{}', |
|
74
|
|
|
'>' => '\\textgreater{}', |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
// We can do just with a simple str_replace() because PHP promises to |
|
78
|
|
|
// process them sequentially: |
|
79
|
|
|
// https://secure.php.net/manual/en/function.str-replace.php#refsect1-function.str-replace-parameters |
|
80
|
|
|
return str_replace( |
|
81
|
|
|
array_keys($replace), |
|
82
|
|
|
array_values($replace), |
|
83
|
|
|
$token |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getOpenCloseTags($token) |
|
88
|
|
|
{ |
|
89
|
|
|
$openTag = $closeTag = ''; |
|
90
|
|
|
$style = $this->getStyle($token); |
|
91
|
|
|
|
|
92
|
|
|
if (ArrayHelper::get($style, 'italic', false)) { |
|
93
|
|
|
$openTag .= '\\textit{'; |
|
94
|
|
|
$closeTag .= '}'; |
|
95
|
|
|
} |
|
96
|
|
|
if (ArrayHelper::get($style, 'bold', false)) { |
|
97
|
|
|
$openTag .= '\\textbf{'; |
|
98
|
|
|
$closeTag .= '}'; |
|
99
|
|
|
} |
|
100
|
|
|
if (($color = ArrayHelper::get($style, 'color', 'default')) !== 'default') { |
|
101
|
|
|
$openTag .= sprintf('\\textcolor{%s}{', $style['color']); |
|
102
|
|
|
$closeTag .= '}'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return [$openTag, $closeTag]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function getStyle($token) |
|
109
|
|
|
{ |
|
110
|
|
|
return ArrayHelper::resolve($this->_styles, $token->name, []); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|