tags_finder 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67333989e8a0d13e565f90929e2fe00a8a14c6c8ed6b245a1386f222357402c1
4
- data.tar.gz: aecd5d4d26587df0869070137e70f805d26ab0e1c02e849f9b5cd2e4b0e982d5
3
+ metadata.gz: b82e2d98ef05deaac97bc0844f3acd47038c7e26d7c2415ae10412b8364cb5ee
4
+ data.tar.gz: cd201c99dc20c355f57a5d1620a8edd59b8555b920519f3c613f60e444f2355e
5
5
  SHA512:
6
- metadata.gz: 3916bf3f8cebac39f59bc387afc41733db5476c3bc82ee2ec31bd5b6ae135a5f8ab6986ddc0b112586a4d9d8b2055bfa5509a1c56dd7dcc916955c7265b64630
7
- data.tar.gz: 11bb05ec098b1df744729553898c49486b40cac40179322185c17946d0868e3cc0757f0fe875d5a144acff8554e368ed7c5df75b355c7e6d9acbc366086e602c
6
+ metadata.gz: 97e8f96d68ff7459b281f844abbdae9e678c807c2c9a9c4f815c21ae5ed3feccc34b00d0623ffd9d1d2f04eab1ee3f8bb621bfe5d34eeb0d7223461769552e95
7
+ data.tar.gz: dd41c7015025d6caf87eb19b86378922c59b2d791da9389eac981e520e788911a4b29c8ac7d20d194d69673881066caf25e81434ceb17ef3c807f16036298d45
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ class TagsFinder
5
+ # :nodoc:
6
+ class Job
7
+ attr_reader :pid
8
+
9
+ def initialize(&block)
10
+ @block = block
11
+ @read, @write = IO.pipe
12
+ end
13
+
14
+ def call
15
+ @pid = fork do
16
+ @read.close
17
+ @write.write(Marshal.dump(@block.call))
18
+ end
19
+
20
+ self
21
+ end
22
+
23
+ def result
24
+ @write.close
25
+ res = @read.read
26
+ res.empty? ? nil : Marshal.load(res)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ class TagsFinder
5
+ # :nodoc:
6
+ class Parser
7
+ attr_reader :results
8
+
9
+ def self.build_html_tags_reg(tags:, gtags:)
10
+ @@html_tags_reg = %r{<#{gtags}\s+([^>]*)>|<#{tags}\s+([^>]*)>|(</#{gtags}>)|(</#{tags}>)}i
11
+ end
12
+
13
+ def initialize(path, search_tags, path_tags)
14
+ @path = path
15
+ @search_tags = search_tags
16
+ @path_tags = path_tags
17
+ @gtags = []
18
+ @tag = nil
19
+ @results = []
20
+ end
21
+
22
+ def parse
23
+ File.readlines(@path).each_with_index do |line, line_number|
24
+ next unless (matches = line.match(@@html_tags_reg))
25
+
26
+ next push_gtags(matches[1]) if matches[1]
27
+ next set_tags(matches[2], line_number) if matches[2]
28
+ next pop_gtags(line_number) if matches[3]
29
+
30
+ push_tag(line_number)
31
+ end
32
+
33
+ self
34
+ end
35
+
36
+ private
37
+
38
+ def push_gtags(gtags)
39
+ @gtags << gtags.gsub(/\s+/, ' ').split
40
+ end
41
+
42
+ def pop_gtags(line_number)
43
+ if @gtags.empty?
44
+ raise TagsFinder::Error,
45
+ "Unexpected gtag closure at #{@path}:#{line_number + 1}"
46
+ end
47
+
48
+ @gtags.pop
49
+ end
50
+
51
+ def set_tags(tags, line_number)
52
+ @tag = tags.gsub(/\s+/, ' ').split
53
+ @start_line = line_number
54
+ end
55
+
56
+ def push_tag(line_number)
57
+ unless @tag
58
+ raise TagsFinder::Error,
59
+ "Unexpected tag closure at #{@path}:#{line_number + 1}"
60
+ end
61
+
62
+ @results << Result.new(
63
+ path: @path,
64
+ start_line: @start_line,
65
+ end_line: line_number,
66
+ tags: (@path_tags + (@gtags.last || []) + @tag).uniq,
67
+ search_tags: @search_tags
68
+ )
69
+
70
+ @tag = nil
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ class TagsFinder
5
+ # :nodoc:
6
+ class Result
7
+ attr_reader :path, :start_line, :end_line, :tags, :search_tags
8
+
9
+ def initialize(
10
+ path:, start_line:, end_line:, tags:, search_tags:
11
+ )
12
+ @path = path
13
+ @start_line = start_line
14
+ @end_line = end_line
15
+ @tags = tags
16
+ @search_tags = search_tags
17
+ end
18
+
19
+ def score
20
+ @score ||= begin
21
+ similarities = @search_tags.map do |search_tag|
22
+ @tags.map do |tag|
23
+ String::Similarity.cosine(search_tag, tag)
24
+ end.max || 0
25
+ end
26
+
27
+ similarities.sum / search_tags.length.to_f
28
+ end
29
+ end
30
+
31
+ def extract
32
+ File.read(@path).split("\n")[@start_line..@end_line] * "\n"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ class TagsFinder
5
+ VERSION = '0.1.2'
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tags_finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joakim Carrilho
@@ -31,6 +31,10 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/tags_finder.rb
34
+ - lib/tags_finder/job.rb
35
+ - lib/tags_finder/parser.rb
36
+ - lib/tags_finder/result.rb
37
+ - lib/tags_finder/version.rb
34
38
  homepage: https://github.com/Kimoja/tags_finder
35
39
  licenses:
36
40
  - MIT