summaryrefslogtreecommitdiffstats
path: root/application/libraries/Pygments.php
blob: 3c99481d17c4b0fcfe90c79a419ebd38ab445684 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace libraries;

class Pygments {
	private $file;
	private $mimetype;
	private $filename;

	public function __construct($file, $mimetype, $filename) {
		$this->file = $file;
		$this->mimetype = $mimetype;
		$this->filename = $filename;
	}

	private static function get_pygments_info() {
		return cache_function_full('pygments_info', 1800, function() {
			ob_start();
			passthru("python ".escapeshellarg(FCPATH."scripts/get_lexer_list.py"));
			$output = ob_get_clean();

			return json_decode($output, true);
		});
	}

	public static function get_lexers() {
		return cache_function('lexers', 1800, function() {
			$last_desc = "";

			foreach (self::get_pygments_info() as $lexer) {
				$desc = $lexer['fullname'];
				$name = array_keys($lexer['names'])[0];
				if ($desc == $last_desc) {
					continue;
				}
				$last_desc = $desc;
				$lexers[$name] = $desc;
			}
			$lexers["text"] = "Plain text";
			return $lexers;
		});
	}

	// Allow certain types to be highlight without doing it automatically
	public function should_highlight()
	{
		$typearray = array(
			'image/svg+xml',
		);
		if (in_array($this->mimetype, $typearray)) return false;

		if ($this->mime2lexer($this->mimetype)) return true;

		return false;
	}

	public function can_highlight()
	{
		if ($this->mime2lexer($this->mimetype)) return true;

		return false;
	}

	// Return the lexer that should be used for highlighting
	public function autodetect_lexer()
	{
		if (!$this->should_highlight()) {
			return false;
		}

		$lexer = $this->mime2lexer($this->mimetype);

		// filename lexers overwrite mime type mappings
		$filename_lexer = $this->filename2lexer();
		if ($filename_lexer) {
			return $filename_lexer;
		}

		return $lexer;
	}

	// Map MIME types to lexers needed for highlighting
	private function mime2lexer()
	{
		$typearray = array(
		'application/javascript' => 'javascript',
		'application/mbox' => 'text',
		'application/postscript' => 'postscript',
		'application/smil' => 'ocaml',
		'application/x-applix-spreadsheet' => 'actionscript',
		'application/x-awk' => 'awk',
		'application/x-desktop' => 'text',
		'application/x-fluid' => 'text',
		'application/x-genesis-rom' => 'text',
		'application/x-java' => 'java',
		'application/x-m4' => 'text',
		'application/xml-dtd' => "xml",
		'application/xml' => 'xml',
		'application/x-perl' => 'perl',
		'application/x-php' => 'php',
		'application/x-ruby' => 'ruby',
		'application/x-shellscript' => 'bash',
		'application/xslt+xml' => "xml",
		'application/x-x509-ca-cert' => 'text',
		'message/rfc822' => 'text',
		'text/css' => 'css',
		'text/html' => 'xml',
		'text/plain-ascii' => 'ascii',
		'text/plain' => 'text',
		'text/troff' => 'groff',
		'text/x-asm' => 'nasm',
		'text/x-awk' => 'awk',
		'text/x-c' => 'c',
		'text/x-c++' => 'cpp',
		'text/x-c++hdr' => 'c',
		'text/x-chdr' => 'c',
		'text/x-csrc' => 'c',
		'text/x-c++src' => 'cpp',
		'text/x-diff' => 'diff',
		'text/x-gawk' => 'awk',
		'text/x-haskell' => 'haskell',
		'text/x-java' => 'java',
		'text/x-lisp' => 'cl',
		'text/x-literate-haskell' => 'haskell',
		'text/x-lua' => 'lua',
		'text/x-makefile' => 'make',
		'text/x-ocaml' => 'ocaml',
		'text/x-patch' => 'diff',
		'text/x-perl' => 'perl',
		'text/x-php' => 'php',
		'text/x-python' => 'python',
		'text/x-ruby' => 'ruby',
		'text/x-scheme' => 'scheme',
		'text/x-shellscript' => 'bash',
		'text/x-subviewer' => 'bash',
		'text/x-tcl' => 'tcl',
		'text/x-tex' => 'tex',
		);
		if (array_key_exists($this->mimetype, $typearray)) return $typearray[$this->mimetype];

		// fall back to pygments own list if not found in our list
		foreach (self::get_pygments_info() as $lexer) {
			if (isset($lexer['mimetypes'][$this->mimetype])) {
				return $lexer;
			}
		}

		if (strpos($this->mimetype, 'text/') === 0) return 'text';

		# default
		return false;
	}

	// Map special filenames to lexers
	private function filename2lexer()
	{
		$namearray = array(
			'PKGBUILD' => 'bash',
			'.vimrc' => 'vim'
		);
		if (array_key_exists($this->filename, $namearray)) return $namearray[$this->filename];


		if (strpos($this->filename, ".") !== false) {
			$extension = substr($this->filename, strrpos($this->filename, ".") + 1);

			$extensionarray = array(
				'awk' => 'awk',
				'c' => 'c',
				'coffee' => 'coffee-script',
				'cpp' => 'cpp',
				'diff' => 'diff',
				'h' => 'c',
				'hs' => 'haskell',
				'html' => 'xml',
				'java' => 'java',
				'js' => 'js',
				'lua' => 'lua',
				'mli' => 'ocaml',
				'mll' => 'ocaml',
				'ml' => 'ocaml',
				'mly' => 'ocaml',
				'patch' => 'diff',
				'php' => 'php',
				'pl' => 'perl',
				'pp' => 'puppet',
				'py' => 'python',
				'rb' => 'ruby',
				's' => 'asm',
				'sh' => 'bash',
				'tcl' => 'tcl',
				'tex' => 'tex',
			);
			if (array_key_exists($extension, $extensionarray)) return $extensionarray[$extension];
		}

		return false;
	}

	// Handle lexer aliases
	public function resolve_lexer_alias($alias)
	{
		if ($alias === false) return false;
		$aliasarray = array(
			'py' => 'python',
			'sh' => 'bash',
			's' => 'asm',
			'pl' => 'perl'
		);
		if (array_key_exists($alias, $aliasarray)) return $aliasarray[$alias];

		return $alias;
	}

}