##// 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 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module MimeType
20 20
21 21 MIME_TYPES = {
22 'text/plain' => 'txt',
22 'text/plain' => 'txt,tpl,properties',
23 23 'text/css' => 'css',
24 24 'text/html' => 'html,htm,xhtml',
25 25 'text/x-c' => 'c,cpp,h',
26 'text/x-java' => 'java',
26 27 'text/x-javascript' => 'js',
27 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 32 'text/x-ruby' => 'rb,rbw,ruby,rake',
33 'text/x-sh' => 'sh',
29 34 'text/xml' => 'xml',
30 35 'text/yaml' => 'yml,yaml',
31 36 'image/gif' => 'gif',
32 37 'image/jpeg' => 'jpg,jpeg,jpe',
33 38 'image/png' => 'png',
34 39 'image/tiff' => 'tiff,tif'
35 40 }.freeze
36 41
37 42 EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|
38 43 exts.split(',').each {|ext| map[ext] = type}
39 44 map
40 45 end
41 46
42 47 # returns mime type for name or nil if unknown
43 48 def self.of(name)
44 49 return nil unless name
45 50 m = name.to_s.match(/\.([^\.]+)$/)
46 51 EXTENSIONS[m[1]] if m
47 52 end
48 53
49 54 def self.main_mimetype_of(name)
50 55 mimetype = of(name)
51 56 mimetype.split('/').first if mimetype
52 57 end
53 58
54 59 # return true if mime-type for name is type/*
55 60 # otherwise false
56 61 def self.is_type?(type, name)
57 62 main_mimetype = main_mimetype_of(name)
58 63 type.to_s == main_mimetype
59 64 end
60 65 end
61 66 end
@@ -1,190 +1,194
1 1 module CodeRay
2 2
3 3 # = FileType
4 4 #
5 5 # A simple filetype recognizer.
6 6 #
7 7 # Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>
8 8 #
9 9 # License:: LGPL / ask the author
10 10 # Version:: 0.1 (2005-09-01)
11 11 #
12 12 # == Documentation
13 13 #
14 14 # # determine the type of the given
15 15 # lang = FileType[ARGV.first]
16 16 #
17 17 # # return :plaintext if the file type is unknown
18 18 # lang = FileType.fetch ARGV.first, :plaintext
19 19 #
20 20 # # try the shebang line, too
21 21 # lang = FileType.fetch ARGV.first, :plaintext, true
22 22 module FileType
23 23
24 24 UnknownFileType = Class.new Exception
25 25
26 26 class << self
27 27
28 28 # Try to determine the file type of the file.
29 29 #
30 30 # +filename+ is a relative or absolute path to a file.
31 31 #
32 32 # The file itself is only accessed when +read_shebang+ is set to true.
33 33 # That means you can get filetypes from files that don't exist.
34 34 def [] filename, read_shebang = false
35 35 name = File.basename filename
36 36 ext = File.extname name
37 37 ext.sub!(/^\./, '') # delete the leading dot
38 38
39 39 type =
40 40 TypeFromExt[ext] ||
41 41 TypeFromExt[ext.downcase] ||
42 42 TypeFromName[name] ||
43 43 TypeFromName[name.downcase]
44 44 type ||= shebang(filename) if read_shebang
45 45
46 46 type
47 47 end
48 48
49 49 def shebang filename
50 50 begin
51 51 File.open filename, 'r' do |f|
52 52 first_line = f.gets
53 53 first_line[TypeFromShebang]
54 54 end
55 55 rescue IOError
56 56 nil
57 57 end
58 58 end
59 59
60 60 # This works like Hash#fetch.
61 61 #
62 62 # If the filetype cannot be found, the +default+ value
63 63 # is returned.
64 64 def fetch filename, default = nil, read_shebang = false
65 65 if default and block_given?
66 66 warn 'block supersedes default value argument'
67 67 end
68 68
69 69 unless type = self[filename, read_shebang]
70 70 return yield if block_given?
71 71 return default if default
72 72 raise UnknownFileType, 'Could not determine type of %p.' % filename
73 73 end
74 74 type
75 75 end
76 76
77 77 end
78 78
79 79 TypeFromExt = {
80 80 'rb' => :ruby,
81 81 'rbw' => :ruby,
82 82 'rake' => :ruby,
83 83 'mab' => :ruby,
84 84 'cpp' => :c,
85 85 'c' => :c,
86 86 'h' => :c,
87 87 'js' => :javascript,
88 88 'xml' => :xml,
89 89 'htm' => :html,
90 90 'html' => :html,
91 'php' => :php,
92 'php3' => :php,
93 'php4' => :php,
94 'php5' => :php,
91 95 'xhtml' => :xhtml,
92 96 'raydebug' => :debug,
93 97 'rhtml' => :rhtml,
94 98 'ss' => :scheme,
95 99 'sch' => :scheme,
96 100 'yaml' => :yaml,
97 101 'yml' => :yaml,
98 102 }
99 103
100 104 TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
101 105
102 106 TypeFromName = {
103 107 'Rakefile' => :ruby,
104 108 'Rantfile' => :ruby,
105 109 }
106 110
107 111 end
108 112
109 113 end
110 114
111 115 if $0 == __FILE__
112 116 $VERBOSE = true
113 117 eval DATA.read, nil, $0, __LINE__+4
114 118 end
115 119
116 120 __END__
117 121
118 122 require 'test/unit'
119 123
120 124 class TC_FileType < Test::Unit::TestCase
121 125
122 126 def test_fetch
123 127 assert_raise FileType::UnknownFileType do
124 128 FileType.fetch ''
125 129 end
126 130
127 131 assert_throws :not_found do
128 132 FileType.fetch '.' do
129 133 throw :not_found
130 134 end
131 135 end
132 136
133 137 assert_equal :default, FileType.fetch('c', :default)
134 138
135 139 stderr, fake_stderr = $stderr, Object.new
136 140 $err = ''
137 141 def fake_stderr.write x
138 142 $err << x
139 143 end
140 144 $stderr = fake_stderr
141 145 FileType.fetch('c', :default) { }
142 146 assert_equal "block supersedes default value argument\n", $err
143 147 $stderr = stderr
144 148 end
145 149
146 150 def test_ruby
147 151 assert_equal :ruby, FileType['test.rb']
148 152 assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
149 153 assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
150 154 assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
151 155 assert_equal :ruby, FileType['./lib/tasks\repository.rake']
152 156 assert_not_equal :ruby, FileType['test_rb']
153 157 assert_not_equal :ruby, FileType['Makefile']
154 158 assert_not_equal :ruby, FileType['set.rb/set']
155 159 assert_not_equal :ruby, FileType['~/projects/blabla/rb']
156 160 end
157 161
158 162 def test_c
159 163 assert_equal :c, FileType['test.c']
160 164 assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
161 165 assert_not_equal :c, FileType['test_c']
162 166 assert_not_equal :c, FileType['Makefile']
163 167 assert_not_equal :c, FileType['set.h/set']
164 168 assert_not_equal :c, FileType['~/projects/blabla/c']
165 169 end
166 170
167 171 def test_html
168 172 assert_equal :html, FileType['test.htm']
169 173 assert_equal :xhtml, FileType['test.xhtml']
170 174 assert_equal :xhtml, FileType['test.html.xhtml']
171 175 assert_equal :rhtml, FileType['_form.rhtml']
172 176 end
173 177
174 178 def test_yaml
175 179 assert_equal :yaml, FileType['test.yml']
176 180 assert_equal :yaml, FileType['test.yaml']
177 181 assert_equal :yaml, FileType['my.html.yaml']
178 182 assert_not_equal :yaml, FileType['YAML']
179 183 end
180 184
181 185 def test_shebang
182 186 dir = './test'
183 187 if File.directory? dir
184 188 Dir.chdir dir do
185 189 assert_equal :c, FileType['test.c']
186 190 end
187 191 end
188 192 end
189 193
190 194 end
General Comments 0
You need to be logged in to leave comments. Login now