coderay 0.9.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{lib/README → README_INDEX.rdoc} +10 -21
- data/Rakefile +6 -6
- data/bin/coderay +193 -64
- data/lib/coderay.rb +61 -105
- data/lib/coderay/duo.rb +17 -21
- data/lib/coderay/encoder.rb +100 -112
- data/lib/coderay/encoders/_map.rb +12 -7
- data/lib/coderay/encoders/comment_filter.rb +12 -30
- data/lib/coderay/encoders/count.rb +29 -11
- data/lib/coderay/encoders/debug.rb +32 -20
- data/lib/coderay/encoders/div.rb +13 -9
- data/lib/coderay/encoders/filter.rb +34 -51
- data/lib/coderay/encoders/html.rb +155 -161
- data/lib/coderay/encoders/html/css.rb +4 -9
- data/lib/coderay/encoders/html/numbering.rb +115 -0
- data/lib/coderay/encoders/html/output.rb +22 -70
- data/lib/coderay/encoders/json.rb +59 -45
- data/lib/coderay/encoders/lines_of_code.rb +12 -57
- data/lib/coderay/encoders/null.rb +6 -14
- data/lib/coderay/encoders/page.rb +13 -9
- data/lib/coderay/encoders/span.rb +13 -9
- data/lib/coderay/encoders/statistic.rb +58 -39
- data/lib/coderay/encoders/terminal.rb +179 -0
- data/lib/coderay/encoders/text.rb +31 -17
- data/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/coderay/encoders/xml.rb +19 -18
- data/lib/coderay/encoders/yaml.rb +37 -9
- data/lib/coderay/for_redcloth.rb +4 -4
- data/lib/coderay/helpers/file_type.rb +127 -246
- data/lib/coderay/helpers/gzip.rb +41 -0
- data/lib/coderay/helpers/plugin.rb +241 -306
- data/lib/coderay/helpers/word_list.rb +65 -126
- data/lib/coderay/scanner.rb +173 -156
- data/lib/coderay/scanners/_map.rb +18 -17
- data/lib/coderay/scanners/c.rb +63 -77
- data/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/coderay/scanners/cpp.rb +71 -84
- data/lib/coderay/scanners/css.rb +103 -120
- data/lib/coderay/scanners/debug.rb +47 -44
- data/lib/coderay/scanners/delphi.rb +70 -76
- data/lib/coderay/scanners/diff.rb +141 -50
- data/lib/coderay/scanners/erb.rb +81 -0
- data/lib/coderay/scanners/groovy.rb +104 -113
- data/lib/coderay/scanners/haml.rb +168 -0
- data/lib/coderay/scanners/html.rb +181 -110
- data/lib/coderay/scanners/java.rb +73 -75
- data/lib/coderay/scanners/java/builtin_types.rb +2 -0
- data/lib/coderay/scanners/java_script.rb +90 -101
- data/lib/coderay/scanners/json.rb +40 -53
- data/lib/coderay/scanners/php.rb +123 -147
- data/lib/coderay/scanners/python.rb +93 -91
- data/lib/coderay/scanners/raydebug.rb +66 -0
- data/lib/coderay/scanners/ruby.rb +343 -326
- data/lib/coderay/scanners/ruby/patterns.rb +40 -106
- data/lib/coderay/scanners/ruby/string_state.rb +71 -0
- data/lib/coderay/scanners/sql.rb +80 -66
- data/lib/coderay/scanners/text.rb +26 -0
- data/lib/coderay/scanners/xml.rb +1 -1
- data/lib/coderay/scanners/yaml.rb +74 -73
- data/lib/coderay/style.rb +10 -7
- data/lib/coderay/styles/_map.rb +3 -3
- data/lib/coderay/styles/alpha.rb +143 -0
- data/lib/coderay/token_kinds.rb +90 -0
- data/lib/coderay/tokens.rb +102 -277
- data/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/coderay/version.rb +3 -0
- data/test/functional/basic.rb +200 -18
- data/test/functional/examples.rb +130 -0
- data/test/functional/for_redcloth.rb +15 -8
- data/test/functional/suite.rb +9 -6
- metadata +103 -123
- data/FOLDERS +0 -53
- data/bin/coderay_stylesheet +0 -4
- data/lib/coderay/encoders/html/numerization.rb +0 -133
- data/lib/coderay/encoders/term.rb +0 -158
- data/lib/coderay/encoders/token_class_filter.rb +0 -84
- data/lib/coderay/helpers/gzip_simple.rb +0 -123
- data/lib/coderay/scanners/nitro_xhtml.rb +0 -136
- data/lib/coderay/scanners/plaintext.rb +0 -20
- data/lib/coderay/scanners/rhtml.rb +0 -78
- data/lib/coderay/scanners/scheme.rb +0 -145
- data/lib/coderay/styles/cycnus.rb +0 -152
- data/lib/coderay/styles/murphy.rb +0 -134
- data/lib/coderay/token_classes.rb +0 -86
- data/test/functional/load_plugin_scanner.rb +0 -11
- data/test/functional/vhdl.rb +0 -126
- data/test/functional/word_list.rb +0 -79
@@ -1,32 +1,46 @@
|
|
1
1
|
module CodeRay
|
2
2
|
module Encoders
|
3
|
-
|
3
|
+
|
4
|
+
# Concats the tokens into a single string, resulting in the original
|
5
|
+
# code string if no tokens were removed.
|
6
|
+
#
|
7
|
+
# Alias: +plain+, +plaintext+
|
8
|
+
#
|
9
|
+
# == Options
|
10
|
+
#
|
11
|
+
# === :separator
|
12
|
+
# A separator string to join the tokens.
|
13
|
+
#
|
14
|
+
# Default: empty String
|
4
15
|
class Text < Encoder
|
5
|
-
|
6
|
-
include Streamable
|
16
|
+
|
7
17
|
register_for :text
|
8
|
-
|
18
|
+
|
9
19
|
FILE_EXTENSION = 'txt'
|
10
|
-
|
20
|
+
|
11
21
|
DEFAULT_OPTIONS = {
|
12
|
-
:separator =>
|
22
|
+
:separator => nil
|
13
23
|
}
|
14
|
-
|
24
|
+
|
25
|
+
def text_token text, kind
|
26
|
+
super
|
27
|
+
|
28
|
+
if @first
|
29
|
+
@first = false
|
30
|
+
else
|
31
|
+
@out << @sep
|
32
|
+
end if @sep
|
33
|
+
end
|
34
|
+
|
15
35
|
protected
|
16
36
|
def setup options
|
17
37
|
super
|
38
|
+
|
39
|
+
@first = true
|
18
40
|
@sep = options[:separator]
|
19
41
|
end
|
20
|
-
|
21
|
-
def text_token text, kind
|
22
|
-
text + @sep
|
23
|
-
end
|
24
|
-
|
25
|
-
def finish options
|
26
|
-
super.chomp @sep
|
27
|
-
end
|
28
|
-
|
42
|
+
|
29
43
|
end
|
30
|
-
|
44
|
+
|
31
45
|
end
|
32
46
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module CodeRay
|
2
|
+
module Encoders
|
3
|
+
|
4
|
+
load :filter
|
5
|
+
|
6
|
+
# A Filter that selects tokens based on their token kind.
|
7
|
+
#
|
8
|
+
# == Options
|
9
|
+
#
|
10
|
+
# === :exclude
|
11
|
+
#
|
12
|
+
# One or many symbols (in an Array) which shall be excluded.
|
13
|
+
#
|
14
|
+
# Default: []
|
15
|
+
#
|
16
|
+
# === :include
|
17
|
+
#
|
18
|
+
# One or many symbols (in an array) which shall be included.
|
19
|
+
#
|
20
|
+
# Default: :all, which means all tokens are included.
|
21
|
+
#
|
22
|
+
# Exclusion wins over inclusion.
|
23
|
+
#
|
24
|
+
# See also: CommentFilter
|
25
|
+
class TokenKindFilter < Filter
|
26
|
+
|
27
|
+
register_for :token_kind_filter
|
28
|
+
|
29
|
+
DEFAULT_OPTIONS = {
|
30
|
+
:exclude => [],
|
31
|
+
:include => :all
|
32
|
+
}
|
33
|
+
|
34
|
+
protected
|
35
|
+
def setup options
|
36
|
+
super
|
37
|
+
|
38
|
+
@group_excluded = false
|
39
|
+
@exclude = options[:exclude]
|
40
|
+
@exclude = Array(@exclude) unless @exclude == :all
|
41
|
+
@include = options[:include]
|
42
|
+
@include = Array(@include) unless @include == :all
|
43
|
+
end
|
44
|
+
|
45
|
+
def include_text_token? text, kind
|
46
|
+
include_group? kind
|
47
|
+
end
|
48
|
+
|
49
|
+
def include_group? kind
|
50
|
+
(@include == :all || @include.include?(kind)) &&
|
51
|
+
!(@exclude == :all || @exclude.include?(kind))
|
52
|
+
end
|
53
|
+
|
54
|
+
public
|
55
|
+
|
56
|
+
# Add the token to the output stream if +kind+ matches the conditions.
|
57
|
+
def text_token text, kind
|
58
|
+
super if !@group_excluded && include_text_token?(text, kind)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Add the token group to the output stream if +kind+ matches the
|
62
|
+
# conditions.
|
63
|
+
#
|
64
|
+
# If it does not, all tokens inside the group are excluded from the
|
65
|
+
# stream, even if their kinds match.
|
66
|
+
def begin_group kind
|
67
|
+
if @group_excluded
|
68
|
+
@group_excluded += 1
|
69
|
+
elsif include_group? kind
|
70
|
+
super
|
71
|
+
else
|
72
|
+
@group_excluded = 1
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# See +begin_group+.
|
77
|
+
def begin_line kind
|
78
|
+
if @group_excluded
|
79
|
+
@group_excluded += 1
|
80
|
+
elsif include_group? kind
|
81
|
+
super
|
82
|
+
else
|
83
|
+
@group_excluded = 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Take care of re-enabling the delegation of tokens to the output stream
|
88
|
+
# if an exluded group has ended.
|
89
|
+
def end_group kind
|
90
|
+
if @group_excluded
|
91
|
+
@group_excluded -= 1
|
92
|
+
@group_excluded = false if @group_excluded.zero?
|
93
|
+
else
|
94
|
+
super
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# See +end_group+.
|
99
|
+
def end_line kind
|
100
|
+
if @group_excluded
|
101
|
+
@group_excluded -= 1
|
102
|
+
@group_excluded = false if @group_excluded.zero?
|
103
|
+
else
|
104
|
+
super
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
data/lib/coderay/encoders/xml.rb
CHANGED
@@ -1,39 +1,40 @@
|
|
1
1
|
module CodeRay
|
2
2
|
module Encoders
|
3
|
-
|
3
|
+
|
4
4
|
# = XML Encoder
|
5
5
|
#
|
6
6
|
# Uses REXML. Very slow.
|
7
7
|
class XML < Encoder
|
8
|
-
|
9
|
-
include Streamable
|
8
|
+
|
10
9
|
register_for :xml
|
11
|
-
|
10
|
+
|
12
11
|
FILE_EXTENSION = 'xml'
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
|
13
|
+
autoload :REXML, 'rexml/document'
|
14
|
+
|
16
15
|
DEFAULT_OPTIONS = {
|
17
16
|
:tab_width => 8,
|
18
17
|
:pretty => -1,
|
19
18
|
:transitive => false,
|
20
19
|
}
|
21
|
-
|
20
|
+
|
22
21
|
protected
|
23
|
-
|
24
22
|
def setup options
|
23
|
+
super
|
24
|
+
|
25
25
|
@doc = REXML::Document.new
|
26
26
|
@doc << REXML::XMLDecl.new
|
27
27
|
@tab_width = options[:tab_width]
|
28
28
|
@root = @node = @doc.add_element('coderay-tokens')
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def finish options
|
32
|
-
@out = ''
|
33
32
|
@doc.write @out, options[:pretty], options[:transitive], true
|
34
|
-
|
33
|
+
|
34
|
+
super
|
35
35
|
end
|
36
36
|
|
37
|
+
public
|
37
38
|
def text_token text, kind
|
38
39
|
if kind == :space
|
39
40
|
token = @node
|
@@ -53,19 +54,19 @@ module Encoders
|
|
53
54
|
end
|
54
55
|
end
|
55
56
|
end
|
56
|
-
|
57
|
-
def
|
57
|
+
|
58
|
+
def begin_group kind
|
58
59
|
@node = @node.add_element kind.to_s
|
59
60
|
end
|
60
|
-
|
61
|
-
def
|
61
|
+
|
62
|
+
def end_group kind
|
62
63
|
if @node == @root
|
63
64
|
raise 'no token to close!'
|
64
65
|
end
|
65
66
|
@node = @node.parent
|
66
67
|
end
|
67
|
-
|
68
|
+
|
68
69
|
end
|
69
|
-
|
70
|
+
|
70
71
|
end
|
71
72
|
end
|
@@ -1,22 +1,50 @@
|
|
1
|
+
autoload :YAML, 'yaml'
|
2
|
+
|
1
3
|
module CodeRay
|
2
4
|
module Encoders
|
3
|
-
|
5
|
+
|
4
6
|
# = YAML Encoder
|
5
7
|
#
|
6
8
|
# Slow.
|
7
9
|
class YAML < Encoder
|
8
|
-
|
10
|
+
|
9
11
|
register_for :yaml
|
10
|
-
|
12
|
+
|
11
13
|
FILE_EXTENSION = 'yaml'
|
12
|
-
|
14
|
+
|
13
15
|
protected
|
14
|
-
def
|
15
|
-
|
16
|
-
|
16
|
+
def setup options
|
17
|
+
super
|
18
|
+
|
19
|
+
@data = []
|
17
20
|
end
|
18
|
-
|
21
|
+
|
22
|
+
def finish options
|
23
|
+
output ::YAML.dump(@data)
|
24
|
+
end
|
25
|
+
|
26
|
+
public
|
27
|
+
def text_token text, kind
|
28
|
+
@data << [text, kind]
|
29
|
+
end
|
30
|
+
|
31
|
+
def begin_group kind
|
32
|
+
@data << [:begin_group, kind]
|
33
|
+
end
|
34
|
+
|
35
|
+
def end_group kind
|
36
|
+
@data << [:end_group, kind]
|
37
|
+
end
|
38
|
+
|
39
|
+
def begin_line kind
|
40
|
+
@data << [:begin_line, kind]
|
41
|
+
end
|
42
|
+
|
43
|
+
def end_line kind
|
44
|
+
@data << [:end_line, kind]
|
45
|
+
end
|
46
|
+
|
19
47
|
end
|
20
|
-
|
48
|
+
|
21
49
|
end
|
22
50
|
end
|
data/lib/coderay/for_redcloth.rb
CHANGED
@@ -30,7 +30,7 @@ module CodeRay
|
|
30
30
|
end
|
31
31
|
RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
|
32
32
|
RedCloth::Formatters::HTML.module_eval do
|
33
|
-
def unescape(html)
|
33
|
+
def unescape(html) # :nodoc:
|
34
34
|
replacements = {
|
35
35
|
'&' => '&',
|
36
36
|
'"' => '"',
|
@@ -45,7 +45,7 @@ module CodeRay
|
|
45
45
|
if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
|
46
46
|
# simulating pre-4.2 behavior
|
47
47
|
if opts[:text].sub!(/\A\[(\w+)\]/, '')
|
48
|
-
if CodeRay::Scanners[$1].
|
48
|
+
if CodeRay::Scanners[$1].lang == :text
|
49
49
|
opts[:text] = $& + opts[:text]
|
50
50
|
else
|
51
51
|
opts[:lang] = $1
|
@@ -57,7 +57,7 @@ module CodeRay
|
|
57
57
|
@in_bc ||= nil
|
58
58
|
format = @in_bc ? :div : :span
|
59
59
|
opts[:text] = unescape(opts[:text]) unless @in_bc
|
60
|
-
highlighted_code = CodeRay.encode opts[:text], opts[:lang], format
|
60
|
+
highlighted_code = CodeRay.encode opts[:text], opts[:lang], format
|
61
61
|
highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
|
62
62
|
highlighted_code
|
63
63
|
else
|
@@ -74,7 +74,7 @@ module CodeRay
|
|
74
74
|
@in_bc = nil
|
75
75
|
opts[:lang] ? '' : "</pre>\n"
|
76
76
|
end
|
77
|
-
def escape_pre(text)
|
77
|
+
def escape_pre(text) # :nodoc:
|
78
78
|
if @in_bc ||= nil
|
79
79
|
text
|
80
80
|
else
|
@@ -1,56 +1,70 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
module CodeRay
|
3
|
-
|
4
|
-
# = FileType
|
5
|
-
#
|
6
|
-
# A simple filetype recognizer.
|
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
|
-
|
2
|
+
|
3
|
+
# = FileType
|
4
|
+
#
|
5
|
+
# A simple filetype recognizer.
|
6
|
+
#
|
7
|
+
# == Usage
|
8
|
+
#
|
9
|
+
# # determine the type of the given
|
10
|
+
# lang = FileType[file_name]
|
11
|
+
#
|
12
|
+
# # return :text if the file type is unknown
|
13
|
+
# lang = FileType.fetch file_name, :text
|
14
|
+
#
|
15
|
+
# # try the shebang line, too
|
16
|
+
# lang = FileType.fetch file_name, :text, true
|
17
|
+
module FileType
|
18
|
+
|
19
|
+
UnknownFileType = Class.new Exception
|
20
|
+
|
21
|
+
class << self
|
22
|
+
|
23
|
+
# Try to determine the file type of the file.
|
24
|
+
#
|
25
|
+
# +filename+ is a relative or absolute path to a file.
|
26
|
+
#
|
27
|
+
# The file itself is only accessed when +read_shebang+ is set to true.
|
28
|
+
# That means you can get filetypes from files that don't exist.
|
29
|
+
def [] filename, read_shebang = false
|
30
|
+
name = File.basename filename
|
31
|
+
ext = File.extname(name).sub(/^\./, '') # from last dot, delete the leading dot
|
32
|
+
ext2 = filename.to_s[/\.(.*)/, 1] # from first dot
|
33
|
+
|
34
|
+
type =
|
35
|
+
TypeFromExt[ext] ||
|
36
|
+
TypeFromExt[ext.downcase] ||
|
37
|
+
(TypeFromExt[ext2] if ext2) ||
|
38
|
+
(TypeFromExt[ext2.downcase] if ext2) ||
|
39
|
+
TypeFromName[name] ||
|
40
|
+
TypeFromName[name.downcase]
|
41
|
+
type ||= shebang(filename) if read_shebang
|
42
|
+
|
43
|
+
type
|
44
|
+
end
|
45
|
+
|
46
|
+
# This works like Hash#fetch.
|
47
|
+
#
|
48
|
+
# If the filetype cannot be found, the +default+ value
|
49
|
+
# is returned.
|
50
|
+
def fetch filename, default = nil, read_shebang = false
|
51
|
+
if default && block_given?
|
52
|
+
warn 'Block supersedes default value argument; use either.'
|
53
|
+
end
|
54
|
+
|
55
|
+
if type = self[filename, read_shebang]
|
56
|
+
type
|
57
|
+
else
|
58
|
+
return yield if block_given?
|
59
|
+
return default if default
|
60
|
+
raise UnknownFileType, 'Could not determine type of %p.' % filename
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def shebang filename
|
67
|
+
return unless File.exist? filename
|
54
68
|
File.open filename, 'r' do |f|
|
55
69
|
if first_line = f.gets
|
56
70
|
if type = first_line[TypeFromShebang]
|
@@ -58,203 +72,70 @@ module FileType
|
|
58
72
|
end
|
59
73
|
end
|
60
74
|
end
|
61
|
-
rescue IOError
|
62
|
-
nil
|
63
75
|
end
|
76
|
+
|
64
77
|
end
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
|
79
|
+
TypeFromExt = {
|
80
|
+
'c' => :c,
|
81
|
+
'cfc' => :xml,
|
82
|
+
'cfm' => :xml,
|
83
|
+
'clj' => :clojure,
|
84
|
+
'css' => :css,
|
85
|
+
'diff' => :diff,
|
86
|
+
'dpr' => :delphi,
|
87
|
+
'gemspec' => :ruby,
|
88
|
+
'groovy' => :groovy,
|
89
|
+
'gvy' => :groovy,
|
90
|
+
'h' => :c,
|
91
|
+
'haml' => :haml,
|
92
|
+
'htm' => :page,
|
93
|
+
'html' => :page,
|
94
|
+
'html.erb' => :erb,
|
95
|
+
'java' => :java,
|
96
|
+
'js' => :java_script,
|
97
|
+
'json' => :json,
|
98
|
+
'mab' => :ruby,
|
99
|
+
'pas' => :delphi,
|
100
|
+
'patch' => :diff,
|
101
|
+
'php' => :php,
|
102
|
+
'php3' => :php,
|
103
|
+
'php4' => :php,
|
104
|
+
'php5' => :php,
|
105
|
+
'prawn' => :ruby,
|
106
|
+
'py' => :python,
|
107
|
+
'py3' => :python,
|
108
|
+
'pyw' => :python,
|
109
|
+
'rake' => :ruby,
|
110
|
+
'raydebug' => :raydebug,
|
111
|
+
'rb' => :ruby,
|
112
|
+
'rbw' => :ruby,
|
113
|
+
'rhtml' => :erb,
|
114
|
+
'rjs' => :ruby,
|
115
|
+
'rpdf' => :ruby,
|
116
|
+
'ru' => :ruby,
|
117
|
+
'rxml' => :ruby,
|
118
|
+
# 'sch' => :scheme,
|
119
|
+
'sql' => :sql,
|
120
|
+
# 'ss' => :scheme,
|
121
|
+
'xhtml' => :page,
|
122
|
+
'xml' => :xml,
|
123
|
+
'yaml' => :yaml,
|
124
|
+
'yml' => :yaml,
|
125
|
+
}
|
126
|
+
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
|
127
|
+
TypeFromExt[cpp_alias] = :cpp
|
81
128
|
end
|
82
|
-
|
129
|
+
|
130
|
+
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
|
131
|
+
|
132
|
+
TypeFromName = {
|
133
|
+
'Capfile' => :ruby,
|
134
|
+
'Rakefile' => :ruby,
|
135
|
+
'Rantfile' => :ruby,
|
136
|
+
'Gemfile' => :ruby,
|
137
|
+
}
|
138
|
+
|
83
139
|
end
|
84
|
-
|
85
|
-
TypeFromExt = {
|
86
|
-
'c' => :c,
|
87
|
-
'cfc' => :xml,
|
88
|
-
'cfm' => :xml,
|
89
|
-
'css' => :css,
|
90
|
-
'diff' => :diff,
|
91
|
-
'dpr' => :delphi,
|
92
|
-
'gemspec' => :ruby,
|
93
|
-
'groovy' => :groovy,
|
94
|
-
'gvy' => :groovy,
|
95
|
-
'h' => :c,
|
96
|
-
'htm' => :html,
|
97
|
-
'html' => :html,
|
98
|
-
'html.erb' => :rhtml,
|
99
|
-
'java' => :java,
|
100
|
-
'js' => :java_script,
|
101
|
-
'json' => :json,
|
102
|
-
'mab' => :ruby,
|
103
|
-
'pas' => :delphi,
|
104
|
-
'patch' => :diff,
|
105
|
-
'php' => :php,
|
106
|
-
'php3' => :php,
|
107
|
-
'php4' => :php,
|
108
|
-
'php5' => :php,
|
109
|
-
'py' => :python,
|
110
|
-
'py3' => :python,
|
111
|
-
'pyw' => :python,
|
112
|
-
'rake' => :ruby,
|
113
|
-
'raydebug' => :debug,
|
114
|
-
'rb' => :ruby,
|
115
|
-
'rbw' => :ruby,
|
116
|
-
'rhtml' => :rhtml,
|
117
|
-
'rjs' => :ruby,
|
118
|
-
'rpdf' => :ruby,
|
119
|
-
'rxml' => :ruby,
|
120
|
-
'sch' => :scheme,
|
121
|
-
'sql' => :sql,
|
122
|
-
'ss' => :scheme,
|
123
|
-
'xhtml' => :xhtml,
|
124
|
-
'xml' => :xml,
|
125
|
-
'yaml' => :yaml,
|
126
|
-
'yml' => :yaml,
|
127
|
-
}
|
128
|
-
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
|
129
|
-
TypeFromExt[cpp_alias] = :cpp
|
130
|
-
end
|
131
|
-
|
132
|
-
TypeFromShebang = /\b(?:ruby|perl|python|sh)\b/
|
133
|
-
|
134
|
-
TypeFromName = {
|
135
|
-
'Rakefile' => :ruby,
|
136
|
-
'Rantfile' => :ruby,
|
137
|
-
}
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
if $0 == __FILE__
|
144
|
-
$VERBOSE = true
|
145
|
-
eval DATA.read, nil, $0, __LINE__ + 4
|
146
|
-
end
|
147
|
-
|
148
|
-
__END__
|
149
|
-
require 'test/unit'
|
150
|
-
|
151
|
-
class FileTypeTests < Test::Unit::TestCase
|
152
140
|
|
153
|
-
include CodeRay
|
154
|
-
|
155
|
-
def test_fetch
|
156
|
-
assert_raise FileType::UnknownFileType do
|
157
|
-
FileType.fetch ''
|
158
|
-
end
|
159
|
-
|
160
|
-
assert_throws :not_found do
|
161
|
-
FileType.fetch '.' do
|
162
|
-
throw :not_found
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
assert_equal :default, FileType.fetch('c', :default)
|
167
|
-
|
168
|
-
stderr, fake_stderr = $stderr, Object.new
|
169
|
-
$err = ''
|
170
|
-
def fake_stderr.write x
|
171
|
-
$err << x
|
172
|
-
end
|
173
|
-
$stderr = fake_stderr
|
174
|
-
FileType.fetch('c', :default) { }
|
175
|
-
assert_equal "block supersedes default value argument\n", $err
|
176
|
-
$stderr = stderr
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_ruby
|
180
|
-
assert_equal :ruby, FileType['test.rb']
|
181
|
-
assert_equal :ruby, FileType['test.java.rb']
|
182
|
-
assert_equal :java, FileType['test.rb.java']
|
183
|
-
assert_equal :ruby, FileType['C:\\Program Files\\x\\y\\c\\test.rbw']
|
184
|
-
assert_equal :ruby, FileType['/usr/bin/something/Rakefile']
|
185
|
-
assert_equal :ruby, FileType['~/myapp/gem/Rantfile']
|
186
|
-
assert_equal :ruby, FileType['./lib/tasks\repository.rake']
|
187
|
-
assert_not_equal :ruby, FileType['test_rb']
|
188
|
-
assert_not_equal :ruby, FileType['Makefile']
|
189
|
-
assert_not_equal :ruby, FileType['set.rb/set']
|
190
|
-
assert_not_equal :ruby, FileType['~/projects/blabla/rb']
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_c
|
194
|
-
assert_equal :c, FileType['test.c']
|
195
|
-
assert_equal :c, FileType['C:\\Program Files\\x\\y\\c\\test.h']
|
196
|
-
assert_not_equal :c, FileType['test_c']
|
197
|
-
assert_not_equal :c, FileType['Makefile']
|
198
|
-
assert_not_equal :c, FileType['set.h/set']
|
199
|
-
assert_not_equal :c, FileType['~/projects/blabla/c']
|
200
|
-
end
|
201
|
-
|
202
|
-
def test_cpp
|
203
|
-
assert_equal :cpp, FileType['test.c++']
|
204
|
-
assert_equal :cpp, FileType['test.cxx']
|
205
|
-
assert_equal :cpp, FileType['test.hh']
|
206
|
-
assert_equal :cpp, FileType['test.hpp']
|
207
|
-
assert_equal :cpp, FileType['test.cu']
|
208
|
-
assert_equal :cpp, FileType['test.C']
|
209
|
-
assert_not_equal :cpp, FileType['test.c']
|
210
|
-
assert_not_equal :cpp, FileType['test.h']
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_html
|
214
|
-
assert_equal :html, FileType['test.htm']
|
215
|
-
assert_equal :xhtml, FileType['test.xhtml']
|
216
|
-
assert_equal :xhtml, FileType['test.html.xhtml']
|
217
|
-
assert_equal :rhtml, FileType['_form.rhtml']
|
218
|
-
assert_equal :rhtml, FileType['_form.html.erb']
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_yaml
|
222
|
-
assert_equal :yaml, FileType['test.yml']
|
223
|
-
assert_equal :yaml, FileType['test.yaml']
|
224
|
-
assert_equal :yaml, FileType['my.html.yaml']
|
225
|
-
assert_not_equal :yaml, FileType['YAML']
|
226
|
-
end
|
227
|
-
|
228
|
-
def test_pathname
|
229
|
-
require 'pathname'
|
230
|
-
pn = Pathname.new 'test.rb'
|
231
|
-
assert_equal :ruby, FileType[pn]
|
232
|
-
dir = Pathname.new '/etc/var/blubb'
|
233
|
-
assert_equal :ruby, FileType[dir + pn]
|
234
|
-
assert_equal :cpp, FileType[dir + 'test.cpp']
|
235
|
-
end
|
236
|
-
|
237
|
-
def test_no_shebang
|
238
|
-
dir = './test'
|
239
|
-
if File.directory? dir
|
240
|
-
Dir.chdir dir do
|
241
|
-
assert_equal :c, FileType['test.c']
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_shebang_empty_file
|
247
|
-
require 'tmpdir'
|
248
|
-
tmpfile = File.join(Dir.tmpdir, 'bla')
|
249
|
-
File.open(tmpfile, 'w') { } # touch
|
250
|
-
assert_equal nil, FileType[tmpfile]
|
251
|
-
end
|
252
|
-
|
253
|
-
def test_shebang
|
254
|
-
require 'tmpdir'
|
255
|
-
tmpfile = File.join(Dir.tmpdir, 'bla')
|
256
|
-
File.open(tmpfile, 'w') { |f| f.puts '#!/usr/bin/env ruby' }
|
257
|
-
assert_equal :ruby, FileType[tmpfile, true]
|
258
|
-
end
|
259
|
-
|
260
141
|
end
|