coderay 1.0.0 → 1.0.0.598.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/FOLDERS +49 -0
- data/Rakefile +6 -5
- data/bin/coderay +74 -190
- data/bin/coderay_stylesheet +4 -0
- data/{README_INDEX.rdoc → lib/README} +20 -10
- data/lib/coderay.rb +60 -62
- data/lib/coderay/duo.rb +55 -2
- data/lib/coderay/encoder.rb +39 -52
- data/lib/coderay/encoders/_map.rb +7 -11
- data/lib/coderay/encoders/comment_filter.rb +61 -0
- data/lib/coderay/encoders/count.rb +26 -11
- data/lib/coderay/encoders/debug.rb +60 -11
- data/lib/coderay/encoders/div.rb +8 -9
- data/lib/coderay/encoders/filter.rb +52 -12
- data/lib/coderay/encoders/html.rb +113 -106
- data/lib/coderay/encoders/html/css.rb +7 -2
- data/lib/coderay/encoders/html/numbering.rb +27 -24
- data/lib/coderay/encoders/html/output.rb +58 -15
- data/lib/coderay/encoders/json.rb +44 -37
- data/lib/coderay/encoders/lines_of_code.rb +56 -9
- data/lib/coderay/encoders/null.rb +13 -6
- data/lib/coderay/encoders/page.rb +8 -8
- data/lib/coderay/encoders/span.rb +9 -10
- data/lib/coderay/encoders/statistic.rb +114 -51
- data/lib/coderay/encoders/terminal.rb +10 -7
- data/lib/coderay/encoders/text.rb +36 -17
- data/lib/coderay/encoders/token_kind_filter.rb +58 -1
- data/lib/coderay/encoders/xml.rb +11 -13
- data/lib/coderay/encoders/yaml.rb +14 -16
- data/lib/coderay/for_redcloth.rb +1 -1
- data/lib/coderay/helpers/file_type.rb +240 -125
- data/lib/coderay/helpers/gzip_simple.rb +123 -0
- data/lib/coderay/helpers/plugin.rb +307 -241
- data/lib/coderay/helpers/word_list.rb +126 -65
- data/lib/coderay/scanner.rb +103 -153
- data/lib/coderay/scanners/_map.rb +16 -18
- data/lib/coderay/scanners/c.rb +13 -13
- data/lib/coderay/scanners/cpp.rb +6 -6
- data/lib/coderay/scanners/css.rb +48 -47
- data/lib/coderay/scanners/debug.rb +55 -9
- data/lib/coderay/scanners/delphi.rb +4 -4
- data/lib/coderay/scanners/diff.rb +25 -43
- data/lib/coderay/scanners/groovy.rb +2 -2
- data/lib/coderay/scanners/html.rb +30 -107
- data/lib/coderay/scanners/java.rb +5 -6
- data/lib/coderay/scanners/java/builtin_types.rb +0 -2
- data/lib/coderay/scanners/java_script.rb +6 -6
- data/lib/coderay/scanners/json.rb +6 -7
- data/lib/coderay/scanners/nitro_xhtml.rb +136 -0
- data/lib/coderay/scanners/php.rb +12 -13
- data/lib/coderay/scanners/plaintext.rb +26 -0
- data/lib/coderay/scanners/python.rb +4 -4
- data/lib/coderay/scanners/{erb.rb → rhtml.rb} +11 -19
- data/lib/coderay/scanners/ruby.rb +208 -219
- data/lib/coderay/scanners/ruby/patterns.rb +85 -18
- data/lib/coderay/scanners/scheme.rb +136 -0
- data/lib/coderay/scanners/sql.rb +22 -29
- data/lib/coderay/scanners/yaml.rb +10 -11
- data/lib/coderay/styles/_map.rb +2 -2
- data/lib/coderay/styles/alpha.rb +104 -102
- data/lib/coderay/styles/cycnus.rb +143 -0
- data/lib/coderay/styles/murphy.rb +123 -0
- data/lib/coderay/token_kinds.rb +86 -87
- data/lib/coderay/tokens.rb +169 -26
- data/test/functional/basic.rb +14 -200
- data/test/functional/examples.rb +14 -20
- data/test/functional/for_redcloth.rb +8 -15
- data/test/functional/load_plugin_scanner.rb +11 -0
- data/test/functional/suite.rb +6 -9
- data/test/functional/vhdl.rb +126 -0
- data/test/functional/word_list.rb +79 -0
- metadata +129 -107
- data/lib/coderay/helpers/gzip.rb +0 -41
- data/lib/coderay/scanners/clojure.rb +0 -217
- data/lib/coderay/scanners/haml.rb +0 -168
- data/lib/coderay/scanners/ruby/string_state.rb +0 -71
- data/lib/coderay/scanners/text.rb +0 -26
- data/lib/coderay/tokens_proxy.rb +0 -55
- data/lib/coderay/version.rb +0 -3
@@ -0,0 +1,123 @@
|
|
1
|
+
# =GZip Simple
|
2
|
+
#
|
3
|
+
# A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
|
4
|
+
#
|
5
|
+
# Author: murphy (mail to murphy rubychan de)
|
6
|
+
#
|
7
|
+
# Version: 0.2 (2005.may.28)
|
8
|
+
#
|
9
|
+
# ==Documentation
|
10
|
+
#
|
11
|
+
# See +GZip+ module and the +String+ extensions.
|
12
|
+
#
|
13
|
+
module GZip
|
14
|
+
|
15
|
+
require 'zlib'
|
16
|
+
|
17
|
+
# The default zipping level. 7 zips good and fast.
|
18
|
+
DEFAULT_GZIP_LEVEL = 7
|
19
|
+
|
20
|
+
# Unzips the given string +s+.
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
# require 'gzip_simple'
|
24
|
+
# print GZip.gunzip(File.read('adresses.gz'))
|
25
|
+
def GZip.gunzip s
|
26
|
+
Zlib::Inflate.inflate s
|
27
|
+
end
|
28
|
+
|
29
|
+
# Zips the given string +s+.
|
30
|
+
#
|
31
|
+
# Example:
|
32
|
+
# require 'gzip_simple'
|
33
|
+
# File.open('adresses.gz', 'w') do |file
|
34
|
+
# file.write GZip.gzip('Mum: 0123 456 789', 9)
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# If you provide a +level+, you can control how strong
|
38
|
+
# the string is compressed:
|
39
|
+
# - 0: no compression, only convert to gzip format
|
40
|
+
# - 1: compress fast
|
41
|
+
# - 7: compress more, but still fast (default)
|
42
|
+
# - 8: compress more, slower
|
43
|
+
# - 9: compress best, very slow
|
44
|
+
def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
|
45
|
+
Zlib::Deflate.new(level).deflate s, Zlib::FINISH
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# String extensions to use the GZip module.
|
51
|
+
#
|
52
|
+
# The methods gzip and gunzip provide an even more simple
|
53
|
+
# interface to the ZLib:
|
54
|
+
#
|
55
|
+
# # create a big string
|
56
|
+
# x = 'a' * 1000
|
57
|
+
#
|
58
|
+
# # zip it
|
59
|
+
# x_gz = x.gzip
|
60
|
+
#
|
61
|
+
# # test the result
|
62
|
+
# puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size]
|
63
|
+
# #-> Zipped 1000 bytes to 19 bytes.
|
64
|
+
#
|
65
|
+
# # unzipping works
|
66
|
+
# p x_gz.gunzip == x #-> true
|
67
|
+
class String
|
68
|
+
# Returns the string, unzipped.
|
69
|
+
# See GZip.gunzip
|
70
|
+
def gunzip
|
71
|
+
GZip.gunzip self
|
72
|
+
end
|
73
|
+
# Replaces the string with its unzipped value.
|
74
|
+
# See GZip.gunzip
|
75
|
+
def gunzip!
|
76
|
+
replace gunzip
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns the string, zipped.
|
80
|
+
# +level+ is the gzip compression level, see GZip.gzip.
|
81
|
+
def gzip level = GZip::DEFAULT_GZIP_LEVEL
|
82
|
+
GZip.gzip self, level
|
83
|
+
end
|
84
|
+
# Replaces the string with its zipped value.
|
85
|
+
# See GZip.gzip.
|
86
|
+
def gzip!(*args)
|
87
|
+
replace gzip(*args)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
if $0 == __FILE__
|
92
|
+
eval DATA.read, nil, $0, __LINE__+4
|
93
|
+
end
|
94
|
+
|
95
|
+
__END__
|
96
|
+
#CODE
|
97
|
+
|
98
|
+
# Testing / Benchmark
|
99
|
+
x = 'a' * 1000
|
100
|
+
x_gz = x.gzip
|
101
|
+
puts 'Zipped %d bytes to %d bytes.' % [x.size, x_gz.size] #-> Zipped 1000 bytes to 19 bytes.
|
102
|
+
p x_gz.gunzip == x #-> true
|
103
|
+
|
104
|
+
require 'benchmark'
|
105
|
+
|
106
|
+
INFO = 'packed to %0.3f%%' # :nodoc:
|
107
|
+
|
108
|
+
x = Array.new(100000) { rand(255).chr + 'aaaaaaaaa' + rand(255).chr }.join
|
109
|
+
Benchmark.bm(10) do |bm|
|
110
|
+
for level in 0..9
|
111
|
+
bm.report "zip #{level}" do
|
112
|
+
$x = x.gzip level
|
113
|
+
end
|
114
|
+
puts INFO % [100.0 * $x.size / x.size]
|
115
|
+
end
|
116
|
+
bm.report 'zip' do
|
117
|
+
$x = x.gzip
|
118
|
+
end
|
119
|
+
puts INFO % [100.0 * $x.size / x.size]
|
120
|
+
bm.report 'unzip' do
|
121
|
+
$x.gunzip
|
122
|
+
end
|
123
|
+
end
|
@@ -1,284 +1,350 @@
|
|
1
1
|
module CodeRay
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
# = PluginHost
|
4
|
+
#
|
5
|
+
# A simple subclass plugin system.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# class Generators < PluginHost
|
9
|
+
# plugin_path 'app/generators'
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# class Generator
|
13
|
+
# extend Plugin
|
14
|
+
# PLUGIN_HOST = Generators
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# class FancyGenerator < Generator
|
18
|
+
# register_for :fancy
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Generators[:fancy] #-> FancyGenerator
|
22
|
+
# # or
|
23
|
+
# CodeRay.require_plugin 'Generators/fancy'
|
24
|
+
module PluginHost
|
25
|
+
|
26
|
+
# Raised if Encoders::[] fails because:
|
27
|
+
# * a file could not be found
|
28
|
+
# * the requested Encoder is not registered
|
29
|
+
PluginNotFound = Class.new Exception
|
30
|
+
HostNotFound = Class.new Exception
|
31
|
+
|
32
|
+
PLUGIN_HOSTS = []
|
33
|
+
PLUGIN_HOSTS_BY_ID = {} # dummy hash
|
34
|
+
|
35
|
+
# Loads all plugins using list and load.
|
36
|
+
def load_all
|
37
|
+
for plugin in list
|
38
|
+
load plugin
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the Plugin for +id+.
|
6
43
|
#
|
7
44
|
# Example:
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# * a file could not be found
|
31
|
-
# * the requested Plugin is not registered
|
32
|
-
PluginNotFound = Class.new LoadError
|
33
|
-
HostNotFound = Class.new LoadError
|
34
|
-
|
35
|
-
PLUGIN_HOSTS = []
|
36
|
-
PLUGIN_HOSTS_BY_ID = {} # dummy hash
|
37
|
-
|
38
|
-
# Loads all plugins using list and load.
|
39
|
-
def load_all
|
40
|
-
for plugin in list
|
41
|
-
load plugin
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Returns the Plugin for +id+.
|
46
|
-
#
|
47
|
-
# Example:
|
48
|
-
# yaml_plugin = MyPluginHost[:yaml]
|
49
|
-
def [] id, *args, &blk
|
50
|
-
plugin = validate_id(id)
|
51
|
-
begin
|
52
|
-
plugin = plugin_hash.[] plugin, *args, &blk
|
53
|
-
end while plugin.is_a? Symbol
|
54
|
-
plugin
|
55
|
-
end
|
56
|
-
|
57
|
-
alias load []
|
58
|
-
|
59
|
-
# Tries to +load+ the missing plugin by translating +const+ to the
|
60
|
-
# underscore form (eg. LinesOfCode becomes lines_of_code).
|
61
|
-
def const_missing const
|
62
|
-
id = const.to_s.
|
63
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
64
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
65
|
-
downcase
|
66
|
-
load id
|
45
|
+
# yaml_plugin = MyPluginHost[:yaml]
|
46
|
+
def [] id, *args, &blk
|
47
|
+
plugin = validate_id(id)
|
48
|
+
begin
|
49
|
+
plugin = plugin_hash.[] plugin, *args, &blk
|
50
|
+
end while plugin.is_a? Symbol
|
51
|
+
plugin
|
52
|
+
end
|
53
|
+
|
54
|
+
# Alias for +[]+.
|
55
|
+
alias load []
|
56
|
+
|
57
|
+
def require_helper plugin_id, helper_name
|
58
|
+
path = path_to File.join(plugin_id, helper_name)
|
59
|
+
require path
|
60
|
+
end
|
61
|
+
|
62
|
+
class << self
|
63
|
+
|
64
|
+
# Adds the module/class to the PLUGIN_HOSTS list.
|
65
|
+
def extended mod
|
66
|
+
PLUGIN_HOSTS << mod
|
67
67
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
#
|
72
|
-
def extended mod
|
73
|
-
PLUGIN_HOSTS << mod
|
74
|
-
end
|
75
|
-
|
68
|
+
|
69
|
+
# Warns you that you should not #include this module.
|
70
|
+
def included mod
|
71
|
+
warn "#{name} should not be included. Use extend."
|
76
72
|
end
|
77
|
-
|
78
|
-
#
|
79
|
-
def
|
80
|
-
unless
|
81
|
-
|
73
|
+
|
74
|
+
# Find the PluginHost for host_id.
|
75
|
+
def host_by_id host_id
|
76
|
+
unless PLUGIN_HOSTS_BY_ID.default_proc
|
77
|
+
ph = Hash.new do |h, a_host_id|
|
78
|
+
for host in PLUGIN_HOSTS
|
79
|
+
h[host.host_id] = host
|
80
|
+
end
|
81
|
+
h.fetch a_host_id, nil
|
82
|
+
end
|
83
|
+
PLUGIN_HOSTS_BY_ID.replace ph
|
82
84
|
end
|
83
|
-
|
85
|
+
PLUGIN_HOSTS_BY_ID[host_id]
|
84
86
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
# :luna => :moon
|
94
|
-
# end
|
95
|
-
def map hash
|
96
|
-
for from, to in hash
|
97
|
-
from = validate_id from
|
98
|
-
to = validate_id to
|
99
|
-
plugin_hash[from] = to unless plugin_hash.has_key? from
|
100
|
-
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
# The path where the plugins can be found.
|
91
|
+
def plugin_path *args
|
92
|
+
unless args.empty?
|
93
|
+
@plugin_path = File.expand_path File.join(*args)
|
94
|
+
load_map
|
101
95
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
# MyColorHost.default # loads and returns the Gray plugin
|
114
|
-
def default id = nil
|
115
|
-
if id
|
116
|
-
id = validate_id id
|
117
|
-
raise "The default plugin can't be named \"default\"." if id == :default
|
118
|
-
plugin_hash[:default] = id
|
119
|
-
else
|
120
|
-
load :default
|
121
|
-
end
|
96
|
+
@plugin_path
|
97
|
+
end
|
98
|
+
|
99
|
+
# The host's ID.
|
100
|
+
#
|
101
|
+
# If PLUGIN_HOST_ID is not set, it is simply the class name.
|
102
|
+
def host_id
|
103
|
+
if self.const_defined? :PLUGIN_HOST_ID
|
104
|
+
self::PLUGIN_HOST_ID
|
105
|
+
else
|
106
|
+
name
|
122
107
|
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
108
|
+
end
|
109
|
+
|
110
|
+
# Map a plugin_id to another.
|
111
|
+
#
|
112
|
+
# Usage: Put this in a file plugin_path/_map.rb.
|
113
|
+
#
|
114
|
+
# class MyColorHost < PluginHost
|
115
|
+
# map :navy => :dark_blue,
|
116
|
+
# :maroon => :brown,
|
117
|
+
# :luna => :moon
|
118
|
+
# end
|
119
|
+
def map hash
|
120
|
+
for from, to in hash
|
121
|
+
from = validate_id from
|
122
|
+
to = validate_id to
|
123
|
+
plugin_hash[from] = to unless plugin_hash.has_key? from
|
130
124
|
end
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
125
|
+
end
|
126
|
+
|
127
|
+
# Define the default plugin to use when no plugin is found
|
128
|
+
# for a given id.
|
129
|
+
#
|
130
|
+
# See also map.
|
131
|
+
#
|
132
|
+
# class MyColorHost < PluginHost
|
133
|
+
# map :navy => :dark_blue
|
134
|
+
# default :gray
|
135
|
+
# end
|
136
|
+
def default id = nil
|
137
|
+
if id
|
138
|
+
id = validate_id id
|
139
|
+
plugin_hash[nil] = id
|
140
|
+
else
|
141
|
+
plugin_hash[nil]
|
135
142
|
end
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
end
|
144
|
+
|
145
|
+
# Every plugin must register itself for one or more
|
146
|
+
# +ids+ by calling register_for, which calls this method.
|
147
|
+
#
|
148
|
+
# See Plugin#register_for.
|
149
|
+
def register plugin, *ids
|
150
|
+
for id in ids
|
151
|
+
unless id.is_a? Symbol
|
152
|
+
raise ArgumentError,
|
153
|
+
"id must be a Symbol, but it was a #{id.class}"
|
145
154
|
end
|
155
|
+
plugin_hash[validate_id(id)] = plugin
|
146
156
|
end
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
157
|
+
end
|
158
|
+
|
159
|
+
# A Hash of plugion_id => Plugin pairs.
|
160
|
+
def plugin_hash
|
161
|
+
@plugin_hash ||= create_plugin_hash
|
162
|
+
end
|
163
|
+
|
164
|
+
# Returns an array of all .rb files in the plugin path.
|
165
|
+
#
|
166
|
+
# The extension .rb is not included.
|
167
|
+
def list
|
168
|
+
Dir[path_to('*')].select do |file|
|
169
|
+
File.basename(file)[/^(?!_)\w+\.rb$/]
|
170
|
+
end.map do |file|
|
171
|
+
File.basename file, '.rb'
|
154
172
|
end
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
if File.exist? mapfile
|
163
|
-
require mapfile
|
164
|
-
true
|
165
|
-
else
|
166
|
-
false
|
167
|
-
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Makes a map of all loaded plugins.
|
176
|
+
def inspect
|
177
|
+
map = plugin_hash.dup
|
178
|
+
map.each do |id, plugin|
|
179
|
+
map[id] = plugin.to_s[/(?>\w+)$/]
|
168
180
|
end
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
181
|
+
"#{name}[#{host_id}]#{map.inspect}"
|
182
|
+
end
|
183
|
+
|
184
|
+
protected
|
185
|
+
# Created a new plugin list and stores it to @plugin_hash.
|
186
|
+
def create_plugin_hash
|
187
|
+
@plugin_hash =
|
175
188
|
Hash.new do |h, plugin_id|
|
176
189
|
id = validate_id(plugin_id)
|
177
190
|
path = path_to id
|
178
191
|
begin
|
179
|
-
raise LoadError, "#{path} not found" unless File.exist? path
|
180
192
|
require path
|
181
193
|
rescue LoadError => boom
|
182
|
-
if
|
183
|
-
|
184
|
-
warn '%p could not load plugin %p; falling back to %p' % [self, id, h[:default]]
|
185
|
-
h[:default]
|
186
|
-
else
|
187
|
-
raise PluginNotFound, '%p could not load plugin %p: %s' % [self, id, boom]
|
188
|
-
end
|
194
|
+
if h.has_key? nil # default plugin
|
195
|
+
h[id] = h[nil]
|
189
196
|
else
|
190
|
-
|
191
|
-
h[plugin_id]
|
197
|
+
raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom]
|
192
198
|
end
|
193
199
|
else
|
194
200
|
# Plugin should have registered by now
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
|
201
|
+
unless h.has_key? id
|
202
|
+
raise PluginNotFound,
|
203
|
+
"No #{self.name} plugin for #{id.inspect} found in #{path}."
|
199
204
|
end
|
200
205
|
end
|
206
|
+
h[id]
|
201
207
|
end
|
208
|
+
end
|
209
|
+
|
210
|
+
# Loads the map file (see map).
|
211
|
+
#
|
212
|
+
# This is done automatically when plugin_path is called.
|
213
|
+
def load_map
|
214
|
+
mapfile = path_to '_map'
|
215
|
+
if File.exist? mapfile
|
216
|
+
require mapfile
|
217
|
+
elsif $VERBOSE
|
218
|
+
warn 'no _map.rb found for %s' % name
|
202
219
|
end
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns the Plugin for +id+.
|
223
|
+
# Use it like Hash#fetch.
|
224
|
+
#
|
225
|
+
# Example:
|
226
|
+
# yaml_plugin = MyPluginHost[:yaml, :default]
|
227
|
+
def fetch id, *args, &blk
|
228
|
+
plugin_hash.fetch validate_id(id), *args, &blk
|
229
|
+
end
|
230
|
+
|
231
|
+
# Returns the expected path to the plugin file for the given id.
|
232
|
+
def path_to plugin_id
|
233
|
+
File.join plugin_path, "#{plugin_id}.rb"
|
234
|
+
end
|
235
|
+
|
236
|
+
# Converts +id+ to a Symbol if it is a String,
|
237
|
+
# or returns +id+ if it already is a Symbol.
|
238
|
+
#
|
239
|
+
# Raises +ArgumentError+ for all other objects, or if the
|
240
|
+
# given String includes non-alphanumeric characters (\W).
|
241
|
+
def validate_id id
|
242
|
+
if id.is_a? Symbol or id.nil?
|
243
|
+
id
|
244
|
+
elsif id.is_a? String
|
245
|
+
if id[/\w+/] == id
|
246
|
+
id.downcase.to_sym
|
223
247
|
else
|
224
|
-
raise ArgumentError, "
|
248
|
+
raise ArgumentError, "Invalid id: '#{id}' given."
|
225
249
|
end
|
250
|
+
else
|
251
|
+
raise ArgumentError,
|
252
|
+
"String or Symbol expected, but #{id.class} given."
|
226
253
|
end
|
227
|
-
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
# = Plugin
|
260
|
+
#
|
261
|
+
# Plugins have to include this module.
|
262
|
+
#
|
263
|
+
# IMPORTANT: use extend for this module.
|
264
|
+
#
|
265
|
+
# Example: see PluginHost.
|
266
|
+
module Plugin
|
267
|
+
|
268
|
+
def included mod
|
269
|
+
warn "#{name} should not be included. Use extend."
|
270
|
+
end
|
271
|
+
|
272
|
+
# Register this class for the given langs.
|
273
|
+
# Example:
|
274
|
+
# class MyPlugin < PluginHost::BaseClass
|
275
|
+
# register_for :my_id
|
276
|
+
# ...
|
277
|
+
# end
|
278
|
+
#
|
279
|
+
# See PluginHost.register.
|
280
|
+
def register_for *ids
|
281
|
+
@plugin_id = ids.first
|
282
|
+
plugin_host.register self, *ids
|
228
283
|
end
|
229
284
|
|
230
|
-
|
231
|
-
#
|
285
|
+
# Returns the title of the plugin, or sets it to the
|
286
|
+
# optional argument +title+.
|
287
|
+
def title title = nil
|
288
|
+
if title
|
289
|
+
@title = title.to_s
|
290
|
+
else
|
291
|
+
@title ||= name[/([^:]+)$/, 1]
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# The host for this Plugin class.
|
296
|
+
def plugin_host host = nil
|
297
|
+
if host and not host.is_a? PluginHost
|
298
|
+
raise ArgumentError,
|
299
|
+
"PluginHost expected, but #{host.class} given."
|
300
|
+
end
|
301
|
+
self.const_set :PLUGIN_HOST, host if host
|
302
|
+
self::PLUGIN_HOST
|
303
|
+
end
|
304
|
+
|
305
|
+
# Require some helper files.
|
232
306
|
#
|
233
|
-
#
|
307
|
+
# Example:
|
234
308
|
#
|
235
|
-
#
|
309
|
+
# class MyPlugin < PluginHost::BaseClass
|
310
|
+
# register_for :my_id
|
311
|
+
# helper :my_helper
|
236
312
|
#
|
237
|
-
#
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
# ...
|
248
|
-
# end
|
249
|
-
#
|
250
|
-
# See PluginHost.register.
|
251
|
-
def register_for id
|
252
|
-
@plugin_id = id
|
253
|
-
plugin_host.register self, id
|
254
|
-
end
|
255
|
-
|
256
|
-
# Returns the title of the plugin, or sets it to the
|
257
|
-
# optional argument +title+.
|
258
|
-
def title title = nil
|
259
|
-
if title
|
260
|
-
@title = title.to_s
|
313
|
+
# The above example loads the file myplugin/my_helper.rb relative to the
|
314
|
+
# file in which MyPlugin was defined.
|
315
|
+
#
|
316
|
+
# You can also load a helper from a different plugin:
|
317
|
+
#
|
318
|
+
# helper 'other_plugin/helper_name'
|
319
|
+
def helper *helpers
|
320
|
+
for helper in helpers
|
321
|
+
if helper.is_a?(String) && helper[/\//]
|
322
|
+
self::PLUGIN_HOST.require_helper $`, $'
|
261
323
|
else
|
262
|
-
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
# The PluginHost for this Plugin class.
|
267
|
-
def plugin_host host = nil
|
268
|
-
if host.is_a? PluginHost
|
269
|
-
const_set :PLUGIN_HOST, host
|
270
|
-
end
|
271
|
-
self::PLUGIN_HOST
|
272
|
-
end
|
273
|
-
|
274
|
-
def aliases
|
275
|
-
plugin_host.load_plugin_map
|
276
|
-
plugin_host.plugin_hash.inject [] do |aliases, (key, _)|
|
277
|
-
aliases << key if plugin_host[key] == self
|
278
|
-
aliases
|
324
|
+
self::PLUGIN_HOST.require_helper plugin_id.to_s, helper.to_s
|
279
325
|
end
|
280
326
|
end
|
281
|
-
|
282
327
|
end
|
283
|
-
|
328
|
+
|
329
|
+
# Returns the pulgin id used by the engine.
|
330
|
+
def plugin_id
|
331
|
+
@plugin_id || name[/\w+$/].downcase
|
332
|
+
end
|
333
|
+
|
334
|
+
end
|
335
|
+
|
336
|
+
# Convenience method for plugin loading.
|
337
|
+
# The syntax used is:
|
338
|
+
#
|
339
|
+
# CodeRay.require_plugin '<Host ID>/<Plugin ID>'
|
340
|
+
#
|
341
|
+
# Returns the loaded plugin.
|
342
|
+
def self.require_plugin path
|
343
|
+
host_id, plugin_id = path.split '/', 2
|
344
|
+
host = PluginHost.host_by_id(host_id)
|
345
|
+
raise PluginHost::HostNotFound,
|
346
|
+
"No host for #{host_id.inspect} found." unless host
|
347
|
+
host.load plugin_id
|
284
348
|
end
|
349
|
+
|
350
|
+
end
|