tags_finder 0.1.0 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98fede39edbf75006120fa24c2cd74c0ad819869aa120653f5dbeb663c31dc4e
4
- data.tar.gz: 6bc368c8983dad70e6eb3b58c1db626a1d2501aac93045720ea264b75e1cad55
3
+ metadata.gz: b82e2d98ef05deaac97bc0844f3acd47038c7e26d7c2415ae10412b8364cb5ee
4
+ data.tar.gz: cd201c99dc20c355f57a5d1620a8edd59b8555b920519f3c613f60e444f2355e
5
5
  SHA512:
6
- metadata.gz: c5f724968d14e4207a044f7c4dc3c0cfa7a34d6e1a90aafb21de35e4bd8deebf85b3ad0985060b0ddfdaf242e03d7881b5e69391b2550e0799fde56d705e1d61
7
- data.tar.gz: b02da93517f5f2b24e0fa1e82e14ab2a3f511fc5ec96ee1c4f37cf866df2d4c05ed1108bca470ed151e63eda4276b3c9a53499c78beefa11075836ba90e67d1c
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
data/lib/tags_finder.rb CHANGED
@@ -6,8 +6,6 @@ require 'etc'
6
6
 
7
7
  # :nodoc:
8
8
  class TagsFinder
9
- VERSION = '0.1.0'
10
-
11
9
  # :nodoc:
12
10
  class Error < StandardError; end
13
11
 
@@ -90,3 +88,4 @@ end
90
88
  require_relative 'tags_finder/parser'
91
89
  require_relative 'tags_finder/job'
92
90
  require_relative 'tags_finder/result'
91
+ require_relative 'tags_finder/version'
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.0
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