##// END OF EJS Templates
Added a couple of mime types so that corresponding files can be viewed in the browser....
Jean-Philippe Lang -
r1056:18066ba8bf68
parent child
Show More
@@ -0,0 +1,165
1 module CodeRay module Scanners
2
3 class PHP < Scanner
4
5 register_for :php
6
7 RESERVED_WORDS = [
8 'and', 'or', 'xor', '__FILE__', 'exception', '__LINE__', 'array', 'as', 'break', 'case',
9 'class', 'const', 'continue', 'declare', 'default',
10 'die', 'do', 'echo', 'else', 'elseif',
11 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif',
12 'endswitch', 'endwhile', 'eval', 'exit', 'extends',
13 'for', 'foreach', 'function', 'global', 'if',
14 'include', 'include_once', 'isset', 'list', 'new',
15 'print', 'require', 'require_once', 'return', 'static',
16 'switch', 'unset', 'use', 'var', 'while',
17 '__FUNCTION__', '__CLASS__', '__METHOD__', 'final', 'php_user_filter',
18 'interface', 'implements', 'extends', 'public', 'private',
19 'protected', 'abstract', 'clone', 'try', 'catch',
20 'throw', 'cfunction', 'old_function'
21 ]
22
23 PREDEFINED_CONSTANTS = [
24 'null', '$this', 'true', 'false'
25 ]
26
27 IDENT_KIND = WordList.new(:ident).
28 add(RESERVED_WORDS, :reserved).
29 add(PREDEFINED_CONSTANTS, :pre_constant)
30
31 ESCAPE = / [\$\wrbfnrtv\n\\\/'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
32 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
33
34 def scan_tokens tokens, options
35
36 state = :waiting_php
37 string_type = nil
38 regexp_allowed = true
39
40 until eos?
41
42 kind = :error
43 match = nil
44
45 if state == :initial
46
47 if scan(/ \s+ | \\\n /x)
48 kind = :space
49
50 elsif scan(/\?>/)
51 kind = :char
52 state = :waiting_php
53
54 elsif scan(%r{ (//|\#) [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) }mx)
55 kind = :comment
56 regexp_allowed = false
57
58 elsif match = scan(/ \# \s* if \s* 0 /x)
59 match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
60 kind = :comment
61 regexp_allowed = false
62
63 elsif regexp_allowed and scan(/\//)
64 tokens << [:open, :regexp]
65 state = :regex
66 kind = :delimiter
67
68 elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%] | \.(?!\d) /x)
69 kind = :operator
70 regexp_allowed=true
71
72 elsif match = scan(/ [$@A-Za-z_][A-Za-z_0-9]* /x)
73 kind = IDENT_KIND[match]
74 regexp_allowed=false
75
76 elsif match = scan(/["']/)
77 tokens << [:open, :string]
78 string_type = matched
79 state = :string
80 kind = :delimiter
81
82 elsif scan(/0[xX][0-9A-Fa-f]+/)
83 kind = :hex
84 regexp_allowed=false
85
86 elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
87 kind = :oct
88 regexp_allowed=false
89
90 elsif scan(/(?:\d+)(?![.eEfF])/)
91 kind = :integer
92 regexp_allowed=false
93
94 elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
95 kind = :float
96 regexp_allowed=false
97
98 else
99 getch
100 end
101
102 elsif state == :regex
103 if scan(/[^\\\/]+/)
104 kind = :content
105 elsif scan(/\\\/|\\/)
106 kind = :content
107 elsif scan(/\//)
108 tokens << [matched, :delimiter]
109 tokens << [:close, :regexp]
110 state = :initial
111 next
112 else
113 getch
114 kind = :content
115 end
116
117 elsif state == :string
118 if scan(/[^\\"']+/)
119 kind = :content
120 elsif scan(/["']/)
121 if string_type==matched
122 tokens << [matched, :delimiter]
123 tokens << [:close, :string]
124 state = :initial
125 string_type=nil
126 next
127 else
128 kind = :content
129 end
130 elsif scan(/ \\ (?: \S ) /mox)
131 kind = :char
132 elsif scan(/ \\ | $ /x)
133 kind = :error
134 state = :initial
135 else
136 raise "else case \" reached; %p not handled." % peek(1), tokens
137 end
138
139 elsif state == :waiting_php
140 if scan(/<\?php/m)
141 kind = :char
142 state = :initial
143 elsif scan(/[^<]+/)
144 kind = :comment
145 else
146 kind = :comment
147 getch
148 end
149 else
150 raise 'else-case reached', tokens
151
152 end
153
154 match ||= matched
155
156 tokens << [match, kind]
157
158 end
159 tokens
160
161 end
162
163 end
164
165 end end No newline at end of file
@@ -1,61 +1,66
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 module MimeType
19 module MimeType
20
20
21 MIME_TYPES = {
21 MIME_TYPES = {
22 'text/plain' => 'txt',
22 'text/plain' => 'txt,tpl,properties',
23 'text/css' => 'css',
23 'text/css' => 'css',
24 'text/html' => 'html,htm,xhtml',
24 'text/html' => 'html,htm,xhtml',
25 'text/x-c' => 'c,cpp,h',
25 'text/x-c' => 'c,cpp,h',
26 'text/x-java' => 'java',
26 'text/x-javascript' => 'js',
27 'text/x-javascript' => 'js',
27 'text/x-html-template' => 'rhtml',
28 'text/x-html-template' => 'rhtml',
29 'text/x-perl' => 'pl,pm',
30 'text/x-php' => 'php,php3,php4,php5',
31 'text/x-python' => 'py',
28 'text/x-ruby' => 'rb,rbw,ruby,rake',
32 'text/x-ruby' => 'rb,rbw,ruby,rake',
33 'text/x-sh' => 'sh',
29 'text/xml' => 'xml',
34 'text/xml' => 'xml',
30 'text/yaml' => 'yml,yaml',
35 'text/yaml' => 'yml,yaml',
31 'image/gif' => 'gif',
36 'image/gif' => 'gif',
32 'image/jpeg' => 'jpg,jpeg,jpe',
37 'image/jpeg' => 'jpg,jpeg,jpe',
33 'image/png' => 'png',
38 'image/png' => 'png',
34 'image/tiff' => 'tiff,tif'
39 'image/tiff' => 'tiff,tif'
35 }.freeze
40 }.freeze
36
41
37 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
42 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
38 exts.split(',').each {|ext| map[ext] = type}
43 exts.split(',').each {|ext| map[ext] = type}
39 map
44 map
40 end
45 end
41
46
42 # returns mime type for name or nil if unknown
47 # returns mime type for name or nil if unknown
43 def self.of(name)
48 def self.of(name)
44 return nil unless name
49 return nil unless name
45 m = name.to_s.match(/\.([^\.]+)$/)
50 m = name.to_s.match(/\.([^\.]+)$/)
46 EXTENSIONS[m[1]] if m
51 EXTENSIONS[m[1]] if m
47 end
52 end
48
53
49 def self.main_mimetype_of(name)
54 def self.main_mimetype_of(name)
50 mimetype = of(name)
55 mimetype = of(name)
51 mimetype.split('/').first if mimetype
56 mimetype.split('/').first if mimetype
52 end
57 end
53
58
54 # return true if mime-type for name is type/*
59 # return true if mime-type for name is type/*
55 # otherwise false
60 # otherwise false
56 def self.is_type?(type, name)
61 def self.is_type?(type, name)
57 main_mimetype = main_mimetype_of(name)
62 main_mimetype = main_mimetype_of(name)
58 type.to_s == main_mimetype
63 type.to_s == main_mimetype
59 end
64 end
60 end
65 end
61 end
66 end
@@ -1,190 +1,194
1 module CodeRay
1 module CodeRay
2
2
3 # = FileType
3 # = FileType
4 #
4 #
5 # A simple filetype recognizer.
5 # A simple filetype recognizer.
6 #
6 #
7 # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
7 # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
8 #
8 #
9 # License:: LGPL / ask the author
9 # License:: LGPL / ask the author
10 # Version:: 0.1 (2005-09-01)
10 # Version:: 0.1 (2005-09-01)
11 #
11 #
12 # == Documentation
12 # == Documentation
13 #
13 #
14 # # determine the type of the given
14 # # determine the type of the given
15 # lang = FileType[ARGV.first]
15 # lang = FileType[ARGV.first]
16 #
16 #
17 # # return :plaintext if the file type is unknown
17 # # return :plaintext if the file type is unknown
18 # lang = FileType.fetch ARGV.first, :plaintext
18 # lang = FileType.fetch ARGV.first, :plaintext
19 #
19 #
20 # # try the shebang line, too
20 # # try the shebang line, too
21 # lang = FileType.fetch ARGV.first, :plaintext, true
21 # lang = FileType.fetch ARGV.first, :plaintext, true
22 module FileType
22 module FileType
23
23
24 UnknownFileType = Class.new Exception
24 UnknownFileType = Class.new Exception
25
25
26 class << self
26 class << self
27
27
28 # Try to determine the file type of the file.
28 # Try to determine the file type of the file.
29 #
29 #
30 # +filename+ is a relative or absolute path to a file.
30 # +filename+ is a relative or absolute path to a file.
31 #
31 #
32 # The file itself is only accessed when +read_shebang+ is set to true.
32 # The file itself is only accessed when +read_shebang+ is set to true.
33 # That means you can get filetypes from files that don't exist.
33 # That means you can get filetypes from files that don't exist.
34 def [] filename, read_shebang = false
34 def [] filename, read_shebang = false
35 name = File.basename filename
35 name = File.basename filename
36 ext = File.extname name
36 ext = File.extname name
37 ext.sub!(/^\./, '') # delete the leading dot
37 ext.sub!(/^\./, '') # delete the leading dot
38
38
39 type =
39 type =
40 TypeFromExt[ext] ||
40 TypeFromExt[ext] ||
41 TypeFromExt[ext.downcase] ||
41 TypeFromExt[ext.downcase] ||
42 TypeFromName[name] ||
42 TypeFromName[name] ||
43 TypeFromName[name.downcase]
43 TypeFromName[name.downcase]
44 type ||= shebang(filename) if read_shebang
44 type ||= shebang(filename) if read_shebang
45
45
46 type
46 type
47 end
47 end
48
48
49 def shebang filename
49 def shebang filename
50 begin
50 begin
51 File.open filename, 'r' do |f|
51 File.open filename, 'r' do |f|
52 first_line = f.gets
52 first_line = f.gets
53 first_line[TypeFromShebang]
53 first_line[TypeFromShebang]
54 end
54 end
55 rescue IOError
55 rescue IOError
56 nil
56 nil
57 end
57 end
58 end
58 end
59
59
60 # This works like Hash#fetch.
60 # This works like Hash#fetch.
61 #
61 #
62 # If the filetype cannot be found, the +default+ value
62 # If the filetype cannot be found, the +default+ value
63 # is returned.
63 # is returned.
64 def fetch filename, default = nil, read_shebang = false
64 def fetch filename, default = nil, read_shebang = false
65 if default and block_given?
65 if default and block_given?
66 warn 'block supersedes default value argument'
66 warn 'block supersedes default value argument'
67 end
67 end
68
68
69 unless type = self[filename, read_shebang]
69 unless type = self[filename, read_shebang]
70 return yield if block_given?
70 return yield if block_given?
71 return default if default
71 return default if default
72 raise UnknownFileType, 'Could not determine type of %p.' % filename
72 raise UnknownFileType, 'Could not determine type of %p.' % filename
73 end
73 end
74 type
74 type
75 end
75 end
76
76
77 end
77 end
78
78
79 TypeFromExt = {
79 TypeFromExt = {
80 'rb' => :ruby,
80 'rb' => :ruby,
81 'rbw' => :ruby,
81 'rbw' => :ruby,
82 'rake' => :ruby,
82 'rake' => :ruby,
83 'mab' => :ruby,
83 'mab' => :ruby,
84 'cpp' => :c,
84 'cpp' => :c,
85 'c' => :c,
85 'c' => :c,
86 'h' => :c,
86 'h' => :c,
87 'js' => :javascript,
87 'js' => :javascript,
88 'xml' => :xml,
88 'xml' => :xml,
89 'htm' => :html,
89 'htm' => :html,
90 'html' => :html,
90 'html' => :html,
91 'php' => :php,
92 'php3' => :php,
93 'php4' => :php,
94 'php5' => :php,
91 'xhtml' => :xhtml,
95 'xhtml' => :xhtml,
92 'raydebug' => :debug,
96 'raydebug' => :debug,
93 'rhtml' => :rhtml,
97 'rhtml' => :rhtml,
94 'ss' => :scheme,
98 'ss' => :scheme,
95 'sch' => :scheme,
99 'sch' => :scheme,
96 'yaml' => :yaml,
100 'yaml' => :yaml,
97 'yml' => :yaml,
101 'yml' => :yaml,
98 }
102 }
99
103
100 TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
104 TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
101
105
102 TypeFromName = {
106 TypeFromName = {
103 'Rakefile' => :ruby,
107 'Rakefile' => :ruby,
104 'Rantfile' => :ruby,
108 'Rantfile' => :ruby,
105 }
109 }
106
110
107 end
111 end
108
112
109 end
113 end
110
114
111 if $0 == __FILE__
115 if $0 == __FILE__
112 $VERBOSE = true
116 $VERBOSE = true
113 eval DATA.read, nil, $0, __LINE__+4
117 eval DATA.read, nil, $0, __LINE__+4
114 end
118 end
115
119
116 __END__
120 __END__
117
121
118 require 'test/unit'
122 require 'test/unit'
119
123
120 class TC_FileType < Test::Unit::TestCase
124 class TC_FileType < Test::Unit::TestCase
121
125
122 def test_fetch
126 def test_fetch
123 assert_raise FileType::UnknownFileType do
127 assert_raise FileType::UnknownFileType do
124 FileType.fetch ''
128 FileType.fetch ''
125 end
129 end
126
130
127 assert_throws :not_found do
131 assert_throws :not_found do
128 FileType.fetch '.' do
132 FileType.fetch '.' do
129 throw :not_found
133 throw :not_found
130 end
134 end
131 end
135 end
132
136
133 assert_equal :default, FileType.fetch('c', :default)
137 assert_equal :default, FileType.fetch('c', :default)
134
138
135 stderr, fake_stderr = $stderr, Object.new
139 stderr, fake_stderr = $stderr, Object.new
136 $err = ''
140 $err = ''
137 def fake_stderr.write x
141 def fake_stderr.write x
138 $err << x
142 $err << x
139 end
143 end
140 $stderr = fake_stderr
144 $stderr = fake_stderr
141 FileType.fetch('c', :default) { }
145 FileType.fetch('c', :default) { }
142 assert_equal "block supersedes default value argument\n", $err
146 assert_equal "block supersedes default value argument\n", $err
143 $stderr = stderr
147 $stderr = stderr
144 end
148 end
145
149
146 def test_ruby
150 def test_ruby
147 assert_equal :ruby, FileType['test.rb']
151 assert_equal :ruby, FileType['test.rb']
148 assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
152 assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
149 assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
153 assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
150 assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
154 assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
151 assert_equal :ruby, FileType['./lib/tasks\repository.rake']
155 assert_equal :ruby, FileType['./lib/tasks\repository.rake']
152 assert_not_equal :ruby, FileType['test_rb']
156 assert_not_equal :ruby, FileType['test_rb']
153 assert_not_equal :ruby, FileType['Makefile']
157 assert_not_equal :ruby, FileType['Makefile']
154 assert_not_equal :ruby, FileType['set.rb/set']
158 assert_not_equal :ruby, FileType['set.rb/set']
155 assert_not_equal :ruby, FileType['~/projects/blabla/rb']
159 assert_not_equal :ruby, FileType['~/projects/blabla/rb']
156 end
160 end
157
161
158 def test_c
162 def test_c
159 assert_equal :c, FileType['test.c']
163 assert_equal :c, FileType['test.c']
160 assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
164 assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
161 assert_not_equal :c, FileType['test_c']
165 assert_not_equal :c, FileType['test_c']
162 assert_not_equal :c, FileType['Makefile']
166 assert_not_equal :c, FileType['Makefile']
163 assert_not_equal :c, FileType['set.h/set']
167 assert_not_equal :c, FileType['set.h/set']
164 assert_not_equal :c, FileType['~/projects/blabla/c']
168 assert_not_equal :c, FileType['~/projects/blabla/c']
165 end
169 end
166
170
167 def test_html
171 def test_html
168 assert_equal :html, FileType['test.htm']
172 assert_equal :html, FileType['test.htm']
169 assert_equal :xhtml, FileType['test.xhtml']
173 assert_equal :xhtml, FileType['test.xhtml']
170 assert_equal :xhtml, FileType['test.html.xhtml']
174 assert_equal :xhtml, FileType['test.html.xhtml']
171 assert_equal :rhtml, FileType['_form.rhtml']
175 assert_equal :rhtml, FileType['_form.rhtml']
172 end
176 end
173
177
174 def test_yaml
178 def test_yaml
175 assert_equal :yaml, FileType['test.yml']
179 assert_equal :yaml, FileType['test.yml']
176 assert_equal :yaml, FileType['test.yaml']
180 assert_equal :yaml, FileType['test.yaml']
177 assert_equal :yaml, FileType['my.html.yaml']
181 assert_equal :yaml, FileType['my.html.yaml']
178 assert_not_equal :yaml, FileType['YAML']
182 assert_not_equal :yaml, FileType['YAML']
179 end
183 end
180
184
181 def test_shebang
185 def test_shebang
182 dir = './test'
186 dir = './test'
183 if File.directory? dir
187 if File.directory? dir
184 Dir.chdir dir do
188 Dir.chdir dir do
185 assert_equal :c, FileType['test.c']
189 assert_equal :c, FileType['test.c']
186 end
190 end
187 end
191 end
188 end
192 end
189
193
190 end
194 end
General Comments 0
You need to be logged in to leave comments. Login now