codestock 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/Rakefile +1 -0
  2. data/VERSION +1 -1
  3. data/bin/cdweb +2 -2
  4. data/codestock.gemspec +56 -10
  5. data/lib/cdstk/cdstk.rb +4 -1
  6. data/lib/codestock/cdweb/app.rb +136 -0
  7. data/lib/codestock/cdweb/cli_cdweb.rb +84 -0
  8. data/lib/codestock/cdweb/config.ru +3 -0
  9. data/lib/codestock/cdweb/lib/coderay_wrapper.rb +94 -0
  10. data/lib/codestock/cdweb/lib/command.rb +67 -0
  11. data/lib/{cdweb → codestock/cdweb/lib}/database.rb +55 -28
  12. data/lib/{cdweb → codestock/cdweb/lib}/grep.rb +24 -8
  13. data/lib/codestock/cdweb/lib/mkurl.rb +56 -0
  14. data/lib/{cdweb → codestock/cdweb/lib}/query.rb +3 -4
  15. data/lib/codestock/cdweb/lib/search_contents.rb +117 -0
  16. data/lib/codestock/cdweb/lib/search_files.rb +88 -0
  17. data/lib/{cdweb → codestock/cdweb}/public/css/gren.css +16 -1
  18. data/lib/codestock/cdweb/public/images/directory.png +0 -0
  19. data/lib/codestock/cdweb/public/images/file.png +0 -0
  20. data/lib/codestock/cdweb/views/filelist.haml +14 -0
  21. data/lib/codestock/cdweb/views/help.haml +16 -0
  22. data/lib/codestock/cdweb/views/index.haml +17 -0
  23. data/lib/codestock/cdweb/views/layout.haml +33 -0
  24. data/lib/codestock/cdweb/views/search.haml +12 -0
  25. data/lib/codestock/cdweb/views/view.haml +16 -0
  26. data/test/file_assert.rb +41 -0
  27. data/test/test_coderay_wrapper.rb +22 -0
  28. data/test/test_coderay_wrapper_data.rb +91 -0
  29. data/test/test_database.rb +54 -0
  30. data/test/test_mkurl.rb +30 -0
  31. data/test/test_query.rb +54 -0
  32. metadata +52 -26
  33. data/lib/cdweb/cli_cdweb.rb +0 -54
  34. data/lib/cdweb/coderay_wrapper.rb +0 -38
  35. data/lib/cdweb/grenweb.ru +0 -35
  36. data/lib/cdweb/help.rb +0 -40
  37. data/lib/cdweb/home.rb +0 -40
  38. data/lib/cdweb/html_renderer.rb +0 -243
  39. data/lib/cdweb/searcher.rb +0 -130
  40. data/lib/cdweb/viewer.rb +0 -52
  41. data/test/test_grenweb_database.rb +0 -36
  42. data/test/test_grenweb_html_renderer.rb +0 -41
  43. data/test/test_grenweb_query.rb +0 -54
  44. data/test/test_grenweb_searcher.rb +0 -35
  45. /data/lib/{cdweb → codestock/cdweb}/public/css/coderay.css +0 -0
  46. /data/lib/{cdweb → codestock/cdweb}/public/images/gren-icon-mini.png +0 -0
  47. /data/lib/{cdweb → codestock/cdweb}/public/images/gren-icon.png +0 -0
data/Rakefile CHANGED
@@ -27,6 +27,7 @@ Jeweler::Tasks.new do |gem|
27
27
  gem.add_runtime_dependency 'rack','>=1.2.1'
28
28
  gem.add_runtime_dependency 'launchy', '>=0.3.7'
29
29
  gem.add_runtime_dependency 'coderay', '>=0.9.8'
30
+ gem.add_runtime_dependency 'thin', '>=1.2.10'
30
31
 
31
32
  # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
32
33
  # gem.add_development_dependency 'rspec', '> 1.2.3'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/bin/cdweb CHANGED
@@ -4,7 +4,7 @@
4
4
  # Copyright (c) 2010. All rights reserved.
5
5
 
6
6
  require 'rubygems'
7
- require 'cdweb/cli_cdweb'
7
+ require 'codestock/cdweb/cli_cdweb'
8
8
 
9
- Version = "0.1.0"
9
+ Version = "0.1.3"
10
10
  CodeStock::CLI_Cdweb.execute(STDOUT, ARGV)
data/codestock.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{codestock}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ongaeshi"]
12
- s.date = %q{2011-06-08}
12
+ s.date = %q{2011-07-20}
13
13
  s.description = %q{longer description of your gem}
14
14
  s.email = %q{ongaeshi0621@gmail.com}
15
15
  s.executables = ["cdweb", "cdv", "cdstk", "cdview"]
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  ".document",
22
22
  "Gemfile",
23
23
  "Gemfile.lock",
24
+ "HISTORY.rdoc",
24
25
  "LICENSE.txt",
25
26
  "README.rdoc",
26
27
  "Rakefile",
@@ -30,7 +31,34 @@ Gem::Specification.new do |s|
30
31
  "bin/cdview",
31
32
  "bin/cdweb",
32
33
  "codestock.gemspec",
34
+ "lib/cdstk/cdstk.rb",
35
+ "lib/cdstk/cdstk_yaml.rb",
36
+ "lib/cdstk/cli_cdstk.rb",
37
+ "lib/cdview/cli_cdview.rb",
33
38
  "lib/codestock.rb",
39
+ "lib/codestock/cdweb/app.rb",
40
+ "lib/codestock/cdweb/cli_cdweb.rb",
41
+ "lib/codestock/cdweb/config.ru",
42
+ "lib/codestock/cdweb/lib/coderay_wrapper.rb",
43
+ "lib/codestock/cdweb/lib/command.rb",
44
+ "lib/codestock/cdweb/lib/database.rb",
45
+ "lib/codestock/cdweb/lib/grep.rb",
46
+ "lib/codestock/cdweb/lib/mkurl.rb",
47
+ "lib/codestock/cdweb/lib/query.rb",
48
+ "lib/codestock/cdweb/lib/search_contents.rb",
49
+ "lib/codestock/cdweb/lib/search_files.rb",
50
+ "lib/codestock/cdweb/public/css/coderay.css",
51
+ "lib/codestock/cdweb/public/css/gren.css",
52
+ "lib/codestock/cdweb/public/images/directory.png",
53
+ "lib/codestock/cdweb/public/images/file.png",
54
+ "lib/codestock/cdweb/public/images/gren-icon-mini.png",
55
+ "lib/codestock/cdweb/public/images/gren-icon.png",
56
+ "lib/codestock/cdweb/views/filelist.haml",
57
+ "lib/codestock/cdweb/views/help.haml",
58
+ "lib/codestock/cdweb/views/index.haml",
59
+ "lib/codestock/cdweb/views/layout.haml",
60
+ "lib/codestock/cdweb/views/search.haml",
61
+ "lib/codestock/cdweb/views/view.haml",
34
62
  "lib/common/dbdir.rb",
35
63
  "lib/common/display_util.rb",
36
64
  "lib/common/grenfiletest.rb",
@@ -40,15 +68,21 @@ Gem::Specification.new do |s|
40
68
  "lib/common/util.rb",
41
69
  "lib/findgrep/findgrep.rb",
42
70
  "lib/findgrep/result.rb",
71
+ "test/file_assert.rb",
43
72
  "test/file_test_utils.rb",
73
+ "test/rake_test_loader.rb",
44
74
  "test/runner.rb",
75
+ "test/test_bin_exec.rb",
76
+ "test/test_cdstk.rb",
77
+ "test/test_cdstk_yaml.rb",
78
+ "test/test_coderay_wrapper.rb",
79
+ "test/test_coderay_wrapper_data.rb",
80
+ "test/test_database.rb",
45
81
  "test/test_dbdir.rb",
46
82
  "test/test_gren_util.rb",
47
- "test/test_grenweb_database.rb",
48
- "test/test_grenweb_html_renderer.rb",
49
- "test/test_grenweb_query.rb",
50
- "test/test_grenweb_searcher.rb",
51
83
  "test/test_helper.rb",
84
+ "test/test_mkurl.rb",
85
+ "test/test_query.rb",
52
86
  "test/test_string_snip.rb"
53
87
  ]
54
88
  s.homepage = %q{http://github.com/ongaeshi/codestock}
@@ -57,15 +91,21 @@ Gem::Specification.new do |s|
57
91
  s.rubygems_version = %q{1.3.6}
58
92
  s.summary = %q{one-line summary of your gem}
59
93
  s.test_files = [
94
+ "test/file_assert.rb",
60
95
  "test/file_test_utils.rb",
96
+ "test/rake_test_loader.rb",
61
97
  "test/runner.rb",
98
+ "test/test_bin_exec.rb",
99
+ "test/test_cdstk.rb",
100
+ "test/test_cdstk_yaml.rb",
101
+ "test/test_coderay_wrapper.rb",
102
+ "test/test_coderay_wrapper_data.rb",
103
+ "test/test_database.rb",
62
104
  "test/test_dbdir.rb",
63
105
  "test/test_gren_util.rb",
64
- "test/test_grenweb_database.rb",
65
- "test/test_grenweb_html_renderer.rb",
66
- "test/test_grenweb_query.rb",
67
- "test/test_grenweb_searcher.rb",
68
106
  "test/test_helper.rb",
107
+ "test/test_mkurl.rb",
108
+ "test/test_query.rb",
69
109
  "test/test_string_snip.rb"
70
110
  ]
71
111
 
@@ -81,6 +121,8 @@ Gem::Specification.new do |s|
81
121
  s.add_runtime_dependency(%q<rroonga>, [">= 1.0.0"])
82
122
  s.add_runtime_dependency(%q<rack>, [">= 1.2.1"])
83
123
  s.add_runtime_dependency(%q<launchy>, [">= 0.3.7"])
124
+ s.add_runtime_dependency(%q<coderay>, [">= 0.9.8"])
125
+ s.add_runtime_dependency(%q<thin>, [">= 1.2.10"])
84
126
  else
85
127
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
86
128
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -89,6 +131,8 @@ Gem::Specification.new do |s|
89
131
  s.add_dependency(%q<rroonga>, [">= 1.0.0"])
90
132
  s.add_dependency(%q<rack>, [">= 1.2.1"])
91
133
  s.add_dependency(%q<launchy>, [">= 0.3.7"])
134
+ s.add_dependency(%q<coderay>, [">= 0.9.8"])
135
+ s.add_dependency(%q<thin>, [">= 1.2.10"])
92
136
  end
93
137
  else
94
138
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -98,6 +142,8 @@ Gem::Specification.new do |s|
98
142
  s.add_dependency(%q<rroonga>, [">= 1.0.0"])
99
143
  s.add_dependency(%q<rack>, [">= 1.2.1"])
100
144
  s.add_dependency(%q<launchy>, [">= 0.3.7"])
145
+ s.add_dependency(%q<coderay>, [">= 0.9.8"])
146
+ s.add_dependency(%q<thin>, [">= 1.2.10"])
101
147
  end
102
148
  end
103
149
 
data/lib/cdstk/cdstk.rb CHANGED
@@ -10,6 +10,7 @@ require 'cdstk/cdstk_yaml'
10
10
  require 'common/grenfiletest'
11
11
  require 'common/util'
12
12
  include CodeStock
13
+ require 'kconv'
13
14
 
14
15
  module CodeStock
15
16
  class Cdstk
@@ -240,7 +241,9 @@ module CodeStock
240
241
  # タイムスタンプが新しければデータベースに格納
241
242
  if (document[:timestamp] < values[:timestamp])
242
243
  # 実際に使うタイミングでファイルの内容を読み込み
243
- values[:content] = open(filename).read
244
+ # values[:content] = open(filename).read
245
+ # データベース内の文字コードは'utf-8'で統一
246
+ values[:content] = File.read(filename).kconv(Kconv::UTF8)
244
247
 
245
248
  # データベースに格納
246
249
  values.each do |key, value|
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/06/25
7
+
8
+ require 'rubygems'
9
+ require 'sinatra'
10
+ require 'haml'
11
+ require 'sass'
12
+
13
+ $LOAD_PATH.unshift '../..'
14
+ require 'codestock/cdweb/lib/database'
15
+ require 'codestock/cdweb/lib/command'
16
+ require 'codestock/cdweb/lib/mkurl'
17
+
18
+ set :haml, :format => :html5
19
+
20
+ get '/' do
21
+ @version = '0.1.2'
22
+ @package_num = Database.instance.fileList('').size
23
+ @file_num = Database.instance.fileNum
24
+ haml :index
25
+ end
26
+
27
+ post '/home*' do |path|
28
+ path = path.sub(/^\//, "")
29
+
30
+ case params[:shead]
31
+ when 'all'
32
+ path = ""
33
+ when 'package'
34
+ path = path.split('/')[0]
35
+ end
36
+
37
+ redirect Mkurl.new("home/#{path}", params).inherit_query_shead
38
+ end
39
+
40
+ get '/home*' do |path|
41
+ before = Time.now
42
+ path = path.sub(/^\//, "")
43
+ record = Database.instance.record(path)
44
+
45
+ if (record)
46
+ view(record, params, before)
47
+ else
48
+ unless (params[:query])
49
+ filelist(path, params, before)
50
+ else
51
+ search(path, params, before)
52
+ end
53
+ end
54
+ end
55
+
56
+ get %r{/help} do
57
+ haml :help
58
+ end
59
+
60
+ # -- helper function --
61
+
62
+ helpers do
63
+ # -- escape functions --
64
+ alias h escape_html
65
+ alias escape_url escape
66
+
67
+ def escape_path(src)
68
+ escape_url(src).gsub("%2F", '/')
69
+ end
70
+
71
+ # -- utility --
72
+ def link(query)
73
+ "<a href='#{'/home?query=' + escape_url(query)}'>#{query}</a>"
74
+ end
75
+
76
+ def create_form(path, query, shead)
77
+ shead = shead || 'directory'
78
+
79
+ # こっちにすると'検索'ボタンを押した時に新しくウィンドウが開く
80
+ # <form action='' target='_blank' method='post'>
81
+ <<EOF
82
+ <form action='' method='post'>
83
+ <p>
84
+ <input name='query' size='60' type='text' value='#{query}' />
85
+ <input type='submit' value='検索'><br></input>
86
+ #{create_radio('all', shead)}
87
+ <label>全体を検索</label>
88
+ #{create_radio('package', shead)}
89
+ <label> #{package_name(path)} 以下</label>
90
+ #{create_radio('directory', shead)}
91
+ <label> #{current_name(path)} 以下</label>
92
+ </p>
93
+ </form>
94
+ EOF
95
+ end
96
+
97
+ def create_radio(value, shead)
98
+ str = (value == shead) ? 'checked' : ''
99
+ "<input name='shead' type='radio' value='#{value}' #{str}/>"
100
+ end
101
+
102
+ def create_headmenu(path, query)
103
+ # href = "#{request.path_info}?'#{request.query_string}"
104
+ # href = '/home/rack-1.3.0/lib/rack/handler/cgi.rb?shead=directory'
105
+ href = Mkurl.new('/home/' + path, params).inherit_query_shead
106
+ <<EOF
107
+ <a href="/home" class="headmenu">全てのパッケージ</a>
108
+ <a href="#{href}" class="headmenu" onclick="window.open('#{href}'); return false;">新しい検索</a>
109
+ EOF
110
+ end
111
+
112
+ def topic_path(path, params)
113
+ href = '/home'
114
+ path.split('/').map {|v|
115
+ href += '/' + v
116
+ "<a href='#{Mkurl.new(href, params).inherit_shead}'>#{v}</a>"
117
+ }.join('/')
118
+ end
119
+
120
+ def package_name(path)
121
+ (path == "") ? 'root' : path.split('/')[0]
122
+ end
123
+
124
+ def current_name(path)
125
+ (path == "") ? 'root' : File.basename(path)
126
+ end
127
+
128
+ def path_title(path)
129
+ (path == "") ? "root" : path
130
+ end
131
+
132
+ def filelist_title(path)
133
+ (path == "") ? "Package List" : path
134
+ end
135
+ end
136
+
@@ -0,0 +1,84 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'rack'
4
+ require 'launchy'
5
+ require 'optparse'
6
+
7
+ module Rack
8
+ class Server
9
+ def start
10
+ if options[:warn]
11
+ $-w = true
12
+ end
13
+
14
+ if includes = options[:include]
15
+ $LOAD_PATH.unshift(*includes)
16
+ end
17
+
18
+ if library = options[:require]
19
+ require library
20
+ end
21
+
22
+ if options[:debug]
23
+ $DEBUG = true
24
+ require 'pp'
25
+ p options[:server]
26
+ pp wrapped_app
27
+ pp app
28
+ end
29
+
30
+ # Touch the wrapped app, so that the config.ru is loaded before
31
+ # daemonization (i.e. before chdir, etc).
32
+ wrapped_app
33
+
34
+ daemonize_app if options[:daemonize]
35
+ write_pid if options[:pid]
36
+
37
+ trap(:INT) do
38
+ if server.respond_to?(:shutdown)
39
+ server.shutdown
40
+ else
41
+ exit
42
+ end
43
+ end
44
+
45
+ server.run wrapped_app, options do
46
+ if (options[:LaunchBrowser])
47
+ host = options[:Host] || options[:BindAddress] # options[:BindAddress] for WEBrick
48
+ Launchy.open("http://#{host}:#{options[:Port]}")
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ module CodeStock
56
+ class CLI_Cdweb
57
+ def self.execute(stdout, argv)
58
+ options = {
59
+ :environment => ENV['RACK_ENV'] || "development",
60
+ :pid => nil,
61
+ :Port => 9292,
62
+ :Host => "0.0.0.0",
63
+ :AccessLog => [],
64
+ :config => "config.ru",
65
+ # ----------------------------
66
+ :server => "thin",
67
+ :LaunchBrowser => true
68
+ }
69
+
70
+ opts = OptionParser.new("#{File.basename($0)}")
71
+ opts.on('--db DB_DIR', 'Database dir (default : ~/.codestock)') {|v| options[:DbDir] = v }
72
+ opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") {|host| options[:Host] = host }
73
+ opts.on('-p', '--port PORT', 'use PORT (default: 9292)') {|v| options[:Port] = v }
74
+ opts.on('-n', '--no-browser', 'No launch browser.') {|v| options[:LaunchBrowser] = false }
75
+ opts.parse!(argv)
76
+
77
+ # サーバースクリプトのある場所へ移動
78
+ FileUtils.cd(File.dirname(__FILE__))
79
+
80
+ # Rackサーバー起動
81
+ Rack::Server.start(options)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ require './app'
2
+ run Sinatra::Application
3
+
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/07/03
7
+
8
+ require 'rubygems'
9
+ require 'coderay'
10
+ require 'coderay/helpers/file_type'
11
+
12
+ module CodeStock
13
+ class CodeRayWrapper
14
+ attr_reader :line_number_start
15
+
16
+ def initialize(content, filename, match_lines = [])
17
+ @content = content
18
+ @filename = filename
19
+ @match_lines = match_lines
20
+ @highlight_lines = match_lines.map{|v|v.index+1}
21
+ @line_number_start = 1
22
+ end
23
+
24
+ def set_range(range)
25
+ content_a = @content.to_a
26
+ range = limit_range(range, content_a)
27
+ @content = content_a[range]
28
+ @line_number_start = range.first + 1
29
+ end
30
+
31
+ def limit_range(range, array)
32
+ Range.new(range.first < 0 ? 0 : range.first,
33
+ range.last >= array.size ? array.size - 1 : range.last)
34
+ end
35
+
36
+ def to_html
37
+ html = CodeRay.scan(@content, file_type).
38
+ html(
39
+ :wrap => nil,
40
+ :line_numbers => :table,
41
+ :css => :class,
42
+ :highlight_lines => @highlight_lines,
43
+ :line_number_start => @line_number_start
44
+ )
45
+
46
+ codestock_ornament(html)
47
+ end
48
+
49
+ def codestock_ornament(html)
50
+ a = html.split("\n")
51
+
52
+ line_number = @line_number_start
53
+ is_code_content = false
54
+
55
+ a.each_with_index do |l, index|
56
+ if (l =~ / <td class="code"><pre (.*?)>(.*)<tt>/)
57
+ a[index] = " <td class=\"code\"><pre #{$1}><span #{line_attr(line_number)}>#{$2}</span><tt>"
58
+ is_code_content = true
59
+ line_number += 1
60
+ next
61
+ elsif (l =~ %r|</tt></pre></td>|)
62
+ is_code_content = false
63
+ end
64
+
65
+ if (is_code_content)
66
+ if (l =~ %r|</tt>(.*)<tt>|)
67
+ a[index] = "</tt><span #{line_attr(line_number)}>#{$1}</span><tt>"
68
+ line_number += 1
69
+ end
70
+ end
71
+ end
72
+
73
+ a.join("\n") + "\n"
74
+ end
75
+
76
+ def file_type
77
+ case File.extname(@filename)
78
+ when ".el"
79
+ :scheme
80
+ else
81
+ CodeRay::FileType.fetch @filename, :plaintext
82
+ end
83
+ end
84
+
85
+ def line_attr(no)
86
+ r = []
87
+ r << "id=\"#{no}\""
88
+ r << "class=\"highlight-line\"" if @highlight_lines.include?(no)
89
+ r.join(" ")
90
+ end
91
+ end
92
+ end
93
+
94
+
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/07/11
7
+
8
+ require 'codestock/cdweb/lib/database'
9
+ require 'codestock/cdweb/lib/coderay_wrapper'
10
+ require 'codestock/cdweb/lib/search_contents'
11
+ require 'codestock/cdweb/lib/search_files'
12
+ require 'codestock/cdweb/lib/mkurl'
13
+
14
+ module CodeStock
15
+ def view(record, params, before)
16
+ @title = record.shortpath
17
+ @path = record.shortpath
18
+
19
+ q = params[:query] && Query.new(params[:query])
20
+
21
+ if (q and !q.keywords.empty?)
22
+ grep = Grep.new(record.content)
23
+ match_lines = grep.match_lines_and(q.keywords)
24
+ @record_content = CodeRayWrapper.new(record.content, record.shortpath, match_lines).to_html
25
+ else
26
+ @record_content = CodeRayWrapper.new(record.content, record.shortpath).to_html
27
+ end
28
+
29
+ @elapsed = Time.now - before
30
+ haml :view
31
+ end
32
+
33
+ def search(path, params, before)
34
+ @path = path
35
+ query = Query.new(params[:query])
36
+ @title = "'#{query.query_string}' in #{path_title(path)}"
37
+
38
+ if (query.keywords.size > 0)
39
+ searcher = SearchContents.new(path, params, query)
40
+ else
41
+ searcher = SearchFiles.new(path, params, query)
42
+ end
43
+
44
+ @total_records = searcher.total_records
45
+ @range = searcher.data_range
46
+ @record_content = searcher.html_contents + searcher.html_pagination;
47
+ @elapsed = Time.now - before
48
+ haml :search
49
+ end
50
+
51
+ def filelist(path, params, before)
52
+ @title = filelist_title(path)
53
+ @path = path
54
+ fileList = Database.instance.fileList(path)
55
+ @total_records = fileList.size
56
+ @record_content = fileList.map do |v|
57
+ "<dt class='result-file'>#{file_or_dirimg(v[1])}<a href='#{Mkurl.new('/home/' + v[0], params).inherit_query_shead}'>#{File.basename v[0]}</a></dt>"
58
+ end
59
+ @elapsed = Time.now - before
60
+ haml :filelist
61
+ end
62
+
63
+ def file_or_dirimg(is_file)
64
+ src = (is_file) ? '/images/file.png' : '/images/directory.png'
65
+ "<img alt='' style='vertical-align:bottom; border: 0; margin: 1px;' src='#{src}'>"
66
+ end
67
+ end
@@ -15,14 +15,15 @@ include CodeStock
15
15
  module CodeStock
16
16
  class Database
17
17
  include Singleton
18
- include CodeStock
18
+
19
+ @@db_dir = nil
19
20
 
20
21
  def self.setup(db_dir)
21
22
  @@db_dir = db_dir
22
23
  end
23
24
 
24
25
  def initialize
25
- open(@@db_dir)
26
+ open(@@db_dir || db_default_dir)
26
27
  end
27
28
 
28
29
  def open(db_dir)
@@ -38,34 +39,20 @@ module CodeStock
38
39
  end
39
40
 
40
41
  def record(shortpath)
41
- before = Time.now
42
42
  table = @documents.select { |record| record.shortpath == shortpath }
43
- elapsed = Time.now - before
44
- return table.records[0], elapsed
43
+ return table.records[0]
45
44
  end
46
45
 
47
46
  def fileNum
48
47
  @documents.select.size
49
48
  end
50
-
51
- def search(patterns, packages, fpaths, suffixs, page = 0, limit = -1)
52
- before = Time.now
53
-
54
- # 全てのパターンを検索
55
- if (fpaths.include?("*"))
56
- records, total_records = selectAll(page, limit)
57
- else
58
- records, total_records = searchMain(patterns, packages, fpaths, suffixs, page, limit)
59
- end
60
-
61
- # 検索にかかった時間
62
- elapsed = Time.now - before
63
-
64
- # 結果
65
- return records, total_records, elapsed
49
+
50
+ def search(patterns, packages, fpaths, suffixs, offset = 0, limit = -1)
51
+ # @todo fpathを厳密に検索するには、検索結果からさらに先頭からのパスではないものを除外する
52
+ records, total_records = searchMain(patterns, packages, fpaths, suffixs, offset, limit)
66
53
  end
67
54
 
68
- def selectAll(page, limit)
55
+ def selectAll(offset = 0, limit = -1)
69
56
  table = @documents.select
70
57
 
71
58
  # マッチ数
@@ -79,12 +66,45 @@ module CodeStock
79
66
  # :limit => limit)
80
67
 
81
68
  # ソート
82
- records = table.records.sort_by{|record| record.shortpath.downcase }[page * limit, limit]
69
+ if (limit != -1)
70
+ records = table.records.sort_by{|record| record.shortpath.downcase }[offset, limit]
71
+ else
72
+ records = table.records.sort_by{|record| record.shortpath.downcase }[offset..limit]
73
+ end
74
+
83
75
 
84
76
  return records, total_records
85
77
  end
86
78
 
87
- def searchMain(patterns, packages, fpaths, suffixs, page, limit)
79
+ # @sample test/test_database.rb:43 TestDatabase#t_fileList
80
+ def fileList(base)
81
+ base_parts = base.split("/")
82
+ base_depth = base_parts.length
83
+
84
+ # shortpathにマッチするものだけに絞り込む
85
+ if (base == "")
86
+ records = @documents.select.records
87
+ else
88
+ records = @documents.select {|record| record.shortpath =~ base }.to_a
89
+ end
90
+
91
+ # ファイルリストの生成
92
+ paths = records.map {|record|
93
+ record.shortpath.split("/")
94
+ }.find_all {|parts|
95
+ parts.length > base_depth and parts[0, base_depth] == base_parts
96
+ }.map {|parts|
97
+ is_file = parts.length == base_depth + 1
98
+ path = parts[0, base_depth + 1].join("/")
99
+ [path, is_file]
100
+ }.uniq
101
+
102
+ paths
103
+ end
104
+
105
+ private
106
+
107
+ def searchMain(patterns, packages, fpaths, suffixs, offset, limit)
88
108
  table = @documents.select do |record|
89
109
  expression = nil
90
110
 
@@ -132,15 +152,22 @@ module CodeStock
132
152
  expression
133
153
  end
134
154
 
135
- # マッチ数
136
- total_records = table.size
137
-
138
155
  # スコアとタイムスタンプでソート
139
156
  records = table.sort([{:key => "_score", :order => "descending"},
140
157
  {:key => "timestamp", :order => "descending"}],
141
- :offset => page * limit,
158
+ :offset => offset,
142
159
  :limit => limit)
160
+
161
+ # パッケージの条件追加
162
+ if (packages.size > 0)
163
+ records.delete_if do |record|
164
+ !packages.any?{|package| record.shortpath.split('/')[0] =~ /#{package}/ }
165
+ end
166
+ end
143
167
 
168
+ # マッチ数
169
+ total_records = records.size
170
+
144
171
  return records, total_records
145
172
  end
146
173
  private :searchMain