regex_generator 0.3.2 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 481f895706d869e948bda4786af67e5c12940228c50cd53f33b7d2f84eb4a0f1
4
- data.tar.gz: 847974b444d2a2d217fc042144e625c60c297dec1dbfcf60bc692c0b73e985e2
3
+ metadata.gz: 36f1ddb1d1da4bc572085278d85588d3fc085996faf614d3b1cf3b8d53305f9b
4
+ data.tar.gz: b78bb19b4795acde2be57a9bffa144cdb8c3633c100460d06f970889faeb4ed8
5
5
  SHA512:
6
- metadata.gz: d7f8c8af1fb62b699fe599431e83f568fe51ea752feb1749c933c876ff76e4a7c16e8e16aeb3467add14eb9aac6f63c07984f6ad4c849a64b01beeff36467284
7
- data.tar.gz: 9b1038527f6f696146eccbba4f9b4abc1a17e0c201e7d35d5d03bca8b971f80660c52509375481efe62666efc491f1dde101ead3f2368ffa2bc03f73cc14eb38
6
+ metadata.gz: 6ed312d16bd6576c419289f7a507bf538d3704a4ed33e17b84f46acd2650b02cdd277fb80206ef6ff94f71fe76336dc1285b38a5523e12de2c54e6e45410425a
7
+ data.tar.gz: 663fd4746c73451f047cb72deacb8a7301c99032193a7bcaa80f6ef3a0a48bb99f8b96d524f9977737c3534ac31253b9a7a5809200844b9484199c9e056071cd
data/README.md CHANGED
@@ -31,9 +31,14 @@ You can use additional options to generate regex. For example:
31
31
  RegexGenerator.generate('45', 'some text 45', exact_target: true)
32
32
  ```
33
33
 
34
+ Or use from command line:
35
+
36
+ $ generate_regex "target" "text or path/to/file" [options]
37
+
34
38
  Allowed options:
35
39
  - `:exact_target` - When it `true` regex will generated for exact target value
36
40
  - `:self_recognition` - Symbols that will be represented as itself. Can be string or array
41
+ - `:look` - `:ahead` or `:behind` (`:behind` by default). To generate regex with text after or before the value
37
42
 
38
43
  ## Contributing
39
44
 
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'regex_generator'
4
+ require 'optparse'
5
+
6
+ def text_content(args)
7
+ args.map do |arg|
8
+ File.exist?(arg) ? File.read(arg) : arg
9
+ end
10
+ end
11
+
12
+ params = {}
13
+
14
+ OptionParser.new do |parser|
15
+ parser.banner = 'Usage: generate_regex "target or path/to/file" "text or path/to/file" [options]'
16
+
17
+ parser.on('--help', '-h', 'Displays this help message') do
18
+ puts parser
19
+ exit
20
+ end
21
+
22
+ parser.on('--self_recognition=CHARS', 'Recognizes chargs as itself') do |chars|
23
+ chars
24
+ end
25
+
26
+ parser.on('--exact_target', 'Generates regex with exact target value')
27
+ end.parse!(into: params)
28
+
29
+ unless ARGV.count.eql?(2)
30
+ error = ARGV.count < 2 ? 'target or text is missing' : "#{ARGV[2..-1].join(', ')}"
31
+
32
+ raise OptionParser::InvalidOption.new error
33
+ end
34
+
35
+ target, text = text_content(ARGV)
36
+ puts RegexGenerator::Generator.new(target, text, params).generate
@@ -4,4 +4,14 @@ module RegexGenerator
4
4
  'The target was not found in the provided text'
5
5
  end
6
6
  end
7
+
8
+ class InvalidOption < StandardError
9
+ def initialize(*options)
10
+ @options = options
11
+ end
12
+
13
+ def message
14
+ "Invalid option(s): #{@options}"
15
+ end
16
+ end
7
17
  end
@@ -8,6 +8,8 @@ module RegexGenerator
8
8
  # with exact target value
9
9
  # @option options [String, Array] :self_recognition to recognize chars as
10
10
  # itself
11
+ # @option options [:ahead, :behind] :look to generate regex with text before
12
+ # or after the target
11
13
  def initialize(target, text, options = {})
12
14
  @text = text
13
15
  @target = RegexGenerator::Target.new(target)
@@ -17,6 +19,7 @@ module RegexGenerator
17
19
 
18
20
  # @return [Regexp]
19
21
  # @raise [TargetNotFoundError] if target text was not found in the text
22
+ # @raise [InvalidOption] if :look option is not :ahead or :behind
20
23
  def generate
21
24
  raise RegexGenerator::TargetNotFoundError unless @target.present?(@text)
22
25
 
@@ -31,21 +34,24 @@ module RegexGenerator
31
34
 
32
35
  # Cuts nearest to target, text from the start of the string
33
36
  def cut_nearest_text
34
- start_pattern = @text[/\n/] ? /\n/ : /^/
35
-
36
37
  if @target.kind_of? Hash
37
- target_regex = /(?:#{@target.escape.join('|')})/
38
+ target_regex_str = "(?:#{@target.escape.join('|')})"
38
39
  text_regex_str = (1..@target_str.count).map do |step|
39
40
  all = step.eql?(1) ? '.' : '[\w\W]'
40
- "#{all}+?#{target_regex}"
41
+ add_to("#{all}+?", target_regex_str)
41
42
  end.join
42
- text_regex = Regexp.new "#{start_pattern}#{text_regex_str}"
43
43
 
44
- return @text[text_regex]
44
+ return @text[Regexp.new(add_to('(?:\n|\A|\Z)', text_regex_str))]
45
45
  end
46
46
 
47
- regex = /[\w\W]*(#{start_pattern}[\w\W]+?)#{Regexp.escape(@target_str)}/
48
- @text[regex, 1]
47
+ @text[text_regex_for_string, 1]
48
+ end
49
+
50
+ def text_regex_for_string
51
+ {
52
+ behind: /[\w\W]*((?:\n|\A)[\w\W]+?)#{@target.escape}/,
53
+ ahead: /#{@target.escape}([\w\W]+?(?:\n|\Z))/
54
+ }[options[:look]]
49
55
  end
50
56
 
51
57
  # Slices array to subarrays with identical neighbor elements
@@ -73,12 +79,15 @@ module RegexGenerator
73
79
 
74
80
  # Prepares options
75
81
  def options
76
- return @options unless @options.any?
77
-
78
82
  if @options[:self_recognition].kind_of? String
79
83
  @options[:self_recognition] = @options[:self_recognition].chars
80
84
  end
81
85
 
86
+ @options[:look] = @options[:look] ? @options[:look].to_sym : :behind
87
+ unless %i[ahead behind].include? @options[:look]
88
+ raise RegexGenerator::InvalidOption, :look
89
+ end
90
+
82
91
  @options
83
92
  end
84
93
 
@@ -88,19 +97,30 @@ module RegexGenerator
88
97
 
89
98
  if @target.kind_of? Hash
90
99
  @target_str.each_with_object({}) do |(key, value), patterns|
91
- slices_patterns = slice_to_identicals(recognize(value))
100
+ slices_patterns = slice_to_identicals(recognize(value, options))
92
101
  patterns[key] = join_patterns(slices_patterns)
93
102
  end
94
103
  else
95
- target_patterns_array = slice_to_identicals(recognize(@target))
104
+ target_patterns_array = slice_to_identicals(recognize(@target, options))
96
105
  join_patterns(target_patterns_array)
97
106
  end
98
107
  end
99
108
 
109
+ # Adds target to source depending on source type and :look option, i.e. when
110
+ # :look is :behind it adds target to the start of the source, otherwise adds
111
+ # target to the end of the source
112
+ def add_to(source, target)
113
+ actions = { array: { behind: :push, ahead: :unshift },
114
+ string: { behind: :concat, ahead: :prepend } }
115
+ action = actions[source.class.name.downcase.to_sym][options[:look]]
116
+
117
+ source.public_send(action, target)
118
+ end
119
+
100
120
  # Recognizes text depending on target type
101
121
  def recognize_text(text, options = {})
102
122
  unless @target.kind_of? Hash
103
- return recognize(text, options) << "(#{target_patterns})"
123
+ return add_to(recognize(text, options), "(#{target_patterns})")
104
124
  end
105
125
 
106
126
  target_regex = /#{@target.escape.join('|')}/
@@ -1,3 +1,3 @@
1
1
  module RegexGenerator
2
- VERSION = '0.3.2'
2
+ VERSION = '0.3.3'
3
3
  end
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
19
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
20
  end
21
- spec.bindir = "exe"
22
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.bindir = "bin"
22
+ spec.executables = ["generate_regex"]
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  spec.add_development_dependency "bundler", ">= 1.16"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regex_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - o.vykhor
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-03 00:00:00.000000000 Z
11
+ date: 2019-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,7 +55,8 @@ dependencies:
55
55
  description:
56
56
  email:
57
57
  - o.vykhor@ukr.net
58
- executables: []
58
+ executables:
59
+ - generate_regex
59
60
  extensions: []
60
61
  extra_rdoc_files: []
61
62
  files:
@@ -67,6 +68,7 @@ files:
67
68
  - README.md
68
69
  - Rakefile
69
70
  - bin/console
71
+ - bin/generate_regex
70
72
  - bin/setup
71
73
  - lib/regex_generator.rb
72
74
  - lib/regex_generator/characters_recognizer.rb
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  - !ruby/object:Gem::Version
95
97
  version: '0'
96
98
  requirements: []
97
- rubygems_version: 3.0.1
99
+ rubygems_version: 3.0.3
98
100
  signing_key:
99
101
  specification_version: 4
100
102
  summary: Simple regex generator