grn_mini 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b9f025b3d3dfc38fd85b82b8552b9df10244dd8
4
+ data.tar.gz: 2294e3e7af33ffd32d15253d01572bbeff84bbb3
5
+ SHA512:
6
+ metadata.gz: 44faf36a48ca1245ddd3621955b998295a7e608e1f6ddb98229374391374cb18919c4883c0a3c0517fa4cee9eb9034a213e3a87ba271f3f4c2611f3454015c9b
7
+ data.tar.gz: 68bd08647aa71e471072aec681a547821f06c111fcac909309545986f0898b9046d365fee677bc4334047772f844a286a673a39b738ff0317a2e8001160a619b
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.db*
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # GrnMini
2
2
 
3
- Groonga(Rroonga) wrapper for using easily. It is the KVS so easy to use.
3
+ Groonga(Rroonga) wrapper for use as the KVS.
4
+
5
+ Groonga(Rroonga) for using easily. You can add the data in the column without specifying. You can easy to use and persistence, advanced search query, sort, grouping (drill down), snippets, and pagination. You can make an immediate search engine.
4
6
 
5
7
  ## Installation
6
8
 
@@ -31,6 +33,14 @@ array << {text: "ccc", number: 3}
31
33
  array.size #=> 3
32
34
  ```
33
35
 
36
+ ### Open an existing database
37
+
38
+ ```ruby
39
+ require 'grn_mini'
40
+ array = GrnMini::Array.new("test.db")
41
+ array.size #=> 3
42
+ ```
43
+
34
44
  ### Create a temporary database. (Useful for testing)
35
45
 
36
46
  ```ruby
data/grn_mini.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = GrnMini::VERSION
9
9
  spec.authors = ["ongaeshi"]
10
10
  spec.email = ["ongaeshi0621@gmail.com"]
11
- spec.description = %q{Groonga(Rroonga) wrapper for using easily.}
12
- spec.summary = %q{It is the KVS it's so easy to use.}
11
+ spec.summary = %q{Groonga(Rroonga) wrapper for use as the KVS.}
12
+ spec.description = %q{Groonga(Rroonga) for using easily. You can add the data in the column without specifying. You can easy to use and persistence, advanced search query, sort, grouping (drill down), snippets, and pagination. You can make an immediate search engine.}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
data/lib/grn_mini/util.rb CHANGED
@@ -9,11 +9,11 @@ module GrnMini
9
9
  end
10
10
 
11
11
  def text_snippet_from_selection_results(table, open_tag = '<<', close_tag = ">>")
12
- table.expression.snippet([[open_tag, close_tag]])
12
+ table.expression.snippet([[open_tag, close_tag]], {normalize: true})
13
13
  end
14
14
 
15
15
  def html_snippet_from_selection_results(table, open_tag = '<strong>', close_tag = "</strong>")
16
- table.expression.snippet([[open_tag, close_tag]], {html_escape: true})
16
+ table.expression.snippet([[open_tag, close_tag]], {html_escape: true, normalize: true})
17
17
  end
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module GrnMini
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,153 @@
1
+ require 'grn_mini'
2
+ require 'find'
3
+ require 'kconv'
4
+ require 'sinatra'
5
+ require "sinatra/reloader" if ENV['SINATRA_RELOADER']
6
+
7
+ module Input
8
+ module_function
9
+
10
+ def from_dir(array, dir = ".")
11
+ index = 1
12
+ puts "Create database .."
13
+ Find.find(File.expand_path(dir)) do |filename|
14
+ Find.prune if ignore_dir?(filename)
15
+
16
+ if File.file? filename
17
+ next if ignore_file?(filename)
18
+ array << {filename: filename, text: read_file(filename), timestamp: File.stat(filename).mtime, suffix: File.extname(filename).sub('.', ""), index: index } # index is dirty
19
+ index += 1
20
+ end
21
+ end
22
+ puts "Input complete : #{array.size} files"
23
+ end
24
+
25
+ def read_file(filename)
26
+ text = File.read(filename)
27
+ Kconv.kconv(text, Kconv::UTF8).gsub("\r\n", "\n")
28
+ end
29
+
30
+ def ignore_dir?(filename)
31
+ basename = File.basename(filename)
32
+ /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/.match(basename)
33
+ end
34
+
35
+ def ignore_file?(filename)
36
+ (s = File.read(filename, 1024)) && s.index("\x00")
37
+ end
38
+ end
39
+
40
+ class Search
41
+ def initialize(array, params)
42
+ @array = array
43
+ @params = params
44
+ @page = @params[:page] ? @params[:page].to_i : 1
45
+ end
46
+
47
+ def has_query?
48
+ @params[:query] && !@params[:query].empty?
49
+ end
50
+
51
+ def parse
52
+ unless has_query?
53
+ @header = "<span>#{@array.size} files.</span>"
54
+ @content = ""
55
+ @pagination = ""
56
+ else
57
+ results = @array.select(@params[:query])
58
+
59
+ page_entries = results.paginate([["_score", :desc]], :page => @page, :size => 20)
60
+ snippet = GrnMini::Util::html_snippet_from_selection_results(results, "<strong style=\"background-color: #FFEE55\">", "</strong>")
61
+ elements = []
62
+
63
+ page_entries.each do |record|
64
+ element = "<hr>\n<a href=\"/#{record.index}\">#{record.filename}</a>\n"
65
+
66
+ snippet.execute(record.text).each do |segment|
67
+ element += "<pre style=\"border:1px solid #bbb;\">#{segment}</pre>\n"
68
+ end
69
+
70
+ elements << element
71
+ end
72
+
73
+ @header = "<span>#{page_entries.n_records} hit. (#{page_entries.start_offset} - #{page_entries.end_offset})</span>"
74
+ @content = elements.join("\n")
75
+
76
+ if page_entries.n_pages > 1
77
+ @pagination += page_link(@page - 1, "&lt;-") + "&nbsp;" if @page > 1
78
+
79
+ @pagination += page_range(page_entries).map {|v|
80
+ if (v == @page)
81
+ "<strong>#{v.to_s}</strong>"
82
+ else
83
+ page_link(v, v.to_s)
84
+ end
85
+ }.join("&nbsp;")
86
+
87
+ @pagination += "&nbsp;" + page_link(@page + 1, "-&gt;") if @page < page_entries.n_pages
88
+ end
89
+ end
90
+ end
91
+
92
+ def page_range(page_entries)
93
+ first_diff = [5 - (@page - 1), 0].max
94
+ last_diff = [5 - (page_entries.n_pages - @page), 0].max
95
+ [@page - 5 - last_diff, 1].max .. [@page + 5 + first_diff, page_entries.n_pages].min
96
+ end
97
+
98
+ def page_link(page, msg)
99
+ "<a href=\"/?query=#{@params[:query]}&page=#{page}\">#{msg}</a>"
100
+ end
101
+
102
+ def html
103
+ <<EOF
104
+ #{@header}
105
+ <div class="form">
106
+ <form method="post" action="/search">
107
+ <input type="text" style="width: 419px;" name="query" value="#{@params[:query]}">
108
+ <input type="submit" value="Search">
109
+ </form>
110
+ </div>
111
+ <div class="content">
112
+ #{@content}
113
+ </div>
114
+ <div class="pagination">
115
+ #{@pagination}
116
+ </div>
117
+ EOF
118
+ end
119
+ end
120
+
121
+ ### main ###
122
+ configure do
123
+ $array = GrnMini::Array.new("mini-directory-search.db")
124
+ Input.from_dir($array) if $array.empty?
125
+ end
126
+
127
+ get '/' do
128
+ search = Search.new($array, params)
129
+ search.parse
130
+ search.html
131
+ end
132
+
133
+ get '/:id' do
134
+ record = $array[params[:id].to_i]
135
+
136
+ <<EOF
137
+ <span>#{record.filename} (#{record.timestamp})</span>
138
+ <div class="form">
139
+ <form method="post" action="/search">
140
+ <input type="text" style="width: 419px;" name="query" value="#{@params[:query]}">
141
+ <input type="submit" value="Search">
142
+ </form>
143
+ </div>
144
+ <div class="content">
145
+ <hr>
146
+ <pre>#{CGI.escapeHTML(record.text)}</pre>
147
+ </div>
148
+ EOF
149
+ end
150
+
151
+ post '/search' do
152
+ redirect "/?query=#{escape(params[:query])}"
153
+ end
metadata CHANGED
@@ -1,61 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grn_mini
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - ongaeshi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-01-05 00:00:00.000000000Z
11
+ date: 2014-01-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
- requirement: &2156695780 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *2156695780
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rake
27
- requirement: &2156695360 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *2156695360
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: minitest
38
- requirement: &2156694900 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *2156694900
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: rroonga
49
- requirement: &2156694480 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :runtime
56
63
  prerelease: false
57
- version_requirements: *2156694480
58
- description: Groonga(Rroonga) wrapper for using easily.
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Groonga(Rroonga) for using easily. You can add the data in the column
70
+ without specifying. You can easy to use and persistence, advanced search query,
71
+ sort, grouping (drill down), snippets, and pagination. You can make an immediate
72
+ search engine.
59
73
  email:
60
74
  - ongaeshi0621@gmail.com
61
75
  executables: []
@@ -73,34 +87,34 @@ files:
73
87
  - lib/grn_mini/array.rb
74
88
  - lib/grn_mini/util.rb
75
89
  - lib/grn_mini/version.rb
90
+ - sample/mini-directory-search.rb
76
91
  - test/minitest_helper.rb
77
92
  - test/test_grn_mini.rb
78
93
  - test/test_grn_mini_array.rb
79
94
  homepage: ''
80
95
  licenses:
81
96
  - MIT
97
+ metadata: {}
82
98
  post_install_message:
83
99
  rdoc_options: []
84
100
  require_paths:
85
101
  - lib
86
102
  required_ruby_version: !ruby/object:Gem::Requirement
87
- none: false
88
103
  requirements:
89
- - - ! '>='
104
+ - - '>='
90
105
  - !ruby/object:Gem::Version
91
106
  version: '0'
92
107
  required_rubygems_version: !ruby/object:Gem::Requirement
93
- none: false
94
108
  requirements:
95
- - - ! '>='
109
+ - - '>='
96
110
  - !ruby/object:Gem::Version
97
111
  version: '0'
98
112
  requirements: []
99
113
  rubyforge_project:
100
- rubygems_version: 1.8.6
114
+ rubygems_version: 2.0.0
101
115
  signing_key:
102
- specification_version: 3
103
- summary: It is the KVS it's so easy to use.
116
+ specification_version: 4
117
+ summary: Groonga(Rroonga) wrapper for use as the KVS.
104
118
  test_files:
105
119
  - test/minitest_helper.rb
106
120
  - test/test_grn_mini.rb