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
@@ -0,0 +1,41 @@
|
|
1
|
+
module CodeRay
|
2
|
+
|
3
|
+
# A simplified interface to the gzip library +zlib+ (from the Ruby Standard Library.)
|
4
|
+
module GZip
|
5
|
+
|
6
|
+
require 'zlib'
|
7
|
+
|
8
|
+
# The default zipping level. 7 zips good and fast.
|
9
|
+
DEFAULT_GZIP_LEVEL = 7
|
10
|
+
|
11
|
+
# Unzips the given string +s+.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
# require 'gzip_simple'
|
15
|
+
# print GZip.gunzip(File.read('adresses.gz'))
|
16
|
+
def GZip.gunzip s
|
17
|
+
Zlib::Inflate.inflate s
|
18
|
+
end
|
19
|
+
|
20
|
+
# Zips the given string +s+.
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
# require 'gzip_simple'
|
24
|
+
# File.open('adresses.gz', 'w') do |file
|
25
|
+
# file.write GZip.gzip('Mum: 0123 456 789', 9)
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# If you provide a +level+, you can control how strong
|
29
|
+
# the string is compressed:
|
30
|
+
# - 0: no compression, only convert to gzip format
|
31
|
+
# - 1: compress fast
|
32
|
+
# - 7: compress more, but still fast (default)
|
33
|
+
# - 8: compress more, slower
|
34
|
+
# - 9: compress best, very slow
|
35
|
+
def GZip.gzip s, level = DEFAULT_GZIP_LEVEL
|
36
|
+
Zlib::Deflate.new(level).deflate s, Zlib::FINISH
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -1,349 +1,284 @@
|
|
1
1
|
module CodeRay
|
2
2
|
|
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+.
|
3
|
+
# = PluginHost
|
4
|
+
#
|
5
|
+
# A simple subclass/subfolder plugin system.
|
43
6
|
#
|
44
7
|
# Example:
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
8
|
+
# class Generators
|
9
|
+
# extend PluginHost
|
10
|
+
# plugin_path 'app/generators'
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# class Generator
|
14
|
+
# extend Plugin
|
15
|
+
# PLUGIN_HOST = Generators
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class FancyGenerator < Generator
|
19
|
+
# register_for :fancy
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# Generators[:fancy] #-> FancyGenerator
|
23
|
+
# # or
|
24
|
+
# CodeRay.require_plugin 'Generators/fancy'
|
25
|
+
# # or
|
26
|
+
# Generators::Fancy
|
27
|
+
module PluginHost
|
28
|
+
|
29
|
+
# Raised if Encoders::[] fails because:
|
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
|
67
43
|
end
|
68
|
-
|
69
|
-
#
|
70
|
-
|
71
|
-
|
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
|
72
55
|
end
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|
67
|
+
end
|
68
|
+
|
69
|
+
class << self
|
70
|
+
|
71
|
+
# Adds the module/class to the PLUGIN_HOSTS list.
|
72
|
+
def extended mod
|
73
|
+
PLUGIN_HOSTS << mod
|
84
74
|
end
|
85
|
-
|
75
|
+
|
86
76
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
@plugin_path
|
94
|
-
load_map
|
77
|
+
|
78
|
+
# The path where the plugins can be found.
|
79
|
+
def plugin_path *args
|
80
|
+
unless args.empty?
|
81
|
+
@plugin_path = File.expand_path File.join(*args)
|
82
|
+
end
|
83
|
+
@plugin_path ||= ''
|
95
84
|
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
85
|
+
|
86
|
+
# Map a plugin_id to another.
|
87
|
+
#
|
88
|
+
# Usage: Put this in a file plugin_path/_map.rb.
|
89
|
+
#
|
90
|
+
# class MyColorHost < PluginHost
|
91
|
+
# map :navy => :dark_blue,
|
92
|
+
# :maroon => :brown,
|
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
|
107
101
|
end
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
102
|
+
|
103
|
+
# Define the default plugin to use when no plugin is found
|
104
|
+
# for a given id, or return the default plugin.
|
105
|
+
#
|
106
|
+
# See also map.
|
107
|
+
#
|
108
|
+
# class MyColorHost < PluginHost
|
109
|
+
# map :navy => :dark_blue
|
110
|
+
# default :gray
|
111
|
+
# end
|
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
|
124
122
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
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]
|
123
|
+
|
124
|
+
# Every plugin must register itself for +id+ by calling register_for,
|
125
|
+
# which calls this method.
|
126
|
+
#
|
127
|
+
# See Plugin#register_for.
|
128
|
+
def register plugin, id
|
129
|
+
plugin_hash[validate_id(id)] = plugin
|
142
130
|
end
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
131
|
+
|
132
|
+
# A Hash of plugion_id => Plugin pairs.
|
133
|
+
def plugin_hash
|
134
|
+
@plugin_hash ||= make_plugin_hash
|
135
|
+
end
|
136
|
+
|
137
|
+
# Returns an array of all .rb files in the plugin path.
|
138
|
+
#
|
139
|
+
# The extension .rb is not included.
|
140
|
+
def list
|
141
|
+
Dir[path_to('*')].select do |file|
|
142
|
+
File.basename(file)[/^(?!_)\w+\.rb$/]
|
143
|
+
end.map do |file|
|
144
|
+
File.basename(file, '.rb').to_sym
|
154
145
|
end
|
155
|
-
plugin_hash[validate_id(id)] = plugin
|
156
146
|
end
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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'
|
147
|
+
|
148
|
+
# Returns an array of all Plugins.
|
149
|
+
#
|
150
|
+
# Note: This loads all plugins using load_all.
|
151
|
+
def all_plugins
|
152
|
+
load_all
|
153
|
+
plugin_hash.values.grep(Class)
|
172
154
|
end
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
155
|
+
|
156
|
+
# Loads the map file (see map).
|
157
|
+
#
|
158
|
+
# This is done automatically when plugin_path is called.
|
159
|
+
def load_plugin_map
|
160
|
+
mapfile = path_to '_map'
|
161
|
+
@plugin_map_loaded = true
|
162
|
+
if File.exist? mapfile
|
163
|
+
require mapfile
|
164
|
+
true
|
165
|
+
else
|
166
|
+
false
|
167
|
+
end
|
180
168
|
end
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
@plugin_hash =
|
169
|
+
|
170
|
+
protected
|
171
|
+
|
172
|
+
# Return a plugin hash that automatically loads plugins.
|
173
|
+
def make_plugin_hash
|
174
|
+
@plugin_map_loaded ||= false
|
188
175
|
Hash.new do |h, plugin_id|
|
189
176
|
id = validate_id(plugin_id)
|
190
177
|
path = path_to id
|
191
178
|
begin
|
179
|
+
raise LoadError, "#{path} not found" unless File.exist? path
|
192
180
|
require path
|
193
181
|
rescue LoadError => boom
|
194
|
-
if
|
195
|
-
|
182
|
+
if @plugin_map_loaded
|
183
|
+
if h.has_key?(:default)
|
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
|
196
189
|
else
|
197
|
-
|
190
|
+
load_plugin_map
|
191
|
+
h[plugin_id]
|
198
192
|
end
|
199
193
|
else
|
200
194
|
# Plugin should have registered by now
|
201
|
-
|
202
|
-
|
203
|
-
|
195
|
+
if h.has_key? id
|
196
|
+
h[id]
|
197
|
+
else
|
198
|
+
raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
|
204
199
|
end
|
205
200
|
end
|
206
|
-
h[id]
|
207
201
|
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
|
219
202
|
end
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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
|
203
|
+
|
204
|
+
# Returns the expected path to the plugin file for the given id.
|
205
|
+
def path_to plugin_id
|
206
|
+
File.join plugin_path, "#{plugin_id}.rb"
|
207
|
+
end
|
208
|
+
|
209
|
+
# Converts +id+ to a Symbol if it is a String,
|
210
|
+
# or returns +id+ if it already is a Symbol.
|
211
|
+
#
|
212
|
+
# Raises +ArgumentError+ for all other objects, or if the
|
213
|
+
# given String includes non-alphanumeric characters (\W).
|
214
|
+
def validate_id id
|
215
|
+
if id.is_a? Symbol or id.nil?
|
216
|
+
id
|
217
|
+
elsif id.is_a? String
|
218
|
+
if id[/\w+/] == id
|
219
|
+
id.downcase.to_sym
|
220
|
+
else
|
221
|
+
raise ArgumentError, "Invalid id given: #{id}"
|
222
|
+
end
|
247
223
|
else
|
248
|
-
raise ArgumentError, "
|
224
|
+
raise ArgumentError, "String or Symbol expected, but #{id.class} given."
|
249
225
|
end
|
250
|
-
else
|
251
|
-
raise ArgumentError,
|
252
|
-
"String or Symbol expected, but #{id.class} given."
|
253
226
|
end
|
254
|
-
|
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_host.register self, *ids
|
227
|
+
|
282
228
|
end
|
283
229
|
|
284
|
-
|
285
|
-
#
|
286
|
-
def title title = nil
|
287
|
-
if title
|
288
|
-
@title = title.to_s
|
289
|
-
else
|
290
|
-
@title ||= name[/([^:]+)$/, 1]
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
# The host for this Plugin class.
|
295
|
-
def plugin_host host = nil
|
296
|
-
if host and not host.is_a? PluginHost
|
297
|
-
raise ArgumentError,
|
298
|
-
"PluginHost expected, but #{host.class} given."
|
299
|
-
end
|
300
|
-
self.const_set :PLUGIN_HOST, host if host
|
301
|
-
self::PLUGIN_HOST
|
302
|
-
end
|
303
|
-
|
304
|
-
# Require some helper files.
|
230
|
+
|
231
|
+
# = Plugin
|
305
232
|
#
|
306
|
-
#
|
233
|
+
# Plugins have to include this module.
|
307
234
|
#
|
308
|
-
#
|
309
|
-
# register_for :my_id
|
310
|
-
# helper :my_helper
|
235
|
+
# IMPORTANT: Use extend for this module.
|
311
236
|
#
|
312
|
-
#
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
237
|
+
# See CodeRay::PluginHost for examples.
|
238
|
+
module Plugin
|
239
|
+
|
240
|
+
attr_reader :plugin_id
|
241
|
+
|
242
|
+
# Register this class for the given +id+.
|
243
|
+
#
|
244
|
+
# Example:
|
245
|
+
# class MyPlugin < PluginHost::BaseClass
|
246
|
+
# register_for :my_id
|
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
|
322
261
|
else
|
323
|
-
|
262
|
+
@title ||= name[/([^:]+)$/, 1]
|
324
263
|
end
|
325
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
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
326
282
|
end
|
327
|
-
|
328
|
-
# Returns the pulgin id used by the engine.
|
329
|
-
def plugin_id
|
330
|
-
name[/\w+$/].downcase
|
331
|
-
end
|
332
|
-
|
333
|
-
end
|
334
|
-
|
335
|
-
# Convenience method for plugin loading.
|
336
|
-
# The syntax used is:
|
337
|
-
#
|
338
|
-
# CodeRay.require_plugin '<Host ID>/<Plugin ID>'
|
339
|
-
#
|
340
|
-
# Returns the loaded plugin.
|
341
|
-
def self.require_plugin path
|
342
|
-
host_id, plugin_id = path.split '/', 2
|
343
|
-
host = PluginHost.host_by_id(host_id)
|
344
|
-
raise PluginHost::HostNotFound,
|
345
|
-
"No host for #{host_id.inspect} found." unless host
|
346
|
-
host.load plugin_id
|
283
|
+
|
347
284
|
end
|
348
|
-
|
349
|
-
end
|