1: <?php
2: /**
3: * The Horde_Mime_Viewer_Syntaxhighlighter class renders source code appropriate
4: * for highlighting with http://alexgorbatchev.com/SyntaxHighlighter/.
5: *
6: * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Chuck Hagenbuch <chuck@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Mime_Viewer
15: */
16: class Horde_Mime_Viewer_Syntaxhighlighter extends Horde_Mime_Viewer_Base
17: {
18: /**
19: * This driver's display capabilities.
20: *
21: * @var array
22: */
23: protected $_capability = array(
24: 'full' => true,
25: 'info' => false,
26: 'inline' => true,
27: 'raw' => false,
28: );
29:
30: /**
31: * Return the full rendered version of the Horde_Mime_Part object.
32: *
33: * @return array See parent::render().
34: */
35: protected function _render()
36: {
37: return $this->_renderFullReturn($this->_renderInline());
38: }
39:
40: /**
41: * Return the rendered inline version of the Horde_Mime_Part object.
42: *
43: * @return array See parent::render().
44: */
45: protected function _renderInline()
46: {
47: /* Determine the language and brush from the mime type. */
48: $mimeType = $this->_mimepart->getType();
49: $language = $this->_mimeTypeToLanguage($mimeType);
50:
51: $results = '<pre class="brush: ' . $language . '">' . htmlspecialchars($this->_mimepart->getContents()) . '</pre>';
52: return $this->_renderReturn(
53: $results,
54: 'text/html; charset=UTF-8'
55: );
56: }
57:
58: protected function _getLanguageOptions($language)
59: {
60: if ($language == 'php') {
61: return 'html-script: true';
62: }
63: }
64:
65: /**
66: * Attempts to determine what mode to use for the source-highlight
67: * program from a MIME type.
68: *
69: * @param string $type The MIME type.
70: *
71: * @return string The mode to use.
72: */
73: protected function _mimeTypeToLanguage($type)
74: {
75: $type = str_replace('x-unknown', 'x-extension', $type);
76:
77: switch ($type) {
78: case 'application/javascript':
79: case 'application/x-javascript':
80: case 'application/x-extension-javascript':
81: case 'application/x-extension-js':
82: return 'js';
83:
84: case 'application/x-perl':
85: case 'application/x-extension-pl':
86: return 'perl';
87:
88: case 'application/x-php':
89: case 'application/x-extension-php':
90: case 'application/x-extension-php3':
91: case 'application/x-extension-phps':
92: case 'application/x-extension-php3s':
93: case 'application/x-httpd-php':
94: case 'application/x-httpd-php3':
95: case 'application/x-httpd-phps':
96: return 'php';
97:
98: case 'application/x-python':
99: return 'python';
100:
101: case 'application/x-ruby':
102: return 'ruby';
103:
104: case 'application/x-sh':
105: case 'application/x-shellscript':
106: case 'application/x-extension-bash':
107: case 'application/x-extension-sh':
108: return 'bash';
109:
110: case 'application/xml':
111: case 'text/xml':
112: case 'text/xslt':
113: case 'text/html':
114: case 'text/xhtml':
115: case 'application/xhtml':
116: return 'xml';
117:
118: case 'text/css':
119: case 'application/x-extension-css':
120: return 'css';
121:
122: case 'text/diff':
123: case 'text/x-diff':
124: case 'text/x-patch':
125: return 'diff';
126:
127: case 'text/cpp':
128: case 'text/x-c++':
129: case 'text/x-c++src':
130: case 'text/x-c++hdr':
131: case 'text/x-c':
132: case 'text/x-chdr':
133: case 'text/x-csrc':
134: return 'cpp';
135:
136: case 'text/x-java':
137: return 'java';
138:
139: case 'text/x-sql':
140: return 'sql';
141:
142: case 'application/x-extension-cs':
143: return 'csharp';
144:
145: case 'application/x-extension-vb':
146: case 'application/x-extension-vba':
147: return 'vb';
148:
149: default:
150: return 'plain';
151: }
152: }
153:
154: protected function _languageToBrush($language)
155: {
156: switch ($language) {
157: case 'bash':
158: case 'sh':
159: case 'shell':
160: return 'Bash';
161:
162: case 'csharp':
163: return 'Csharp';
164:
165: case 'c':
166: case 'cpp':
167: return 'Cpp';
168:
169: case 'css':
170: return 'Css';
171:
172: case 'diff':
173: case 'patch':
174: case 'pas':
175: return 'Diff';
176:
177: case 'java':
178: return 'Java';
179:
180: case 'js':
181: case 'jscript':
182: case 'javascript':
183: return 'JScript';
184:
185: case 'perl':
186: return 'Perl';
187:
188: case 'php':
189: return 'Php';
190:
191: case 'python':
192: return 'Python';
193:
194: case 'ruby':
195: return 'Ruby';
196:
197: case 'sql':
198: return 'Sql';
199:
200: case 'vb':
201: return 'Vb';
202:
203: case 'xml':
204: case 'html':
205: case 'xhtml':
206: case 'xslt':
207: return 'Xml';
208:
209: default:
210: return 'Plain';
211: }
212: }
213: }
214: