Class: Tidtools::Tidgrep

Inherits:
Object
  • Object
show all
Defined in:
lib/tidtools/tidgrep.rb

Constant Summary collapse

MATCH_LINE_COMP_NUM =

圧縮表示時のパラメータ

5
MATCH_ONLY_TITLE_COMP_NUM =
5
MATCH_TIDDLE_LINE_NUM =
3
MATCH_TIDDLE_COMP_NUM =
5
MATCH_TWEET_LINE_NUM =
3
MATCH_TWEET_COMP_NUM =
5

Instance Method Summary collapse

Constructor Details

#initialize(stdout, file_names, file_no, title, regexp_option, report, match_rule, is_comp, keywords, kcode) ⇒ Tidgrep

Returns a new instance of Tidgrep.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tidtools/tidgrep.rb', line 19

def initialize(stdout, file_names, file_no, title, regexp_option, report, match_rule, is_comp, keywords, kcode)
  @stdout = stdout
  @file_names = file_names
  @file_no = file_no
  @title = title
  @regexp_option = regexp_option
  @report = report
  @match_rule = match_rule
  @is_comp = is_comp
  @kcode = kcode

  @title_regexp = @title && Regexp.new(@title, @regexp_option)

  @content_regexps = []
  keywords.each do |keyword|
    @content_regexps << Regexp.new(kcode2utf(keyword), @regexp_option)
  end
end

Instance Method Details

#executeObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/tidtools/tidgrep.rb', line 265

def execute
  if (@content_regexps.size > 0) 
    case @match_rule
    when "line"
      match_line
    when "tiddle"
      match_tiddle
    when "tweet"
      match_tweet
    end
  else
    match_only_title
  end
end

#kcode2utf(str) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/tidtools/tidgrep.rb', line 50

def kcode2utf(str)
  if (@kcode != Kconv::UTF8)
    str.kconv(Kconv::UTF8, @kcode)
  else
    str
  end
end

#match?(target) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/tidtools/tidgrep.rb', line 43

def match?(target)
  @content_regexps.each do |content_regexp|
    return false if content_regexp !~ target
  end
  return true
end

#match_lineObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tidtools/tidgrep.rb', line 89

def match_line
  tiddles = create_tiddles

  match_lines = 0
  search_lines = 0
  match_tiddles = 0

  is_limit = false

  tiddles.each do |tiddle|
    next if (@title && tiddle.title !~ @title_regexp)
    is_match_tiddle = false
    line_no = 1

    tiddle.content.each_line do |line|
      if (match? line)
        match_lines += 1

        unless is_limit
          puts "#{tiddle.title}:#{line_no}:#{line}"

          if (@is_comp && match_lines >= MATCH_LINE_COMP_NUM)
            is_limit = true
            print ".\n.\n"
          end
        end
          

        unless is_match_tiddle
          match_tiddles += 1
          is_match_tiddle = true
        end
      end
      line_no += 1
      search_lines += 1
    end
  end

  if (@report)
    puts "------------------------------"
    puts "search lines : #{search_lines}"
    puts "match lines : #{match_lines}"
    puts "total tiddles : #{tiddles.size}"
    puts "match tiddles : #{match_tiddles}"
  end
end

#match_only_titleObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tidtools/tidgrep.rb', line 136

def match_only_title
  tiddles = create_tiddles

  match_tiddles = 0

  is_limit = false

  tiddles.each do |tiddle|
    next if (@title && tiddle.title !~ @title_regexp)

    match_tiddles += 1

    unless is_limit
      puts tiddle.title

      if (@is_comp && match_tiddles >= MATCH_ONLY_TITLE_COMP_NUM)
        is_limit = true
        print ".\n.\n"
      end
    end
  end

  if (@report)
    puts "------------------------------"
    puts "total tiddles : #{tiddles.size}"
    puts "match tiddles : #{match_tiddles}"
  end
end

#match_tiddleObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tidtools/tidgrep.rb', line 165

def match_tiddle
  tiddles = create_tiddles

  search_tiddles = 0
  match_tiddles = 0

  is_limit = false

  tiddles.each do |tiddle|
    next if (@title && tiddle.title !~ @title_regexp)
    search_tiddles += 1

    if (match? tiddle.content)
      match_tiddles += 1

      unless is_limit
        puts "--- #{tiddle.title} --------------------"

        unless @is_comp
          puts tiddle.content
        else
          tiddle_a = tiddle.content.split(/\n/)

          if (tiddle_a.size <= MATCH_TIDDLE_LINE_NUM)
            print tiddle.content
          else
            print tiddle_a[0..(MATCH_TIDDLE_LINE_NUM - 1)].join("\n") + "\n.\n"
          end
        end

        if (@is_comp && match_tiddles >= MATCH_TIDDLE_COMP_NUM)
          is_limit = true
          print ".\n.\n"
        end
      end
    end
  end

  if (@report)
    puts "------------------------------"
    puts "total tiddles : #{tiddles.size}"
    puts "search tiddles : #{search_tiddles}"
    puts "match tiddles : #{match_tiddles}"
  end
end

#match_tweetObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/tidtools/tidgrep.rb', line 211

def match_tweet
  tiddles = create_tiddles

  search_tweets = 0
  match_tweets = 0

  is_limit = false

  tiddles.each do |tiddle|
    next if (@title && tiddle.title !~ @title_regexp)
    is_match_tiddle = false

    tweets = tiddle.content.split(/^----+\n/)
    search_tweets += tweets.size

    tweets.each do |tweet|
      if (match? tweet)
        match_tweets += 1
        unless is_limit
          unless is_match_tiddle
            puts "--- #{tiddle.title} --------------------"
            is_match_tiddle = true
          else
            puts "----\n"
          end

          unless @is_comp
            print tweet
          else
            tweet_a = tweet.split(/\n/)
            
            if (tweet_a.size <= MATCH_TWEET_LINE_NUM)
              print tweet
            else
              print tweet_a[0..(MATCH_TWEET_LINE_NUM - 1)].join("\n") + "\n.\n"
            end
          end
          
          if (@is_comp && match_tweets >= MATCH_TWEET_COMP_NUM)
            is_limit = true
            print ".\n.\n"
          end
        end
      end
    end
  end

  if (@report)
    puts "------------------------------"
    puts "search tweets : #{search_tweets}"
    puts "match tweets : #{match_tweets}"
  end
end


66
67
68
# File 'lib/tidtools/tidgrep.rb', line 66

def print(msg)
  @stdout.print utf2kcode(msg)
end

#puts(msg) ⇒ Object



70
71
72
# File 'lib/tidtools/tidgrep.rb', line 70

def puts(msg)
  @stdout.puts utf2kcode(msg)
end

#utf2kcode(str) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/tidtools/tidgrep.rb', line 58

def utf2kcode(str)
  if (@kcode != Kconv::UTF8)
    str.kconv(@kcode, Kconv::UTF8)
  else
    str
  end
end

#validOption?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/tidtools/tidgrep.rb', line 38

def validOption?
  return false if @file_names.empty?
  return @title || @content_regexps.size > 0
end