tidtools 0.0.2 → 0.0.3
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.
- data/History.txt +9 -0
- data/bin/tidgrep +5 -0
- data/lib/tidgrep/cli.rb +215 -53
- data/lib/tidtools.rb +1 -1
- metadata +3 -3
data/History.txt
CHANGED
data/bin/tidgrep
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
2
3
|
#
|
3
4
|
# Created on 2010-4-23.
|
4
5
|
# Copyright (c) 2010. All rights reserved.
|
@@ -7,4 +8,8 @@ require 'rubygems'
|
|
7
8
|
require File.expand_path(File.dirname(__FILE__) + "/../lib/tidtools")
|
8
9
|
require "tidgrep/cli"
|
9
10
|
|
11
|
+
# バージョン番号
|
12
|
+
Version = "0.0.3"
|
13
|
+
|
14
|
+
# コマンドの実行
|
10
15
|
Tidgrep::CLI.execute(STDOUT, ARGV)
|
data/lib/tidgrep/cli.rb
CHANGED
@@ -1,90 +1,252 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
require File.join(File.dirname(__FILE__), '../tidtools/tiddle')
|
2
3
|
require 'optparse'
|
3
4
|
|
4
5
|
module Tidgrep
|
5
|
-
|
6
|
-
|
7
|
-
return false if !file_name
|
8
|
-
return title || keyword
|
9
|
-
end
|
6
|
+
# 圧縮表示時のパラメータ
|
7
|
+
MATCH_LINE_COMP_NUM = 5
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
MATCH_ONLY_TITLE_COMP_NUM = 5
|
10
|
+
|
11
|
+
MATCH_TIDDLE_LINE_NUM = 3
|
12
|
+
MATCH_TIDDLE_COMP_NUM = 5
|
13
|
+
|
14
|
+
MATCH_TWEET_LINE_NUM = 3
|
15
|
+
MATCH_TWEET_COMP_NUM = 5
|
16
|
+
|
17
|
+
class Tidgrep
|
18
|
+
def initialize
|
19
|
+
@file_name = ENV['TIDGREP_PATH']
|
20
|
+
@title = nil
|
21
|
+
@regexp_option = 0
|
22
|
+
@report = false
|
23
|
+
@match_rule = "line"
|
24
|
+
@is_comp = false
|
25
|
+
end
|
17
26
|
|
27
|
+
def setupParam(stdout, arguments)
|
18
28
|
opt = OptionParser.new('tidgrep [option] keyword')
|
19
|
-
opt.on('-f FILE_NAME', '--filename FILE_NAME', 'TiddlyWiki file name') {|v| file_name = v }
|
20
|
-
opt.on('-t TITLE', '--title TITLE', 'match title') {|v| title = v }
|
21
|
-
opt.on('-i', '--ignore', 'ignore case') {|v| regexp_option |= Regexp::IGNORECASE }
|
22
|
-
opt.on('-r', '--report', 'disp report') {|v| report = true }
|
23
|
-
opt.on('-m MATCH_RULE', '--match MATCH_RULE', 'match rule [
|
29
|
+
opt.on('-f FILE_NAME', '--filename FILE_NAME', 'TiddlyWiki file name') {|v| @file_name = v }
|
30
|
+
opt.on('-t TITLE', '--title TITLE', 'match title') {|v| @title = v }
|
31
|
+
opt.on('-i', '--ignore', 'ignore case') {|v| @regexp_option |= Regexp::IGNORECASE }
|
32
|
+
opt.on('-r', '--report', 'disp report') {|v| @report = true }
|
33
|
+
opt.on('-m MATCH_RULE', '--match MATCH_RULE', 'match rule [line, tiddle, tweet]') {|v| @match_rule = v }
|
34
|
+
opt.on('-c', '--comp', 'compression disp') {|v| @is_comp = true; @report = true }
|
24
35
|
opt.parse!(arguments)
|
36
|
+
|
37
|
+
@title_regexp = @title && Regexp.new(@title, @regexp_option)
|
25
38
|
|
26
|
-
|
39
|
+
@content_regexps = []
|
40
|
+
arguments.each do |keyword|
|
41
|
+
@content_regexps << Regexp.new(keyword, @regexp_option)
|
42
|
+
end
|
27
43
|
|
28
|
-
|
44
|
+
unless validOption?
|
29
45
|
puts opt.help
|
30
46
|
exit
|
31
47
|
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def validOption?
|
51
|
+
return false if !@file_name
|
52
|
+
return @title || @content_regexps.size > 0
|
53
|
+
end
|
54
|
+
|
55
|
+
def match?(target)
|
56
|
+
@content_regexps.each do |content_regexp|
|
57
|
+
return false if content_regexp !~ target
|
58
|
+
end
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
|
62
|
+
def match_line
|
63
|
+
tiddles = Tiddle.parse_sort_modified(@file_name)
|
32
64
|
|
33
|
-
tiddles = Tiddle.parse_sort_modified(file_name)
|
34
65
|
match_lines = 0
|
35
|
-
|
66
|
+
search_lines = 0
|
36
67
|
match_tiddles = 0
|
37
68
|
|
38
|
-
|
39
|
-
content_regexp = keyword && Regexp.new(keyword, regexp_option)
|
69
|
+
is_limit = false
|
40
70
|
|
41
71
|
tiddles.each do |tiddle|
|
42
|
-
next if (title && tiddle.title !~ title_regexp)
|
72
|
+
next if (@title && tiddle.title !~ @title_regexp)
|
43
73
|
is_match_tiddle = false
|
44
74
|
line_no = 1
|
45
75
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
when "tiddle"
|
58
|
-
match_lines += 1
|
59
|
-
unless is_match_tiddle
|
60
|
-
puts "--- #{tiddle.title} --------------------"
|
61
|
-
match_tiddles += 1
|
62
|
-
is_match_tiddle = true
|
63
|
-
end
|
64
|
-
puts "#{line}"
|
65
|
-
when "hr"
|
76
|
+
tiddle.content.each_line do |line|
|
77
|
+
if (match? line)
|
78
|
+
match_lines += 1
|
79
|
+
|
80
|
+
unless is_limit
|
81
|
+
puts "#{tiddle.title}:#{line_no}:#{line}"
|
82
|
+
|
83
|
+
if (@is_comp && match_lines >= MATCH_LINE_COMP_NUM)
|
84
|
+
is_limit = true
|
85
|
+
print ".\n.\n"
|
66
86
|
end
|
67
87
|
end
|
68
|
-
|
69
|
-
|
88
|
+
|
89
|
+
|
90
|
+
unless is_match_tiddle
|
91
|
+
match_tiddles += 1
|
92
|
+
is_match_tiddle = true
|
93
|
+
end
|
70
94
|
end
|
71
|
-
|
95
|
+
line_no += 1
|
96
|
+
search_lines += 1
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
if (@report)
|
101
|
+
puts "------------------------------"
|
102
|
+
puts "search lines : #{search_lines}"
|
103
|
+
puts "match lines : #{match_lines}"
|
104
|
+
puts "total tiddles : #{tiddles.size}"
|
105
|
+
puts "match tiddles : #{match_tiddles}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def match_only_title
|
110
|
+
tiddles = Tiddle.parse_sort_modified(@file_name)
|
111
|
+
|
112
|
+
match_tiddles = 0
|
113
|
+
|
114
|
+
is_limit = false
|
115
|
+
|
116
|
+
tiddles.each do |tiddle|
|
117
|
+
next if (@title && tiddle.title !~ @title_regexp)
|
118
|
+
|
119
|
+
match_tiddles += 1
|
120
|
+
|
121
|
+
unless is_limit
|
72
122
|
puts tiddle.title
|
123
|
+
|
124
|
+
if (@is_comp && match_tiddles >= MATCH_ONLY_TITLE_COMP_NUM)
|
125
|
+
is_limit = true
|
126
|
+
print ".\n.\n"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
if (@report)
|
132
|
+
puts "------------------------------"
|
133
|
+
puts "total tiddles : #{tiddles.size}"
|
134
|
+
puts "match tiddles : #{match_tiddles}"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def match_tiddle
|
139
|
+
tiddles = Tiddle.parse_sort_modified(@file_name)
|
140
|
+
|
141
|
+
search_tiddles = 0
|
142
|
+
match_tiddles = 0
|
143
|
+
|
144
|
+
is_limit = false
|
145
|
+
|
146
|
+
tiddles.each do |tiddle|
|
147
|
+
next if (@title && tiddle.title !~ @title_regexp)
|
148
|
+
search_tiddles += 1
|
149
|
+
|
150
|
+
if (match? tiddle.content)
|
73
151
|
match_tiddles += 1
|
74
|
-
|
152
|
+
|
153
|
+
unless is_limit
|
154
|
+
puts "--- #{tiddle.title} --------------------"
|
155
|
+
|
156
|
+
unless @is_comp
|
157
|
+
puts tiddle.content
|
158
|
+
else
|
159
|
+
print tiddle.content.split(/\n/)[0..(MATCH_TIDDLE_LINE_NUM - 1)].join("\n") + "\n.\n"
|
160
|
+
end
|
161
|
+
|
162
|
+
if (@is_comp && match_tiddles >= MATCH_TIDDLE_COMP_NUM)
|
163
|
+
is_limit = true
|
164
|
+
print ".\n.\n"
|
165
|
+
end
|
166
|
+
end
|
75
167
|
end
|
168
|
+
end
|
76
169
|
|
170
|
+
if (@report)
|
171
|
+
puts "------------------------------"
|
172
|
+
puts "total tiddles : #{tiddles.size}"
|
173
|
+
puts "search tiddles : #{search_tiddles}"
|
174
|
+
puts "match tiddles : #{match_tiddles}"
|
77
175
|
end
|
176
|
+
end
|
78
177
|
|
79
|
-
|
178
|
+
def match_tweet
|
179
|
+
tiddles = Tiddle.parse_sort_modified(@file_name)
|
180
|
+
|
181
|
+
search_tweets = 0
|
182
|
+
match_tweets = 0
|
183
|
+
|
184
|
+
is_limit = false
|
185
|
+
|
186
|
+
tiddles.each do |tiddle|
|
187
|
+
next if (@title && tiddle.title !~ @title_regexp)
|
188
|
+
is_match_tiddle = false
|
189
|
+
|
190
|
+
tweets = tiddle.content.split(/^----+\n/)
|
191
|
+
search_tweets += tweets.size
|
192
|
+
|
193
|
+
tweets.each do |tweet|
|
194
|
+
if (match? tweet)
|
195
|
+
match_tweets += 1
|
196
|
+
unless is_limit
|
197
|
+
unless is_match_tiddle
|
198
|
+
puts "--- #{tiddle.title} --------------------"
|
199
|
+
is_match_tiddle = true
|
200
|
+
else
|
201
|
+
puts "----\n"
|
202
|
+
end
|
203
|
+
|
204
|
+
unless @is_comp
|
205
|
+
print tweet
|
206
|
+
else
|
207
|
+
print tweet.split(/\n/)[0..(MATCH_TWEET_LINE_NUM - 1)].join("\n") + "\n.\n"
|
208
|
+
end
|
209
|
+
|
210
|
+
if (@is_comp && match_tweets >= MATCH_TWEET_COMP_NUM)
|
211
|
+
is_limit = true
|
212
|
+
print ".\n.\n"
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
if (@report)
|
80
220
|
puts "------------------------------"
|
81
|
-
|
82
|
-
|
83
|
-
|
221
|
+
puts "search tweets : #{search_tweets}"
|
222
|
+
puts "match tweets : #{match_tweets}"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def execute(stdout, arguments=[])
|
227
|
+
# パラメータの設定
|
228
|
+
setupParam(stdout, arguments)
|
229
|
+
|
230
|
+
# マッチルールごとに処理を変える
|
231
|
+
if (@content_regexps.size > 0)
|
232
|
+
case @match_rule
|
233
|
+
when "line"
|
234
|
+
match_line
|
235
|
+
when "tiddle"
|
236
|
+
match_tiddle
|
237
|
+
when "tweet"
|
238
|
+
match_tweet
|
84
239
|
end
|
85
|
-
|
86
|
-
|
240
|
+
else
|
241
|
+
match_only_title
|
87
242
|
end
|
88
243
|
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
class CLI
|
248
|
+
def self.execute(stdout, arguments=[])
|
249
|
+
Tidgrep.new.execute(stdout, arguments)
|
250
|
+
end
|
89
251
|
end
|
90
252
|
end
|
data/lib/tidtools.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- ongaeshi
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-25 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|