summaryrefslogtreecommitdiff
path: root/vendor/kzykhys/pygments/src/KzykHys/Pygments/Pygments.php
blob: 6ebd6522ae02d737e61adf50dd90271aab784495 (plain)
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
<?php

namespace KzykHys\Pygments;

use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

/**
 * Pygments.php - A Thin Wrapper for the Python Pygments
 *
 * @author Kazuyuki Hayashi <hayashi@valnur.net>
 */
class Pygments
{

    /**
     * @var string
     */
    private $pygmentize;

    /**
     * Constructor
     *
     * @param string $pygmentize The path to pygmentize command
     */
    public function __construct($pygmentize = 'pygmentize')
    {
        $this->pygmentize = $pygmentize;
    }

    /**
     * Highlight the input code
     *
     * @param string $code      The code to highlight
     * @param string $lexer     The name of the lexer (php, html,...)
     * @param string $formatter The name of the formatter (html, ansi,...)
     * @param array  $options   An array of options
     *
     * @return string
     */
    public function highlight($code, $lexer = null, $formatter = null, $options = array())
    {
        $builder = $this->createProcessBuilder();

        if ($lexer) {
            $builder->add('-l')->add($lexer);
        } else {
            $builder->add('-g');
        }

        if ($formatter) {
            $builder->add('-f')->add($formatter);
        }

        if (count($options)) {
            $arg = array();

            foreach ($options as $key => $value) {
                $arg[] = sprintf('%s=%s', $key, $value);
            }

            $builder->add('-O')->add(implode(',', $arg));
        }

        $process = $builder->getProcess()->setStdin($code);

        return $this->getOutput($process);
    }

    /**
     * Gets style definition
     *
     * @param string $style    The name of the style (default, colorful,...)
     * @param string $selector The css selector
     *
     * @return string
     */
    public function getCss($style = 'default', $selector = null)
    {
        $builder = $this->createProcessBuilder();
        $builder->add('-f')->add('html');
        $builder->add('-S')->add($style);

        if ($selector) {
            $builder->add('-a')->add($selector);
        }

        return $this->getOutput($builder->getProcess());
    }

    /**
     * Guesses a lexer name based solely on the given filename
     *
     * @param string $fileName The file does not need to exist, or be readable.
     *
     * @return string
     */
    public function guessLexer($fileName)
    {
        $process = $this->createProcessBuilder()
            ->setArguments(array('-N', $fileName))
            ->getProcess();

        return trim($this->getOutput($process));
    }

    /**
     * Gets a list of lexers
     *
     * @return array
     */
    public function getLexers()
    {
        $process = $this->createProcessBuilder()
            ->setArguments(array('-L', 'lexer'))
            ->getProcess();

        $output = $this->getOutput($process);

        return $this->parseList($output);
    }

    /**
     * Gets a list of formatters
     *
     * @return array
     */
    public function getFormatters()
    {
        $process = $this->createProcessBuilder()
            ->setArguments(array('-L', 'formatter'))
            ->getProcess();

        $output = $this->getOutput($process);

        return $this->parseList($output);
    }

    /**
     * Gets a list of styles
     *
     * @return array
     */
    public function getStyles()
    {
        $process = $this->createProcessBuilder()
            ->setArguments(array('-L', 'style'))
            ->getProcess();

        $output = $this->getOutput($process);

        return $this->parseList($output);
    }

    /**
     * @return ProcessBuilder
     */
    protected function createProcessBuilder()
    {
        return ProcessBuilder::create()->setPrefix($this->pygmentize);
    }

    /**
     * @param Process $process
     * @throws \RuntimeException
     * @return string
     */
    protected function getOutput(Process $process)
    {
        $process->run();

        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }

        return $process->getOutput();
    }

    /**
     * @param string $input
     * @return array
     */
    protected function parseList($input)
    {
        $list = array();

        if (preg_match_all('/^\* (.*?):\r?\n *([^\r\n]*?)$/m', $input, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $names = explode(',', $match[1]);

                foreach ($names as $name) {
                    $list[trim($name)] = $match[2];
                }
            }
        }

        return $list;
    }

}