Completed
Push — master ( edcce7...6aac40 )
by Kacper
04:23
created

LatexFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 * @author Olgierd Grzyb <[email protected]>
9
 *
10
 * Contact with author:
11
 * Xmpp:   [email protected]
12
 * E-mail: [email protected]
13
 *
14
 * From Kadet with love.
15
 */
16
17
namespace Kadet\Highlighter\Formatter;
18
19
use Kadet\Highlighter\Parser\Token\Token;
20
use Kadet\Highlighter\Parser\Tokens;
21
use Kadet\Highlighter\Utils\ArrayHelper;
22
23
/**
24
 * Class LatexFormatter
25
 *
26
 * @package Kadet\Highlighter\Formatter
27
 */
28
class LatexFormatter extends AbstractFormatter implements FormatterInterface
29
{
30
    private $_styles;
31
32
    /**
33
     * LatexFormatter constructor.
34
     *
35
     * @param array $options
36
     */
37 View Code Duplication
    public function __construct(array $options = [])
38
    {
39
        parent::__construct(array_replace_recursive([
40
            'styles' => include __DIR__.'/../Styles/Cli/Default.php'
41
        ], $options));
42
43
        $this->_styles = $this->_options['styles'];
44
    }
45
46
    protected function token(Token $token)
47
    {
48
        list($openTag, $closeTag) = $this->getOpenCloseTags($token);
49
50
        return $token->isStart() ? $openTag : $closeTag;
51
    }
52
53
    protected function content($token)
54
    {
55
        $replace = [
56
            '\\' => '\\textbackslash{}',
57
            '{' => '\\{',
58
            '}' => '\\}',
59
            // When there is a \ in the source, it gets translated to
60
            // \textasciibackslash{}, but then the { and } are escaped to \{
61
            // and \}. This substitution reverts this.
62
            '\\textbackslash\\{\\}' => '\\textbackslash{}',
63
            '%' => '\\%',
64
            '_' => '\\_',
65
            '^' => '\\textasciicircum{}',
66
            '~' => '\\textasciitilde{}',
67
            '$' => '\\$',
68
            '&' => '\\&',
69
            '<' => '\\textless{}',
70
            '>' => '\\textgreater{}',
71
        ];
72
73
        // We can do just with a simple str_replace() because PHP promises to
74
        // process them sequentially:
75
        // https://secure.php.net/manual/en/function.str-replace.php#refsect1-function.str-replace-parameters
76
        return str_replace(
77
            array_keys($replace),
78
            array_values($replace),
79
            $token
80
        );
81
    }
82
83
    protected function getOpenCloseTags($token)
84
    {
85
        $openTag = $closeTag = '';
86
        $style = $this->getStyle($token);
87
88
        if (ArrayHelper::get($style, 'bold', false)) {
89
            $openTag .= '\\textbf{';
90
            $closeTag .= '}';
91
        }
92
93
        if (ArrayHelper::get($style, 'italic', false)) {
94
            $openTag .= '\\textsl{';
95
            $closeTag .= '}';
96
        }
97
98
        if (ArrayHelper::get($style, 'underline', false)) {
99
            $openTag .= '\\underline{';
100
            $closeTag .= '}';
101
        }
102
103
        if (($color = ArrayHelper::get($style, 'color', 'default')) !== 'default') {
104
            $openTag .= sprintf('\\textcolor{%s}{', $style['color']);
105
            $closeTag .= '}';
106
        }
107
108
        return [$openTag, $closeTag];
109
    }
110
111
    protected function getStyle($token)
112
    {
113
        return ArrayHelper::resolve($this->_styles, $token->name, []);
114
    }
115
}
116