JosephPecoraro-rr 1.0.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.
Files changed (8) hide show
  1. data/History.txt +21 -0
  2. data/Manifest.txt +7 -0
  3. data/README.txt +77 -0
  4. data/Rakefile +10 -0
  5. data/bin/rr +180 -0
  6. data/lib/rr.rb +3 -0
  7. data/test/test_rr.rb +0 -0
  8. metadata +72 -0
data/History.txt ADDED
@@ -0,0 +1,21 @@
1
+ === 1.0.2 / 2008-12-06
2
+
3
+ * 1 minor enhancements to the documentation
4
+
5
+ * Cleaned README
6
+
7
+ === 1.0.1 / 2008-06-29
8
+
9
+ * 1 minor enhancement
10
+
11
+ * I found that usage quite often has the empty string in the replace portion
12
+ so I added a special usage (for pipes) that defaults the replacement
13
+ to the empty string. Therefore if you're using TextMate or something, you
14
+ can filter the docment on a regex, stripping out whatever matches.
15
+
16
+
17
+ === 1.0.0 / 2008-05-31
18
+
19
+ * 1 major enhancement
20
+
21
+ * Birthday! Thanks to Hoe (http://seattlerb.rubyforge.org/hoe/)
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/rr
6
+ lib/rr.rb
7
+ test/test_rr.rb
data/README.txt ADDED
@@ -0,0 +1,77 @@
1
+ = rr
2
+
3
+ * http://blog.bogojoker.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ A multi-line search and replace utility that uses Ruby regular expressions
8
+ for searching and allows back references to captured groups from the pattern
9
+ to appear in the replacement text.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ usage: rr [options] find replace [filenames]
14
+ rr [options] s/find/replace/ [filenames]
15
+ rr [options] find
16
+ find - a regular expression to be run on the entire file as one string
17
+ the final usage defaults the replacement to the empty string
18
+ replace - replacement text, \1-\9 and metachars (\n, etc.) are allowed
19
+ filenames - names of the input files to be parsed, if blank uses STDIN
20
+
21
+ options:
22
+ --line or -l process line by line instead of all at once (not default)
23
+ --case or -c makes the regular expression case sensitive (not default)
24
+ --global or -g process all occurrences in the text (default)
25
+ --modify or -m changes will directly modify the original file (not default)
26
+
27
+ negated options are done by adding 'not' or 'n' in switches like so:
28
+ --notline or -nl
29
+
30
+ special note:
31
+ When using bash, if you want backslashes in the replace portion make sure
32
+ to use the multiple argument usage with single quotes for the replacement.
33
+
34
+
35
+ == SYNOPSIS:
36
+
37
+ Replace all a's with e's
38
+
39
+ rr s/a/e/ file
40
+ rr a e file
41
+
42
+ Doubles the last character on each line and doubles the newline.
43
+
44
+ rr "(.)\n" "\1\1\n\n" file
45
+
46
+ == REQUIREMENTS:
47
+
48
+ * Ruby 1.8.6 or greater
49
+
50
+ == INSTALL:
51
+
52
+ * sudo gem install regex_replace
53
+
54
+ == LICENSE:
55
+
56
+ (The MIT License)
57
+
58
+ Copyright (c) 2008 Joseph Pecoraro
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining
61
+ a copy of this software and associated documentation files (the
62
+ 'Software'), to deal in the Software without restriction, including
63
+ without limitation the rights to use, copy, modify, merge, publish,
64
+ distribute, sublicense, and/or sell copies of the Software, and to
65
+ permit persons to whom the Software is furnished to do so, subject to
66
+ the following conditions:
67
+
68
+ The above copyright notice and this permission notice shall be
69
+ included in all copies or substantial portions of the Software.
70
+
71
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
72
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
73
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
74
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
75
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
76
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
77
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/rr.rb'
6
+
7
+ Hoe.new('regex_replace', Rr::VERSION) do |p|
8
+ p.rubyforge_name = 'bogojoker'
9
+ p.developer('Joseph Pecoraro', 'joepeck02@gmail.com')
10
+ end
data/bin/rr ADDED
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env ruby
2
+ # Author: Joseph Pecoraro
3
+ # Contact: joepeck02@gmail.com
4
+ # Decription: Default behavior is a multi-line search and replace utility that
5
+ # uses a regular expression for searching and allows back references to
6
+ # captured groups from the pattern to appear in the replacement text
7
+
8
+ # Global States
9
+ line_processing = false
10
+ case_sensitive = false
11
+ global_replace = true
12
+ modify_original = false
13
+
14
+ # Usage Message to print
15
+ $program_name = $0.split(/\//).last
16
+ usage = <<USAGE
17
+ usage: #{$program_name} [options] find replace [filenames]
18
+ #{$program_name} [options] s/find/replace/ [filenames]
19
+ #{$program_name} [options] find
20
+
21
+ find - a regular expression to be run on the entire file as one string
22
+ the final usage defaults the replacement to the empty string
23
+ replace - replacement text, \\1-\\9 and metachars (\\n, etc.) are allowed
24
+ filenames - names of the input files to be parsed, if blank uses STDIN
25
+
26
+ options:
27
+ --line or -l process line by line instead of all at once (not default)
28
+ --case or -c makes the regular expression case sensitive (not default)
29
+ --global or -g process all occurrences in the text (default)
30
+ --modify or -m changes will directly modify the original file (not default)
31
+
32
+ negated options are done by adding 'not' or 'n' in switches like so:
33
+ --notline or -nl
34
+
35
+ special note:
36
+ When using bash, if you want backslashes in the replace portion make sure
37
+ to use the multiple argument usage with single quotes for the replacement.
38
+
39
+ example usage:
40
+ Replace all a's with e's
41
+ #{$program_name} s/a/e/ file
42
+ #{$program_name} a e file
43
+
44
+ Doubles the last character on each line and doubles the newline.
45
+ #{$program_name} "(.)\\n" "\\1\\1\\n\\n" file
46
+
47
+ USAGE
48
+
49
+ # Print an error message and exit
50
+ def err(msg)
51
+ puts "#{$program_name}: #{msg}"
52
+ exit 1
53
+ end
54
+
55
+ # Possible Switches
56
+ ARGV.each_index do |i|
57
+ arg = ARGV[i]
58
+ if arg[0] == ?- then
59
+ case arg
60
+ when "--line" , "-l" then line_processing = true
61
+ when "--notline" , "-nl" then line_processing = false
62
+ when "--case" , "-c" then case_sensitive = true
63
+ when "--notcase" , "-nc" then case_sensitive = false
64
+ when "--global" , "-g" then global_replace = true
65
+ when "--notglobal", "-ng" then global_replace = false
66
+ when "--modify" , "-m" then modify_original = true
67
+ when "--notmodify", "-nm" then modify_original = false
68
+ else err("illegal option #{arg}")
69
+ end
70
+ end
71
+ end
72
+
73
+ # Remove the Switches from ARGV
74
+ ARGV.delete_if { |elem| elem[0] == ?- }
75
+
76
+ # Check if it is quick mode (meaning cmdline argument is s/find/replace/)
77
+ find_str = find = replace = filename = nil
78
+ if ARGV.first =~ /^s\/(.*)?\/(.*)?\/(\w*)?$/
79
+ find_str = $1
80
+ replace = $2
81
+ first_filename = 1
82
+
83
+ # Check for 3rd usage: rr [options] find
84
+ elsif ARGV.size == 1
85
+ find_str = ARGV[0]
86
+ replace = ''
87
+ first_filename = 1
88
+
89
+ # Not Quick Mode, Arguments are seperate on the cmdline
90
+ elsif ARGV.size >= 2
91
+ find_str = ARGV[0]
92
+ replace = ARGV[1].dup
93
+ first_filename = 2
94
+
95
+ # User is allowed to wrap the find regex in /'s (this removes them)
96
+ find_str = find_str[1..(find_str.length-2)] if find_str =~ /^\/.*?\/$/
97
+
98
+ # Bad Arguments, show usage
99
+ else
100
+ puts usage
101
+ exit 1
102
+ end
103
+
104
+ # If there are no "files" put a nil on the end of ARGV to mean STDIN
105
+ ARGV << nil if ARGV.size == first_filename
106
+
107
+ # Make find_str into a Regexp object
108
+ if case_sensitive
109
+ find = Regexp.new( find_str )
110
+ else
111
+ find = Regexp.new( find_str, Regexp::IGNORECASE )
112
+ end
113
+
114
+ # Map metacharacters in the replace portion
115
+ replace.gsub!(/\\[\\ntrvfbae]/) do |match|
116
+ case match
117
+ when "\\\\" then match = "\\" # A backslash
118
+ when "\\n" then match = "\n" # Newline
119
+ when "\\t" then match = "\t" # Tab
120
+ when "\\r" then match = "\r" # Carriage Return
121
+ when "\\v" then match = "\v" # Vertical Tab
122
+ when "\\f" then match = "\f" # Formfeed
123
+ when "\\b" then match = "\b" # Backspace
124
+ when "\\a" then match = "\a" # Bell
125
+ when "\\e" then match = "\e" # Escape
126
+ end
127
+ end
128
+
129
+ # Loop through all the filenames doing the find/replace
130
+ first_filename.upto(ARGV.size-1) do |i|
131
+
132
+ # Check for Possible File Errors or if the filename is nil make it STDIN
133
+ filename = ARGV[i]
134
+ unless filename.nil?
135
+ if !File.exist? filename
136
+ err("#{filename}: No such file")
137
+ elsif File.directory? filename
138
+ err("#{filename}: This is a directory, not a file.")
139
+ elsif !File.readable? filename
140
+ err("#{filename}: File is not readable by this user.")
141
+ elsif !File.writable? filename
142
+ err("#{filename}: File is not writable by this user.")
143
+ end
144
+ else
145
+ filename = STDIN.fileno
146
+ end
147
+
148
+ # Setup the stream to print to
149
+ if modify_original && filename != STDIN.fileno then
150
+ temp_filename = filename + '.tmp'
151
+ stream = File.new(temp_filename, File::CREAT|File::TRUNC|File::RDWR, 0644)
152
+ else
153
+ stream = STDOUT
154
+ end
155
+
156
+ # Default Behavior (and basicically what they all do)
157
+ # 1. Open the file
158
+ # 2. Read text as 1 big sting (memory intensive) or Line by Line
159
+ # 3. Run the Find/Replace globally or non-globally
160
+ # 4. Print the result to a stream
161
+ if line_processing and global_replace then
162
+ File.new(filename).readlines.each { |line| stream.puts line.gsub(find,replace) }
163
+ elsif line_processing and !global_replace then
164
+ File.new(filename).readlines.each { |line| stream.puts line.sub(find,replace) }
165
+ elsif !line_processing and global_replace
166
+ stream.puts File.new(filename).read.gsub(find,replace)
167
+ else
168
+ stream.puts File.new(filename).read.sub(find,replace)
169
+ end
170
+
171
+ # If the stream was a temp file then clean up
172
+ if modify_original && filename != STDIN.fileno then
173
+ stream.close
174
+ File.rename(temp_filename, filename)
175
+ end
176
+
177
+ end
178
+
179
+ # Successful
180
+ exit 0
data/lib/rr.rb ADDED
@@ -0,0 +1,3 @@
1
+ class Rr
2
+ VERSION = '1.0.2'
3
+ end
data/test/test_rr.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JosephPecoraro-rr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Pecoraro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-01 00:00:00 -07:00
13
+ default_executable: rr
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.12.2
24
+ version:
25
+ description: A multi-line search and replace utility that uses Ruby regular expressions for searching and allows back references to captured groups from the pattern to appear in the replacement text.
26
+ email:
27
+ - joepeck02@gmail.com
28
+ executables:
29
+ - rr
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - bin/rr
42
+ - lib/rr.rb
43
+ - test/test_rr.rb
44
+ has_rdoc: true
45
+ homepage: http://blog.bogojoker.com
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: bogojoker
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A multi-line search and replace utility that uses Ruby regular expressions for searching and allows back references to captured groups from the pattern to appear in the replacement text.
71
+ test_files:
72
+ - test/test_rr.rb