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
@@ -1,243 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief HTMLの描画ルーチン
5
- # @author ongaeshi
6
- # @date 2010/10/17
7
-
8
- require 'rubygems'
9
- require 'pathname'
10
- require 'rack'
11
- require 'cdweb/grep'
12
- require 'cdweb/coderay_wrapper'
13
-
14
- module CodeStock
15
- class HTMLRendeler
16
- include Rack::Utils
17
-
18
- def initialize(script_name)
19
- # @script_name = Pathname(script_name)
20
- @script_name = '/'
21
- end
22
-
23
- def header(title, header1)
24
- <<EOS
25
- <?xml version="1.0" encoding="utf-8"?>
26
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
27
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
28
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
29
- <head>
30
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
31
- <!-- <meta name="robot" content="noindex,nofollow" /> -->
32
- <title>#{title}</title>
33
- <link rel="stylesheet" href="#{fullpath('css/gren.css')}" type="text/css" media="all" />
34
- <link rel="stylesheet" href="#{fullpath('css/coderay.css')}" type="text/css" media="all" />
35
- </head>
36
- <body>
37
- <div class="header">
38
- <h1>
39
- <a href="#{fullpath('')}"><img src="#{fullpath('images/gren-icon-mini.png')}" alt="gren-icon" border="0"/></a>
40
- #{header1}
41
- </h1>
42
- </div>
43
-
44
- <div class="content">
45
- EOS
46
- end
47
-
48
- def footer
49
- <<EOS
50
- </div>
51
-
52
- <div class="footer">
53
- </div>
54
- </body>
55
- </html>
56
- EOS
57
- end
58
-
59
- def header_home(title, header1, version)
60
- <<EOS
61
- <?xml version="1.0" encoding="utf-8"?>
62
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
63
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
64
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
65
- <head>
66
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
67
- <!-- <meta name="robot" content="noindex,nofollow" /> -->
68
- <title>#{title}</title>
69
- <link rel="stylesheet" href="#{fullpath('css/gren.css')}" type="text/css" media="all" />
70
- </head>
71
- <body>
72
- <div align="center">
73
- <div class="header_home">
74
- <h1>
75
- <a href="#{fullpath('')}"><img src="#{fullpath('images/gren-icon.png')}" alt="gren-icon" border="0" height="100px"/></a>
76
- #{header1} <font class="version">#{version}</font>
77
- </h1>
78
- </div>
79
-
80
- <div class="content">
81
- EOS
82
- end
83
-
84
- def footer_home(package, files)
85
- <<EOS
86
- </div>
87
-
88
- <div class="footer_home">
89
- <!-- <a href="#{fullpath('::search/p:*')}">#{package}</a>のパッケージ , -->
90
- <a href="#{fullpath('::search/f:*')}">#{files}</a>のファイル<br>
91
- <a href="#{fullpath('::help')}">ヘルプ</a> ,
92
- <a href="http://ongaeshi.github.com/gren">grenについて</a>
93
- </div>
94
- </div>
95
- </body>
96
- </html>
97
- EOS
98
- end
99
-
100
- def result_record(record, patterns, nth=1)
101
- if (patterns.size > 0)
102
- <<EOS
103
- <dt class='result-record'><a href='#{fullpath("::view/" + Rack::Utils::escape_html(record.shortpath))}'>#{record.shortpath}</a></dt>
104
- <dd>
105
- <pre class='lines'>
106
- #{result_record_match_line(record, patterns, nth)}
107
- </pre>
108
- </dd>
109
- EOS
110
- else
111
- <<EOS
112
- <dt class='result-record'><a href='#{fullpath("::view/" + Rack::Utils::escape_html(record.shortpath))}'>#{record.shortpath}</a></dt>
113
- EOS
114
- end
115
- end
116
-
117
- def result_record_match_line(record, patterns, nth)
118
- str = ""
119
-
120
- grep = Grep.new(record.content)
121
- lines = grep.match_lines_or(patterns)
122
-
123
- unless (lines.empty?)
124
- index = lines[0].index
125
-
126
- (index - nth..index + nth).each do |i|
127
- if (0 <= i && i < grep.content.size)
128
- match_datas = (i == index) ? lines[0].match_datas : []
129
- str << line(i + 1, grep.content[i], match_datas) + "\n"
130
- end
131
- end
132
- end
133
-
134
- str
135
- end
136
-
137
- def record_content(record)
138
- CodeRayWrapper.html_memfile(record.content, record.shortpath)
139
- end
140
-
141
- def record_content_line(record)
142
- str = ""
143
-
144
- grep = Grep.new(record.content)
145
- grep.content.each_with_index do |l, index|
146
- str << line(index + 1, l, []) + "\n"
147
- end
148
-
149
- str
150
- end
151
-
152
- def line(lineno, line, match_datas)
153
- sprintf("%5d: %s", lineno, match_strong(Rack::Utils::escape_html(line), match_datas))
154
- end
155
-
156
- def match_strong(line, match_datas)
157
- match_datas.each do |m|
158
- line = line.split(m[0]).join('<strong>' + m[0] + '</strong>') unless (m.nil?)
159
- end
160
-
161
- line
162
- end
163
-
164
- def pagination_link(page, label)
165
- href = "?page=#{page}"
166
- pagination_span("<a href='#{href}'>#{label}</a>")
167
- end
168
-
169
- def pagination_span(content)
170
- "<span class='pagination-link'>#{content}</span>\n"
171
- end
172
-
173
- def empty_summary()
174
- <<EOS
175
- <div class='search-summary'>
176
- <p>gren web検索</p>
177
- </div>
178
- EOS
179
- end
180
-
181
- def search_summary(keyword, total_records, range, elapsed)
182
- <<EOS
183
- <div class='search-summary'>
184
- <span class="keyword">#{keyword}</span>の検索結果:
185
- <span class="total-entries">#{total_records}</span>件中
186
- <span class="display-range">#{range.first} - #{range.last}</span>件(#{elapsed}秒)
187
- </div>
188
- EOS
189
- end
190
-
191
- def view_summary(path, elapsed)
192
- <<EOS
193
- <div class='search-summary'>
194
- <span class="keyword">#{path}</span>(#{elapsed}秒)
195
- </div>
196
- EOS
197
- end
198
-
199
- def search_box(text = "")
200
- <<EOS
201
- <form method="post" action="#{fullpath('::search')}">
202
- <p>
203
- <input name="query" type="text" size="60" value="#{text}" />
204
- <input type="submit" value="検索" />
205
- </p>
206
- </form>
207
- EOS
208
- end
209
-
210
- def sample_code
211
- <<EOS
212
- <div class='sample-code'>
213
- <ol>
214
- <li>キーワードで検索<br>
215
- #{link('def open')}
216
- <li>1フレーズとして検索<br>
217
- #{link('"def open"')}
218
- <li>パッケージ名で絞り込み<br>
219
- #{link('def open p:gren')}
220
- <li>ファイル名や拡張子で絞り込み<br>
221
- #{link('def open f:test s:rb')}
222
- <li>組み合わせ<br>
223
- #{link('p:gren p:tidtools s:rb f:test assert f:cli')}
224
- </ol>
225
- </div>
226
- EOS
227
- end
228
-
229
- def link(keyword)
230
- "<a href='#{fullpath('::search' + '/' + Rack::Utils::escape_html(keyword))}'>#{keyword}</a>"
231
- end
232
-
233
- def fullpath(path)
234
- unless (path == '')
235
- (@script_name + path).to_s
236
- else
237
- @script_name.to_s
238
- end
239
- end
240
- end
241
- end
242
-
243
-
@@ -1,130 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief 検索処理本体
5
- # @author ongaeshi
6
- # @date 2010/10/13
7
-
8
- require 'rubygems'
9
- require 'rack'
10
- require 'cdweb/database'
11
- require 'cdweb/html_renderer'
12
- require 'cdweb/query'
13
-
14
- module CodeStock
15
- class Searcher
16
- include Rack::Utils
17
-
18
- def initialize
19
- end
20
-
21
- def call(env)
22
- @request = Rack::Request.new(env)
23
- @query = Query.new(@request)
24
-
25
- @response = Rack::Response.new
26
- @response["Content-Type"] = "text/html; charset=UTF-8"
27
-
28
- @nth = 3 # マッチした行の前後何行を表示するか
29
-
30
- @rendeler = HTMLRendeler.new(@request.script_name + '/..')
31
-
32
- if @request.post? or @request['query']
33
- post_request
34
- else
35
- search
36
- end
37
- end
38
-
39
- private
40
-
41
- def post_request
42
- query = @request['query'] || ''
43
- if query.empty?
44
- @request.path_info = "/"
45
- else
46
- @request.path_info = "/#{escape(query)}/"
47
- end
48
- @response.redirect(@request.url.split(/\?/, 2)[0])
49
- @response.to_a
50
- end
51
-
52
- def search
53
- render_header
54
- render_search_box
55
- render_search_result
56
- render_footer
57
- @response.to_a
58
- end
59
-
60
- def render_header
61
- @response.write @rendeler.header("gren : #{@query.escape_html}", "gren")
62
- end
63
-
64
- def render_search_box
65
- @response.write @rendeler.search_box(@query.escape_html)
66
- end
67
-
68
- def render_search_result
69
- if @query.empty?
70
- @response.write @rendeler.empty_summary
71
- else
72
- records, total_records, elapsed = Database.instance.search(@query.keywords, @query.packages, @query.fpaths, @query.suffixs, calcPage, calcLimit)
73
- render_search_summary(records, total_records, elapsed)
74
- records.each { |record| @response.write(@rendeler.result_record(record, @query.keywords, @nth)) }
75
- render_pagination(calcPage, total_records)
76
- end
77
- end
78
-
79
- def render_search_summary(records, total_records, elapsed)
80
- pageStart = calcPage * calcLimit
81
- @response.write @rendeler.search_summary(@query.query_string,
82
- total_records,
83
- (total_records.zero? ? 0 : pageStart + 1)..(pageStart + records.size),
84
- elapsed)
85
- end
86
-
87
- def render_pagination(page, total_records)
88
- return if @query.empty?
89
- return if total_records < calcLimit
90
-
91
- last_page = (total_records / calcLimit.to_f).ceil
92
- @response.write("<div class='pagination'>\n")
93
- if page > 0
94
- @response.write(@rendeler.pagination_link(page - 1, "<<"))
95
- end
96
- last_page.times do |i|
97
- if i == page
98
- @response.write(@rendeler.pagination_span(i))
99
- else
100
- @response.write(@rendeler.pagination_link(i, i))
101
- end
102
- end
103
- if page < (last_page - 1)
104
- @response.write(@rendeler.pagination_link(page + 1, ">>"))
105
- end
106
- @response.write("</div>\n")
107
- end
108
-
109
- def render_footer
110
- @response.write @rendeler.footer
111
- end
112
-
113
- private
114
-
115
- # 1ページに表示する最大レコードを計算
116
- def calcLimit
117
- if @query.keywords.size == 0
118
- 100
119
- else
120
- 20
121
- end
122
- end
123
-
124
- # 現在ページを計算
125
- def calcPage
126
- (@request['page'] || 0).to_i
127
- end
128
- end
129
- end
130
-
data/lib/cdweb/viewer.rb DELETED
@@ -1,52 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief ソースコードを表示する
5
- # @author ongaeshi
6
- # @date 2010/10/13
7
-
8
- require 'rubygems'
9
- require 'rack'
10
- require 'cdweb/database'
11
- require 'cdweb/html_renderer'
12
-
13
- module CodeStock
14
- class Viewer
15
- include Rack::Utils
16
-
17
- def initialize
18
- end
19
-
20
- def call(env)
21
- @request = Rack::Request.new(env)
22
- @response = Rack::Response.new
23
- @response["Content-Type"] = "text/html; charset=UTF-8"
24
-
25
- record, elapsed = Database.instance.record(req2query)
26
-
27
- @rendeler = HTMLRendeler.new(@request.script_name + '/..')
28
-
29
- if (record)
30
- @response.write @rendeler.header("gren : #{record.shortpath}", "gren")
31
- @response.write @rendeler.search_box("")
32
- @response.write @rendeler.view_summary(record.shortpath, elapsed)
33
- @response.write @rendeler.record_content(record)
34
- else
35
- @response.write @rendeler.header("gren : not found.", "gren")
36
- @response.write @rendeler.search_box("")
37
- @response.write @rendeler.empty_summary
38
- end
39
- @response.write @rendeler.footer
40
-
41
- @response.to_a
42
- end
43
-
44
- private
45
-
46
- def req2query
47
- p @request.path_info
48
- unescape(@request.path_info.gsub(/\A\/|\/z/, ''))
49
- end
50
-
51
- end
52
- end
@@ -1,36 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief
5
- # @author ongaeshi
6
- # @date 2011/02/20
7
-
8
- require 'rubygems'
9
- require 'groonga'
10
- require 'test_helper'
11
- require 'file_test_utils'
12
- require 'cdweb/database'
13
- require 'common/dbdir'
14
- require 'cdstk/cdstk'
15
- require 'stringio'
16
-
17
- class TestMkgrendb < Test::Unit::TestCase
18
- include FileTestUtils
19
- include CodeStock
20
-
21
- def test_setup_and_open
22
- io = StringIO.new
23
- obj = Cdstk.new(io)
24
- obj.init
25
-
26
- Database.setup('.')
27
- Database.instance
28
- end
29
-
30
- def teardown
31
- teardown_custom(true)
32
- # teardown_custom(false)
33
- end
34
- end
35
-
36
-
@@ -1,41 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'test_helper'
3
- require 'cdweb/html_renderer.rb'
4
-
5
- class TestGrenwebHTMLRendeler < Test::Unit::TestCase
6
- include CodeStock
7
-
8
- def setup
9
- @rendeler = HTMLRendeler.new('/')
10
- end
11
-
12
- def test_pagination_line
13
- assert_equal("<span class='pagination-link'><a href='?page=1'>test</a></span>\n", @rendeler.pagination_link(1, "test"))
14
- end
15
-
16
- def test_search_summary
17
- assert_equal(@rendeler.search_summary(10, 500, 10..20, 0.00893),
18
- <<-EOS)
19
- <div class='search-summary'>
20
- <span class="keyword">10</span>の検索結果:
21
- <span class="total-entries">500</span>件中
22
- <span class="display-range">10 - 20</span>件(0.00893秒)
23
- </div>
24
- EOS
25
- end
26
-
27
- def test_match_strong
28
- assert_equal(@rendeler.match_strong("This is line.", [nil, nil]), "This is line.")
29
- end
30
-
31
- def test_search_box
32
- assert_equal(@rendeler.search_box('test must'), <<-EOF)
33
- <form method="post" action="/::search">
34
- <p>
35
- <input name="query" type="text" size="60" value="test must" />
36
- <input type="submit" value="検索" />
37
- </p>
38
- </form>
39
- EOF
40
- end
41
- end
@@ -1,54 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief
5
- # @author ongaeshi
6
- # @date 2010/10/21
7
-
8
- require 'test_helper'
9
- require 'cdweb/query'
10
-
11
- class TestGrenWebQuery < Test::Unit::TestCase
12
- include CodeStock
13
- include Rack::Utils
14
-
15
- def test_query
16
- q = create_query("test fire beam")
17
- assert_equal q.keywords, ['test', 'fire', 'beam']
18
- assert_equal q.packages, []
19
- assert_equal q.fpaths, []
20
- assert_equal q.suffixs, []
21
- assert_equal q.escape_html, 'test fire beam'
22
-
23
- q = create_query("test fire beam f:testfile1")
24
- assert_equal q.keywords, ['test', 'fire', 'beam']
25
- assert_equal q.packages, []
26
- assert_equal q.fpaths, ['testfile1']
27
- assert_equal q.suffixs, []
28
-
29
- q = create_query("test fire beam f:testfile1 filepath:dir32")
30
- assert_equal q.keywords, ['test', 'fire', 'beam']
31
- assert_equal q.packages, []
32
- assert_equal q.fpaths, ['dir32', 'testfile1']
33
- assert_equal q.suffixs, []
34
-
35
- q = create_query("package:gren test fire beam f:testfile1 filepath:dir32 s:rb p:test suffix:pl")
36
- assert_equal q.keywords, ['test', 'fire', 'beam']
37
- assert_equal q.packages, ['gren', 'test']
38
- assert_equal q.fpaths, ['dir32', 'testfile1']
39
- assert_equal q.suffixs, ['pl', 'rb']
40
-
41
- q = create_query("&p")
42
- assert_equal q.escape_html, '&amp;p'
43
-
44
- q = create_query("int &p")
45
- assert_equal q.escape_html, 'int &amp;p'
46
-
47
- q = create_query('"def update"')
48
- assert_equal q.keywords, ['def update']
49
- end
50
-
51
- def create_query(query)
52
- Query.new(Rack::Request.new({"PATH_INFO"=>"/#{escape(query)}/"}))
53
- end
54
- end
@@ -1,35 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # @file
4
- # @brief
5
- # @author ongaeshi
6
- # @date 2010/10/21
7
-
8
- require 'test_helper'
9
- require 'cdweb/searcher.rb'
10
- require 'rack/mock'
11
-
12
- class TestGrenwebSearcher < Test::Unit::TestCase
13
- def setup
14
- @app = CodeStock::Searcher.new
15
- @mr = Rack::MockRequest.new(@app)
16
- end
17
-
18
- def test_get
19
- res = nil
20
- assert_nothing_raised('なんか失敗した') { res = @mr.get('/', :lint => true) }
21
- assert_not_nil res, 'レスポンス来てない'
22
- assert_equal 200, res.status, 'ステータスコードが変'
23
- assert_equal 'text/html; charset=UTF-8', res['Content-Type'], 'Content-Typeが変'
24
- #puts res.body
25
- end
26
-
27
- def test_post
28
- res = nil
29
- assert_nothing_raised('なんか失敗した') { res = @mr.post('/', :lint => true) }
30
- assert_not_nil res, 'レスポンス来てない'
31
- assert_equal 302, res.status, 'ステータスコードが変'
32
- assert_equal 'text/html; charset=UTF-8', res['Content-Type'], 'Content-Typeが変'
33
- #puts res.body
34
- end
35
- end