Class: WordScoop

Inherits:
Object
  • Object
show all
Defined in:
lib/word_scoop.rb,
lib/word_scoop/version.rb,
ext/word_scoop/word_scoop.c

Constant Summary collapse

VERSION =
'2.1.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute link_url.



7
8
9
# File 'lib/word_scoop.rb', line 7

def link_url
  @link_url
end

Class Method Details

.new(*args) ⇒ Object

new



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ext/word_scoop/word_scoop.c', line 104

static VALUE t_new(int argc, VALUE *argv, VALUE klass)
{
    node root;
    VALUE obj, array, string;

    root = initialize_node(NULL_CHAR);

    obj = Data_Make_Struct(klass, struct _node, NULL, destroy_node, root);

    if (argc == 1) {
        array = argv[0];
        while((string = rb_ary_shift(argv[0])) != Qnil) {
            t_add(obj, string);
        }
    }

    return obj;
}

Instance Method Details

#add(str) ⇒ Object Also known as: <<

add



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/word_scoop/word_scoop.c', line 126

static VALUE t_add(VALUE self, VALUE str)
{
    node root, now;
    char *keyword;
    int i, len;

    keyword = StringValuePtr(str);

    len = strlen(keyword);
    while(keyword[len - 1] == CR || keyword[len - 1] == LF ||
            keyword[len - 1] == TAB ||  keyword[len - 1] == SPACE) {
        len--;
    }

    if (len < 1) {
        return Qfalse;
    }

    Data_Get_Struct(self, struct _node, root);
    now = root;

    for(i = 0; i < len; i++) {
        now = search_child_or_create(now, keyword[i]);
    }

    now->end_flag = true;

    return str;
}

#filter_html(str) ⇒ Object

filter_html



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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/word_scoop/word_scoop.c', line 216

static VALUE t_filter_html(VALUE self, VALUE str)
{
    node root, now, ret;
    bool in_tag;
    char *text, *inner_tag;
    int i, head_i, tail_i, copy_head_i, total_len;
    VALUE change_str, url_base, word;
    rb_encoding *enc;

    change_str = rb_str_new2(EMPTY_STRING);
    enc = rb_enc_get(str);
    text = StringValuePtr(str);

    Data_Get_Struct(self, struct _node, root);
    url_base = rb_iv_get(self, LINK_URL_VARIABLE);
    if (url_base == Qnil) {
        url_base = rb_str_new2(DEAULT_LINK_URL);
    }

    now = root;
    total_len = strlen(text);
    head_i = -1;
    tail_i = -1;
    copy_head_i = 0;
    in_tag = false;
    inner_tag = NULL;

    for(i = 0; i <= total_len; i++) {
        if (!in_tag && text[i] == BEGIN_TAG) {
            in_tag = true;
            if (strncasecmp(&text[i + 1], A_TAG, strlen(A_TAG)) == 0) {
                inner_tag = A_TAG;
            } else if (strncasecmp(&text[i + 1], SCRIPT_TAG, strlen(SCRIPT_TAG)) == 0) {
                inner_tag = SCRIPT_TAG;
            } else if (strncasecmp(&text[i + 1], PRE_TAG, strlen(PRE_TAG)) == 0) {
                inner_tag = PRE_TAG;
            } else if (strncasecmp(&text[i + 1], IFRAME_TAG, strlen(IFRAME_TAG)) == 0) {
                inner_tag = IFRAME_TAG;
            } else if (strncasecmp(&text[i + 1], OBJECT_TAG, strlen(OBJECT_TAG)) == 0) {
                inner_tag = OBJECT_TAG;
            }
            continue;
        }

        if (in_tag && !inner_tag && text[i] == END_TAG) {
            in_tag = false;
            continue;
        }

        if (inner_tag && text[i] == BEGIN_TAG) {
            if (strncasecmp(&text[i + 2], inner_tag, strlen(inner_tag)) == 0) {
                inner_tag = NULL;
                continue;
            }
        }

        if (in_tag) {
            continue;
        }

        ret = search_child(now, text[i]);

        if (ret && i != total_len) {
            if (head_i == -1) {
                head_i = i;
            }

            if (ret->end_flag) {
                tail_i = i;
            }
            now = ret;
        } else {
            if (head_i != -1) {
                if (tail_i != -1) {
                    if (copy_head_i < head_i) {
                        rb_funcall(
                            change_str, 
                            rb_intern("concat"),
                            1,
                            add_encode(rb_str_new(&text[copy_head_i], (head_i - copy_head_i)), enc)
                        );
                    }

                    word = rb_str_new(&text[head_i], (tail_i - head_i + 1));
                    rb_funcall(
                        change_str,
                        rb_intern("concat"),
                        1,
                        add_encode(rb_funcall(url_base, rb_intern("%"), 1, rb_assoc_new(word, word)), enc)
                    );
                    i = tail_i;
                    copy_head_i = tail_i + 1;
                    tail_i = -1;
                } else {
                    i = head_i;
                }
                head_i = -1;
            }
            now = root;
        }
    }

    if (copy_head_i == 0) {
        return str;
    } else {
        rb_funcall(
            change_str,
            rb_intern("concat"),
            1,
            add_encode(rb_str_new(&text[copy_head_i], (total_len - copy_head_i)), enc)
        );
        return change_str;
    }
}

#search(str) ⇒ Object

search



159
160
161
162
163
164
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
210
211
# File 'ext/word_scoop/word_scoop.c', line 159

static VALUE t_search(VALUE self, VALUE str)
{
    node root, now, ret;
    char *text;
    int i, head_i, tail_i, total_len;
    VALUE array;
    rb_encoding *enc;

    array = rb_ary_new();
    enc = rb_enc_get(str);
    text = StringValuePtr(str);

    Data_Get_Struct(self, struct _node, root);

    now = root;
    total_len = strlen(text);
    head_i = -1;
    tail_i = -1;

    for(i = 0; i <= total_len; i++) {
        ret = search_child(now, text[i]);

        if (ret && i != total_len) {
            if (head_i == -1) {
                head_i = i;
            }

            if (ret->end_flag) {
                tail_i = i;
            }
            now = ret;
        } else {
            if (head_i != -1) {
                if (tail_i != -1) {
                    rb_funcall(
                        array,
                        rb_intern("push"),
                        1,
                        add_encode(rb_str_new(&text[head_i], (tail_i - head_i + 1)), enc)
                    );
                    i = tail_i;
                    tail_i = -1;
                } else {
                    i = head_i;
                }
                head_i = -1;
            }
            now = root;
        }
    }

    return array;
}