summaryrefslogtreecommitdiff
path: root/lib/viewers
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-06-04 10:17:52 (GMT)
committerAleksander Machniak <alec@alec.pl>2013-06-04 10:17:52 (GMT)
commit212e68eb6f0b9f759fcc806428f3c115fbbb5cff (patch)
tree8394424ef3e0f6013b055ed70bc19f9b352e1e26 /lib/viewers
parenta1571ae31a5c6df9af5d81c47bd9ffc02c47c848 (diff)
downloadkolab-chwala-212e68eb6f0b9f759fcc806428f3c115fbbb5cff.tar.gz
Improve text editor mode selection
Diffstat (limited to 'lib/viewers')
-rw-r--r--lib/viewers/text.php37
1 files changed, 36 insertions, 1 deletions
diff --git a/lib/viewers/text.php b/lib/viewers/text.php
index 4444af8..05fb9c0 100644
--- a/lib/viewers/text.php
+++ b/lib/viewers/text.php
@@ -48,6 +48,19 @@ class file_viewer_text extends file_viewer
'message/rfc822' => 'text',
);
+ /**
+ * File extension to highligter mode mapping
+ *
+ * @var array
+ */
+ protected $extensions = array(
+ 'php' => '/^(php|phpt|inc)$/',
+ 'html' => '/^html?$/',
+ 'css' => '/^css$/',
+ 'xml' => '/^xml$/',
+ 'javascript' => '/^js$/',
+ 'sh' => '/^sh$/',
+ );
/**
* Returns list of supported mimetype
@@ -115,7 +128,7 @@ class file_viewer_text extends file_viewer
*/
public function output($file, $mimetype = null)
{
- $mode = $this->mimetypes[$mimetype] ?: 'text';
+ $mode = $this->get_mode($mimetype, $file);
echo '<!DOCTYPE html>
<html>
@@ -143,6 +156,28 @@ class file_viewer_text extends file_viewer
</body>
</html>";
}
+
+ protected function get_mode($mimetype, $filename)
+ {
+ $mimetype = strtolower($mimetype);
+
+ if ($this->mimetypes[$mimetype]) {
+ return $this->mimetypes[$mimetype];
+ }
+
+ $filename = explode('.', $filename);
+ $extension = count($filename) > 1 ? array_pop($filename) : null;
+
+ if ($extension) {
+ foreach ($this->extensions as $mode => $regexp) {
+ if (preg_match($regexp, $extension)) {
+ return $mode;
+ }
+ }
+ }
+
+ return 'text';
+ }
}