fluorescent 0.0.0
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.
- checksums.yaml +7 -0
- data/lib/fluorescent.rb +50 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72c5cd15896224a1cf0a08effeb7832f9aae0b11
|
4
|
+
data.tar.gz: ca91915607c6f19ffa9725c3927c62dca97f1fc2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e39e92d3017940e327038eb06b20f6eec874b77fcc0dda85410d95b9d16b47d1689052faca9bf6b770c444e198f76b64d6046484bc3e4de33eecd779e0b32ccf
|
7
|
+
data.tar.gz: 1d6b9592d8b14f3bc1fdcc1c011b168dca0f83b1c5a0423cfb63aa2128b50fbc3c73c3fdb35c6148e58500b68915cf11522d9ed52cac17ed57c13a153f3a3462
|
data/lib/fluorescent.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Fluorescent
|
2
|
+
attr_reader :columns, :to_filter
|
3
|
+
def initialize args
|
4
|
+
# number of characters to display before/after search terms
|
5
|
+
# example:
|
6
|
+
# padding: 10
|
7
|
+
# search terms: "this is a test"
|
8
|
+
# search results: "what do you think this is a test"
|
9
|
+
# padded display: "...t do you think this is a test..."
|
10
|
+
@padding = 10
|
11
|
+
@formatted_results = []
|
12
|
+
args.each do |k,v|
|
13
|
+
instance_variable_set("@#{k}", v) unless v.nil?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# distill search result down to n characters before and after
|
18
|
+
# search terms
|
19
|
+
def distill
|
20
|
+
@results.each do |r|
|
21
|
+
row = {}
|
22
|
+
@columns.each do |c|
|
23
|
+
# 1. highlight the search terms
|
24
|
+
# 2. replace the search terms in the results with bold text
|
25
|
+
# index starting at the first character of the search terms
|
26
|
+
# to the length of the search terms + the padding config value
|
27
|
+
string = r.send(c).to_s
|
28
|
+
row[c] = highlight string # need to find a better way to do this
|
29
|
+
if @to_filter.include? c
|
30
|
+
row[c] = highlight string[
|
31
|
+
string.index(@terms[0]),
|
32
|
+
string.index(@terms[0]) + @terms.length + @padding
|
33
|
+
] << "..."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
@formatted_results.push({:id =>r.id}.merge(row))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# highlight the search terms in the result
|
41
|
+
def highlight(string)
|
42
|
+
string.gsub @terms, "<b>#{@terms}</b>"
|
43
|
+
end
|
44
|
+
|
45
|
+
def formatted_results
|
46
|
+
distill
|
47
|
+
@formatted_results
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluorescent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Devin Austin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Highlight search terms in a result set and distill the result text down
|
14
|
+
to the pertinent parts
|
15
|
+
email: devin.austin@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/fluorescent.rb
|
21
|
+
homepage: http://rubygems.org/gems/fluorescent
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.0.7
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Highlight and distill search terms.
|
45
|
+
test_files: []
|