milkode 0.4.0 → 0.5.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.
Files changed (37) hide show
  1. data/VERSION +1 -1
  2. data/bin/gmilk +1 -1
  3. data/bin/milk +1 -1
  4. data/lib/milkode/cdstk/cdstk.rb +337 -117
  5. data/lib/milkode/cdstk/cli_cdstk.rb +27 -9
  6. data/lib/milkode/cdstk/cli_cdstksub.rb +44 -6
  7. data/lib/milkode/cdstk/milkode_yaml.rb +105 -0
  8. data/lib/milkode/cdstk/package.rb +83 -0
  9. data/lib/milkode/cdstk/yaml_file_wrapper.rb +94 -0
  10. data/lib/milkode/cdweb/app.rb +6 -28
  11. data/lib/milkode/cdweb/lib/database.rb +6 -0
  12. data/lib/milkode/cdweb/lib/grep.rb +28 -0
  13. data/lib/milkode/cdweb/lib/mkurl.rb +2 -0
  14. data/lib/milkode/cdweb/lib/search_contents.rb +49 -10
  15. data/lib/milkode/cdweb/views/filelist.haml +2 -6
  16. data/lib/milkode/cdweb/views/header_menu.haml +5 -0
  17. data/lib/milkode/cdweb/views/search.haml +4 -8
  18. data/lib/milkode/cdweb/views/search_form.haml +22 -0
  19. data/lib/milkode/cdweb/views/view.haml +2 -6
  20. data/lib/milkode/common/ignore_checker.rb +34 -0
  21. data/lib/milkode/common/ignore_setting.rb +62 -0
  22. data/lib/milkode/grep/cli_grep.rb +10 -9
  23. data/milkode.gemspec +18 -5
  24. data/test/data/.gitignore +42 -0
  25. data/test/data/no_auto_ignore/.gitignore +1 -0
  26. data/test/data/no_auto_ignore/a.txt +3 -0
  27. data/test/milkode_test_work.rb +2 -2
  28. data/test/test_cdstk.rb +17 -12
  29. data/test/test_database.rb +4 -4
  30. data/test/test_ignore_checker.rb +26 -0
  31. data/test/test_ignore_setting.rb +113 -0
  32. data/test/test_milkode_yaml.rb +149 -0
  33. data/test/test_package.rb +73 -0
  34. data/test/test_yaml_file_wrapper.rb +97 -0
  35. metadata +19 -6
  36. data/lib/milkode/cdstk/cdstk_yaml.rb +0 -140
  37. data/test/test_cdstk_yaml.rb +0 -167
@@ -18,7 +18,7 @@ require 'milkode/cdweb/lib/mkurl'
18
18
  set :haml, :format => :html5
19
19
 
20
20
  get '/' do
21
- @version = "0.4.0"
21
+ @version = "0.5.0"
22
22
  @package_num = Database.instance.fileList('').size
23
23
  @file_num = Database.instance.fileNum
24
24
  haml :index
@@ -73,38 +73,16 @@ helpers do
73
73
  "<a href='#{'/home?query=' + escape_url(query)}'>#{query}</a>"
74
74
  end
75
75
 
76
- def create_form(path, query, shead)
77
- shead = shead || 'directory'
78
-
79
- # こっちにすると'検索'ボタンを押した時に新しくウィンドウが開く
80
- # <form action='' target='_blank' method='post'>
81
- <<EOF
82
- <script type="text/javascript">
83
- function set_pathname() {
84
- document.searchform.pathname.value = location.pathname;
85
- }
86
- </script>
87
- <form name="searchform" action='/search' method='post'>
88
- <p>
89
- <input name='query' size='60' type='text' value='#{query}' />
90
- <input type='submit' value='検索' onclick='set_pathname()'><br></input>
91
- #{create_radio('all', shead)}
92
- <label>全体を検索</label>
93
- #{create_radio('package', shead)}
94
- <label> #{package_name(path)} 以下</label>
95
- #{create_radio('directory', shead)}
96
- <label> #{current_name(path)} 以下</label>
97
- <input name='pathname' type='hidden' value=''></input>
98
- </p>
99
- </form>
100
- EOF
101
- end
102
-
103
76
  def create_radio(value, shead)
104
77
  str = (value == shead) ? 'checked' : ''
105
78
  "<input name='shead' type='radio' value='#{value}' #{str}/>"
106
79
  end
107
80
 
81
+ def create_checkbox(name, value)
82
+ str = (value) ? 'checked' : ''
83
+ "<input type='checkbox' name='#{name}' value='on' #{str}/>"
84
+ end
85
+
108
86
  def create_headmenu(path, query, flistpath = '')
109
87
  href = Mkurl.new('/home/' + path, params).inherit_query_shead
110
88
  flist = File.join("/home/#{path}", flistpath)
@@ -212,6 +212,12 @@ module Milkode
212
212
  :offset => offset,
213
213
  :limit => limit)
214
214
 
215
+ # ファイル名でソート
216
+ # @todo 本当はこっちが望ましい
217
+ # records = table.sort([{:key => "shortpath", :order => "ascending"}],
218
+ # :offset => offset,
219
+ # :limit => limit)
220
+
215
221
  # パッケージの条件追加
216
222
  if (packages.size > 0)
217
223
  records.delete_if do |record|
@@ -12,6 +12,34 @@ module Milkode
12
12
  end
13
13
 
14
14
  MatchLineResult = Struct.new(:index, :match_datas)
15
+
16
+ def match_lines_stopover(patterns, max_match, start_index)
17
+ result = []
18
+ patternRegexps = strs2regs(patterns, true)
19
+ index = start_index
20
+
21
+ lines = @content.split($/)
22
+
23
+ while (index < lines.size) do
24
+ line = lines[index]
25
+
26
+ match_datas = []
27
+ patternRegexps.each {|v| match_datas << v.match(line)}
28
+
29
+ if (match_datas.all?)
30
+ result << MatchLineResult.new(index, match_datas)
31
+ if result.size >= max_match
32
+ index += 1
33
+ break
34
+ end
35
+ end
36
+
37
+ index += 1
38
+ end
39
+
40
+ index = 0 if (index >= lines.size)
41
+ {:result => result, :next_line => index}
42
+ end
15
43
 
16
44
  def match_lines_and(patterns)
17
45
  result = []
@@ -46,7 +46,9 @@ module Milkode
46
46
  qparam = []
47
47
  qparam << "query=#{escape(@params[:query])}" if (query_inherit and @params[:query])
48
48
  qparam << "shead=#{escape(@params[:shead])}" if (shead_inherit and @params[:shead])
49
+ qparam << "onematch=#{escape(@params[:onematch])}" if (shead_inherit and @params[:onematch])
49
50
  qparam << "offset=#{escape(@params[:offset])}" if (offset_inherit and @params[:offset])
51
+ qparam << "line=#{escape(@params[:line])}" if (offset_inherit and @params[:line])
50
52
  qparam.join('&')
51
53
  end
52
54
  end
@@ -32,6 +32,8 @@ module Milkode
32
32
  @q = query
33
33
  @page = params[:page].to_i || 0
34
34
  @offset = params[:offset].to_i
35
+ @line = params[:line].to_i
36
+ @is_onematch = params[:onematch]
35
37
  fpaths = @q.fpaths
36
38
  fpaths << path + "/" unless path == ""
37
39
  @records, @total_records, @elapsed = Database.instance.search(@q.keywords, @q.packages, fpaths, @q.suffixs, @offset, LIMIT_NUM)
@@ -47,7 +49,7 @@ module Milkode
47
49
  end
48
50
 
49
51
  def data_range
50
- @offset..(next_offset - 1)
52
+ @offset..(@offset + @end_index)
51
53
  end
52
54
 
53
55
  def html_contents
@@ -60,7 +62,7 @@ module Milkode
60
62
 
61
63
  return <<EOF
62
64
  <div class='pagination'>
63
- #{pagination_link(next_offset, "next >>")}
65
+ #{pagination_link(next_offset, @next_line, "next >>")}
64
66
  </div>
65
67
  EOF
66
68
  end
@@ -75,22 +77,58 @@ EOF
75
77
 
76
78
  def grep_contents
77
79
  @match_records = []
78
- @next_index = @records.size
79
-
80
+ @end_index = @next_index = @records.size
81
+ @next_line = nil
82
+
80
83
  @records.each_with_index do |record, index|
81
84
  if (Util::larger_than_oneline(record.content))
82
- grep = Grep.new(record.content)
83
- match_line = grep.one_match_and(@q.keywords)
84
- @match_records << MatchRecord.new(record, match_line) if match_line
85
+
86
+ if @is_onematch
87
+ grep = Grep.new(record.content)
88
+ match_line = grep.one_match_and(@q.keywords)
89
+ @match_records << MatchRecord.new(record, match_line) if match_line
90
+
91
+ if @match_records.size >= DISP_NUM
92
+ @end_index = index
93
+ @next_index = index + 1
94
+ break
95
+ end
96
+ else
97
+ break if grep_match_lines_stopover(record, index)
98
+ end
85
99
  else
86
100
  @match_records << MatchRecord.new(record, Grep::MatchLineResult.new(0, nil))
101
+
102
+ if @match_records.size >= DISP_NUM
103
+ @end_index = index
104
+ @next_index = index + 1
105
+ break
106
+ end
87
107
  end
108
+ end
109
+ end
88
110
 
89
- if @match_records.size >= DISP_NUM
111
+ def grep_match_lines_stopover(record, index)
112
+ grep = Grep.new(record.content)
113
+ r = grep.match_lines_stopover(@q.keywords, DISP_NUM - @match_records.size, (index == 0) ? @line : 0)
114
+
115
+ r[:result].each do |match_line|
116
+ @match_records << MatchRecord.new(record, match_line) if match_line
117
+ end
118
+
119
+ if @match_records.size >= DISP_NUM
120
+ if (r[:next_line] == 0)
121
+ @end_index = index
90
122
  @next_index = index + 1
91
- break
123
+ else
124
+ @end_index = index
125
+ @next_index = index
126
+ @next_line = r[:next_line]
92
127
  end
128
+ return true
93
129
  end
130
+
131
+ return false
94
132
  end
95
133
 
96
134
  def result_match_record(match_record)
@@ -112,9 +150,10 @@ EOF
112
150
  EOS
113
151
  end
114
152
 
115
- def pagination_link(offset, label)
153
+ def pagination_link(offset, line, label)
116
154
  tmpp = @params
117
155
  tmpp[:offset] = offset.to_s
156
+ tmpp[:line] = line.to_s
118
157
  href = Mkurl.new("", tmpp).inherit_query_shead_offset
119
158
  pagination_span("<a href='#{href}' rel='next'>#{label}</a>")
120
159
  end
@@ -1,11 +1,7 @@
1
- .header
2
- %h1
3
- <a href="/"><img src="/images/MilkodeIcon135.png" alt="milkode-icon-mini" border="0" height="75px"/></a>
4
- Milkode
5
- = create_headmenu(@path, params)
1
+ = haml :header_menu
6
2
 
7
3
  .content
8
- = create_form(@path, params[:query], params[:shead])
4
+ = haml :search_form
9
5
 
10
6
  .search-summary
11
7
  <span class="keyword">#{topic_path(@path, params)}</span> <span class="total-entries">#{@total_records}</span>件(#{@elapsed}秒)
@@ -0,0 +1,5 @@
1
+ .header
2
+ %h1
3
+ <a href="/"><img src="/images/MilkodeIcon135.png" alt="milkode-icon-mini" border="0" height="75px"/></a>
4
+ Milkode
5
+ = create_headmenu(@path, params, defined?(flistpath) ? flistpath : '')
@@ -1,16 +1,12 @@
1
- .header
2
- %h1
3
- <a href="/"><img src="/images/MilkodeIcon135.png" alt="milkode-icon-mini" border="0" height="75px"/></a>
4
- Milkode
5
- = create_headmenu(@path, params)
1
+ = haml :header_menu
6
2
 
7
3
  .content
8
- = create_form(@path, params[:query], params[:shead])
4
+ = haml :search_form
9
5
 
10
6
  .search-summary
11
7
  <span class="keyword">#{topic_path(@path, params)}</span>
12
- <span class="hit-num">#{@total_records}</span>件ヒット中</span>
13
- <span class="search-range">#{@range.first}番から#{@range.last - @range.first + 1}件を検索</span>
8
+ <span class="hit-num">#{@total_records}</span>ファイル中</span>
9
+ <span class="search-range">#{@range.first} - #{@range.last}ファイルを検索</span>
14
10
  <span class="match-num">#{@match_num}</span>件マッチ</span>
15
11
  (#{@elapsed}秒)
16
12
 
@@ -0,0 +1,22 @@
1
+ :javascript
2
+ function set_pathname() {
3
+ document.searchform.pathname.value = location.pathname;
4
+ }
5
+ %form(name="searchform" action="/search" method="post")
6
+ %p
7
+ %input(name="query" size="60" type="text"){:value => params[:query]}
8
+ %input(type="submit" value="検索" onclick="set_pathname()")
9
+ %br
10
+ %label
11
+ = create_radio('all', params[:shead])
12
+ 全体を検索
13
+ %label
14
+ = create_radio('package', params[:shead])
15
+ = package_name(@path) + '以下'
16
+ %label
17
+ = create_radio('directory', params[:shead])
18
+ = current_name(@path) + '以下'
19
+ %label
20
+ = create_checkbox('onematch', params[:onematch])
21
+ = '1ファイル1マッチ'
22
+ %input(name="pathname" type="hidden" value="")
@@ -1,11 +1,7 @@
1
- .header
2
- %h1
3
- <a href="/"><img src="/images/MilkodeIcon135.png" alt="milkode-icon-mini" border="0" height="75px"/></a>
4
- Milkode
5
- = create_headmenu(@path, params, '..')
1
+ = haml :header_menu, locals => {:flistpath => '..'}
6
2
 
7
3
  .content
8
- = create_form(@path, params[:query], params[:shead])
4
+ = haml :search_form
9
5
 
10
6
  .search-summary
11
7
  <span class="keyword">#{topic_path(@path, params)}</span>(#{@elapsed}秒)
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2012/03/02
7
+
8
+ require 'milkode/common/ignore_setting.rb'
9
+
10
+ module Milkode
11
+ #
12
+ # Sample:
13
+ # c = IgnoreChecker.new
14
+ # c.add IgnoreSetting.new("/", ["/rdoc", "/test/data", "*.lock"])
15
+ # c.add IgnoreSetting.new("/pkg", ["*.gem"])
16
+ # c.ignore?('/lib/test.rb') #=> false
17
+ # c.ignore?('/pkg/hoge.gem') #=> true
18
+ #
19
+ class IgnoreChecker
20
+ attr_reader :settings
21
+
22
+ def initialize
23
+ @settings = []
24
+ end
25
+
26
+ def add(setting)
27
+ @settings << setting
28
+ end
29
+
30
+ def ignore?(path)
31
+ @settings.any?{|s| s.ignore? path }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2012/03/02
7
+
8
+ module Milkode
9
+ class IgnoreSetting
10
+ attr_reader :path
11
+ attr_reader :ignores
12
+
13
+ def self.create_from_gitignore(path, str)
14
+ IgnoreSetting.new(path, parse_gitignore(str))
15
+ end
16
+
17
+ def self.parse_gitignore(str)
18
+ ignores = str.split($/)
19
+ ignores.delete_if{|v| v =~ /(\A#.*)|(\A\Z)/}
20
+ ignores
21
+ end
22
+
23
+ def initialize(path, ignores)
24
+ @path = path
25
+ @ignores = ignores.map{|v| v.sub(/\/\Z/, "")}
26
+
27
+ @regexp = @ignores.map do |v|
28
+ v = "(\/|\\A)" + Regexp.escape(v).gsub('\\*', "[^/]*") + "(\/|\\Z)"
29
+ Regexp.new(v)
30
+ end
31
+ end
32
+
33
+ def ignore?(path)
34
+ return false unless path.start_with?(@path)
35
+
36
+ if (path.size == @path.size)
37
+ false
38
+ else
39
+ if (@path == '/')
40
+ ignore_in?(path)
41
+ else
42
+ ignore_in?(path[@path.size..-1])
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def ignore_in?(path)
50
+ @regexp.each_with_index do |value, index|
51
+ match = path.match(value)
52
+ is_match_start_pos = @ignores[index].start_with?('/')
53
+
54
+ if match && (!is_match_start_pos || match.begin(0) == 0)
55
+ return true
56
+ end
57
+ end
58
+
59
+ return false
60
+ end
61
+ end
62
+ end
@@ -3,8 +3,9 @@
3
3
  require 'optparse'
4
4
  require 'milkode/findgrep/findgrep'
5
5
  require 'milkode/common/dbdir'
6
- require 'milkode/cdstk/cdstk_yaml'
7
6
  require 'milkode/cdstk/cdstk'
7
+ require 'milkode/cdstk/yaml_file_wrapper'
8
+ require 'milkode/cdstk/package'
8
9
 
9
10
  module Milkode
10
11
  class CLI_Grep
@@ -142,28 +143,28 @@ EOF
142
143
  private
143
144
 
144
145
  def self.setup_package(option, my_option, keyword)
145
- packages = yaml_load.list( CdstkYaml::Query.new([keyword]) ).map{|v| v['directory']}
146
- raise NotFoundPackage.new keyword if (packages.empty?)
147
- option.packages += packages
148
- my_option[:packages] += packages
146
+ dirs = yaml_load.contents.find_all {|p| p.name.include? keyword }.map{|p| p.directory}
147
+ raise NotFoundPackage.new keyword if (dirs.empty?)
148
+ option.packages += dirs
149
+ my_option[:packages] += dirs
149
150
  end
150
151
 
151
152
  def self.package_dir_in?(dir)
152
- yaml_load.package_root_dir(dir)
153
+ yaml_load.package_root(dir)
153
154
  end
154
155
 
155
156
  def self.package_root_dir(dir)
156
- package_root = yaml_load.package_root_dir(dir)
157
+ package_root = yaml_load.package_root(dir)
157
158
 
158
159
  if (package_root)
159
- package_root
160
+ package_root.directory
160
161
  else
161
162
  raise NotFoundPackage.new dir
162
163
  end
163
164
  end
164
165
 
165
166
  def self.yaml_load
166
- CdstkYaml.load(Dbdir.select_dbdir)
167
+ YamlFileWrapper.load(Dbdir.select_dbdir)
167
168
  end
168
169
 
169
170
  class ArgumentParser
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{milkode}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
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{2012-02-02}
12
+ s.date = %q{2012-03-09}
13
13
  s.description = %q{Line based local source code search engine & grep-command & web-app.}
14
14
  s.email = %q{ongaeshi0621@gmail.com}
15
15
  s.executables = ["gmilk", "milk"]
@@ -31,9 +31,11 @@ Gem::Specification.new do |s|
31
31
  "bin/milk",
32
32
  "lib/milkode/cdstk/cdstk.rb",
33
33
  "lib/milkode/cdstk/cdstk_command.rb",
34
- "lib/milkode/cdstk/cdstk_yaml.rb",
35
34
  "lib/milkode/cdstk/cli_cdstk.rb",
36
35
  "lib/milkode/cdstk/cli_cdstksub.rb",
36
+ "lib/milkode/cdstk/milkode_yaml.rb",
37
+ "lib/milkode/cdstk/package.rb",
38
+ "lib/milkode/cdstk/yaml_file_wrapper.rb",
37
39
  "lib/milkode/cdweb/app.rb",
38
40
  "lib/milkode/cdweb/cli_cdweb.rb",
39
41
  "lib/milkode/cdweb/config.ru",
@@ -56,10 +58,12 @@ Gem::Specification.new do |s|
56
58
  "lib/milkode/cdweb/public/images/go-home-5.png",
57
59
  "lib/milkode/cdweb/public/js/milkode.js",
58
60
  "lib/milkode/cdweb/views/filelist.haml",
61
+ "lib/milkode/cdweb/views/header_menu.haml",
59
62
  "lib/milkode/cdweb/views/help.haml",
60
63
  "lib/milkode/cdweb/views/index.haml",
61
64
  "lib/milkode/cdweb/views/layout.haml",
62
65
  "lib/milkode/cdweb/views/search.haml",
66
+ "lib/milkode/cdweb/views/search_form.haml",
63
67
  "lib/milkode/cdweb/views/view.haml",
64
68
  "lib/milkode/common/archive-zip.rb",
65
69
  "lib/milkode/common/dbdir.rb",
@@ -67,6 +71,8 @@ Gem::Specification.new do |s|
67
71
  "lib/milkode/common/display_util.rb",
68
72
  "lib/milkode/common/grenfiletest.rb",
69
73
  "lib/milkode/common/grensnip.rb",
74
+ "lib/milkode/common/ignore_checker.rb",
75
+ "lib/milkode/common/ignore_setting.rb",
70
76
  "lib/milkode/common/platform.rb",
71
77
  "lib/milkode/common/string_snip.rb",
72
78
  "lib/milkode/common/util.rb",
@@ -74,11 +80,14 @@ Gem::Specification.new do |s|
74
80
  "lib/milkode/findgrep/result.rb",
75
81
  "lib/milkode/grep/cli_grep.rb",
76
82
  "milkode.gemspec",
83
+ "test/data/.gitignore",
77
84
  "test/data/a_project/cdstk.rb",
78
85
  "test/data/a_project/cdstk_yaml.rb",
79
86
  "test/data/abc.zip",
80
87
  "test/data/b_project/runner.rb",
81
88
  "test/data/b_project/test_dir.rb",
89
+ "test/data/no_auto_ignore/.gitignore",
90
+ "test/data/no_auto_ignore/a.txt",
82
91
  "test/data/nodir_abc.zip",
83
92
  "test/data/nodir_abc_xpi.xpi",
84
93
  "test/file_assert.rb",
@@ -89,7 +98,6 @@ Gem::Specification.new do |s|
89
98
  "test/test_bin_exec.rb",
90
99
  "test/test_cdstk.rb",
91
100
  "test/test_cdstk_command.rb",
92
- "test/test_cdstk_yaml.rb",
93
101
  "test/test_cli_cdstk.rb",
94
102
  "test/test_cli_grep.rb",
95
103
  "test/test_coderay_wrapper.rb",
@@ -99,10 +107,15 @@ Gem::Specification.new do |s|
99
107
  "test/test_dir.rb",
100
108
  "test/test_gren_util.rb",
101
109
  "test/test_helper.rb",
110
+ "test/test_ignore_checker.rb",
111
+ "test/test_ignore_setting.rb",
112
+ "test/test_milkode_yaml.rb",
102
113
  "test/test_mkurl.rb",
114
+ "test/test_package.rb",
103
115
  "test/test_query.rb",
104
116
  "test/test_string_snip.rb",
105
- "test/test_util.rb"
117
+ "test/test_util.rb",
118
+ "test/test_yaml_file_wrapper.rb"
106
119
  ]
107
120
  s.homepage = %q{http://github.com/ongaeshi/milkode}
108
121
  s.licenses = ["MIT"]