This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docsearch.rb
executable file
·315 lines (266 loc) · 8.24 KB
/
docsearch.rb
1
2
3
4
5
6
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env ruby
require 'find'
require 'optparse'
require 'json'
require 'yaml'
module Message
RED = '31;1m'
BLUE = '34;1m'
GREEN = '32;1m'
YELLOW = '33;1m'
def Message.custom(state, message, color)
return "\033[#{color}#{state}\033[0m #{message}"
end
def Message.info(message)
return custom('[*]', message, BLUE)
end
def Message.success(message)
return custom('[+]', message, GREEN)
end
def Message.error(message)
return custom('[-]', message, RED)
end
def Message.matched(message, color)
return "\033[#{color}#{message}\033[0m"
end
end
class Result
attr_accessor :category
def initialize(topic, category, element, terms="")
@topic = topic
@category = category
@element = element
@terms = terms
end
def description
@element['description']
end
def data
@element['data']
end
def print_text(colored=true, match_colored=false)
cat = self.get_category()
# Description with topic and category colored (or not)
unless colored
message = "#{cat} #{@topic}: #{@element['description']}"
else
message = Message.custom("[#{@topic}]", "#{@element['description']}", Message::YELLOW)
end
# Matched terms colored in description
unless @terms.empty? or !match_colored
message.gsub!(/#{@terms}/, Message.matched("#{@terms}", Message::RED))
end
# Final display
puts message
@element['data'].each do |data|
puts "- #{data}"
end
puts ""
end
def get_category
ret_category = '#'
category_mapping = {
'cheats' => '#',
'links' => '*',
'glossary' => '%'
}
begin
return category_mapping[@category]
rescue IndexError
ret_category = '#'
end
return ret_category
end
end
class Docsearch
def initialize(options)
@opts = options
@paths = ENV['DOCSEARCH_PATH'].split(':')
@results = []
@env = ENV.keys().map { |e| e if e =~ /^DOCSEARCH_/}.compact
end
def yaml_content(path)
begin
content = YAML.load(File.read(path))
return content
rescue
return false
end
end
def search(file)
content = self.yaml_content(file)
# because we are in a loop fetching files from different locations,
# we don't want to break the loop by exiting.
return if content == false
# if we just want to see the resources path
if content and @opts[:pwd]
puts "#{file}"
return
end
# Filters rules
if @opts[:links]
content.delete('cheats')
content.delete('glossary')
elsif @opts[:cheats]
content.delete('links')
content.delete('glossary')
elsif @opts[:glossary]
content.delete('cheats')
content.delete('links')
end
content.each do |category, elements|
elements.each do |element|
topic = File.basename(file)[0..-6]
if @opts[:terms]
terms = @opts[:terms]
if element['description'] =~ /(#{terms})/
@results.push(Result.new(topic, category, element, terms))
end
else
@results.push(Result.new(topic, category, element))
end
end
end
end
def topic?(filename)
[".yaml"].include? File.extname(filename)
end
def topics_inventory
inventory = {}
# Retrieve YAML files for paths
@paths.each do |path|
inventory[path] ||= []
Find.find(path) do |file|
if self.topic?(file)
topic = File.basename(file)[0..-6]
inventory[path].push(topic)
end
end
end
# Display results
inventory.each do |path, topics|
puts Message.info(path)
topics.each do |topic|
puts "#{topic}"
end
puts ""
end
end
def show_env
@env.each do |envvar|
if envvar == "DOCSEARCH_PATH"
puts Message.info(envvar)
@paths.each do |path|
puts "#{path}"
end
elsif envvar == "DOCSEARCH_COLORED"
puts Message.info(envvar)
color_mode = ENV[envvar].to_i == 1 ? 'enabled' : 'disabled'
puts "color mode #{color_mode}"
end
puts ""
end
end
def dispatcher
if @opts[:inventory]
self.topics_inventory
elsif @opts[:env]
self.show_env
elsif @opts[:topic] and not @opts[:terms]
# display all contents
@paths.each do |path|
file = File.join(path, @opts[:topic])
file = "#{file}.yaml"
self.search(file)
end
elsif @opts[:terms]
# perform classic search
@paths.each do |path|
if @opts[:topic]
file = File.join(path, @opts[:topic])
file = "#{file}.yaml"
self.search(file)
else
# Find terms in all files
Find.find(path) do |file|
# if we found a file with .yaml extension
if self.topic?(file)
self.search(file)
end
end
end
end
end
end
def print_results(colored, match_colored)
if @opts[:json] and @results.length > 0
res = @results.sort_by {|r| r.category}
j = {}
res.each do |r|
if ! j.keys().include?(r.category)
j[r.category] = []
end
j[r.category] << { 'description': r.description, 'data': r.data }
end
puts j.to_json
return
end
@results.each do |result|
result.print_text(colored, match_colored)
end
end
end
def main()
if ! ENV.include?('DOCSEARCH_PATH')
puts Message.error("You need to declare DOCSEARCH_PATH environment variable.")
exit 2
end
options = { :colored => false, :match_colored => false, :json => false }
if ENV.include?('DOCSEARCH_COLORED')
options[:colored] = true
end
if ENV.include?('DOCSEARCH_MATCH_COLORED')
options[:match_colored] = true
end
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [ FILTERS ] -s PATTERN"
opts.on('-C', '--cheats', 'Restrict search on cheatsheets terms') do |cheats|
options[:cheats] = cheats
end
opts.on('-G', '--glossary', 'Restrict search on glossary terms') do |glossary|
options[:glossary] = glossary
end
opts.on('-L', '--links', 'Restrict search on links terms') do |links|
options[:links] = links
end
opts.on('-e', '--env', 'Show useful DOCSEARCH_* environment variables') do |env|
options[:env] = true
end
opts.on('-c', '--colored', 'Enable colored output') do |nocolor|
options[:colored] = true
end
opts.on('-i', '--inventory', 'List all availabled topics') do |inventory|
options[:inventory] = true
end
opts.on('-j', '--json', 'JSON output') do |jsonformat|
options[:json] = true
end
opts.on('-p', '--pwd', 'Show matched file found') do |path|
options[:pwd] = true
end
opts.on('-m', '--match-colored', 'Enable colored match') do |match_colored|
options[:match_colored] = true
end
opts.on('-s', '--search terms', 'Keyword or term to search') do |terms|
options[:terms] = terms
end
opts.on('-t', '--topic topic', 'Search on a specific topic') do |topic|
options[:topic] = topic
end
end.parse!
paths = ENV['DOCSEARCH_PATH'].split(':')
docsearch = Docsearch.new(options)
docsearch.dispatcher
docsearch.print_results(options[:colored], options[:match_colored])
end
main