pattern_patch 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ee5cc07a5f245d8290d1ab831cdaa3474e5a720
4
+ data.tar.gz: 2e7dca10edda17d2db442fc8493e96ff00cfea68
5
+ SHA512:
6
+ metadata.gz: dcc4cc20528c5bc172beec3217a3f4de751fe52b212b0567a6703d1674e32b25427ff8cab293ab3f25bfa329bc0253a2e2a6f168444681fd1dfe3f5ef2f695e7
7
+ data.tar.gz: 1b6501fa7dc885a39fbe3961e5ebc5292c7e81460349216209103702a787dd9a28aba49c66e9a77984d997d3c8ec7189666e8725259e61fd494ef635beccff02
@@ -0,0 +1,3 @@
1
+ require "pattern_patch/string"
2
+ require "pattern_patch/utilities"
3
+ require "pattern_patch/version"
@@ -0,0 +1,28 @@
1
+ class String
2
+ # Replace capture group references in self with appropriate
3
+ # data from matches. Modifies the receiver. The receiver
4
+ # need not match the matches.regexp.
5
+ #
6
+ # :matches: A MatchData object returned by Regexp#match
7
+ def apply_matches!(matches)
8
+ search_position = 0
9
+ while (m = /\\(\d+)/.match(self, search_position))
10
+ capture_group = m[1].to_i
11
+ search_position = index m[0]
12
+ gsub! m[0], matches[capture_group]
13
+ search_position += matches[capture_group].length
14
+ end
15
+ nil
16
+ end
17
+
18
+ # Return a copy of the receiver with capture group references
19
+ # in self replaced by appropriate data from matches. The receiver
20
+ # need not match the matches.regexp.
21
+ #
22
+ # :matches: A MatchData object returned by Regexp#match
23
+ def apply_matches(matches)
24
+ string = clone
25
+ string.apply_matches! matches
26
+ string
27
+ end
28
+ end
@@ -0,0 +1,77 @@
1
+ module PatternPatch
2
+ class Utilities
3
+ class << self
4
+ # Add the specified text after the specified pattern.
5
+ # Returns a modified copy of the string.
6
+ #
7
+ # :contents: A string to modify, e.g. the contents of a file
8
+ # :regexp: A regular expression specifying a pattern to be matched
9
+ # :text: Text to be appended to the specified pattern
10
+ # :global: Boolean flag. If true, patch all occurrences of the regex.
11
+ # :mode: :append, :prepend or :replace to specify how to apply the patch
12
+ # :offset: Starting position for matching
13
+ def apply_patch(contents, regexp, text, global, mode, offset)
14
+ search_position = offset
15
+ while (matches = regexp.match(contents, search_position))
16
+ patched_pattern =
17
+ case mode
18
+ when :append
19
+ "#{matches[0]}#{text.apply_matches matches}"
20
+ when :prepend
21
+ "#{text.apply_matches matches}#{matches[0]}"
22
+ when :replace
23
+ matches[0].sub regexp, text
24
+ else
25
+ raise ArgumentError, "Invalid mode argument. Specify :append, :prepend or :replace."
26
+ end
27
+
28
+ contents = "#{matches.pre_match}#{patched_pattern}#{matches.post_match}"
29
+ break unless global
30
+ search_position = matches.pre_match.length + patched_pattern.length
31
+ end
32
+ contents
33
+ end
34
+
35
+ # Reverts a patch. Use the same arguments that were supplied to apply_patch.
36
+ # The mode argument can only be :append or :prepend. Patches using :replace
37
+ # cannot be reverted.
38
+ # Returns a modified copy of the string.
39
+ #
40
+ # :contents: A string to modify, e.g. the contents of a file
41
+ # :regexp: A regular expression specifying a pattern to be matched
42
+ # :text: Text to be appended to the specified pattern
43
+ # :global: Boolean flag. If true, patch all occurrences of the regex.
44
+ # :mode: :append or :prepend. :replace patches cannot be reverted automatically.
45
+ # :offset: Starting position for matching
46
+ def revert_patch(contents, regexp, text, global, mode, offset)
47
+ search_position = offset
48
+ regexp_string = regexp.to_s
49
+
50
+ patched_regexp =
51
+ case mode
52
+ when :append
53
+ /#{regexp_string}#{text}/m
54
+ when :prepend
55
+ # TODO: Capture groups aren't currently revertible in :prepend mode.
56
+ # This patched regexp can turn into something like /\1.*(\d+)/.
57
+ # The capture group reference cannot occur in the regexp before definition
58
+ # of the group. This would have to be transformed to something like
59
+ # /(\d+).*\1/. Patch reversion is probably not a major use case right
60
+ # now, so ignore for the moment.
61
+ /#{text}#{regexp_string}/m
62
+ else
63
+ raise ArgumentError, "Invalid mode argument. Specify :append or :prepend."
64
+ end
65
+
66
+ while (matches = patched_regexp.match(contents, search_position))
67
+ reverted_text = matches[0].sub(text.apply_matches(matches), '')
68
+ contents = "#{matches.pre_match}#{reverted_text}#{matches.post_match}"
69
+ break unless global
70
+ search_position = matches.pre_match.length + reverted_text.length
71
+ end
72
+
73
+ contents
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module PatternPatch
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pattern_patch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Dee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: This is a utility gem that identifies positions in any text using regular
112
+ expressions and then inserts patch text at the specified location or replaces matching
113
+ text. Many patches can be reverted.
114
+ email: jgvdthree@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - lib/pattern_patch.rb
120
+ - lib/pattern_patch/string.rb
121
+ - lib/pattern_patch/utilities.rb
122
+ - lib/pattern_patch/version.rb
123
+ homepage: http://github.com/jdee/pattern_patch
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.6.14
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Apply and revert pattern-based patches to text files of any kind
147
+ test_files: []