subtxt 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/subtxt/version.rb +1 -1
- data/lib/subtxt.rb +52 -24
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af5ad0c33acaeb9226bdb7d86010e844de05434a
|
4
|
+
data.tar.gz: d18798cf48643489886cdd8527e610e425b4f83f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3809a4778b976da2fb552f2cc4923f0be6d8d002c42cb2c5919691ae3f69194bce35d227fc8d73980da11e7cdba5ecc7fe36dc3b207dd889df734a84a3bb6c3
|
7
|
+
data.tar.gz: 8abb197e20d14012e34545735b53f3ec45c3c8776b78a931c6b047d11942304f413f2e6f3eef09448e58c65748fcaa1b2a13ae58f26e3ce867da22abaed0d986
|
data/lib/subtxt/version.rb
CHANGED
data/lib/subtxt.rb
CHANGED
@@ -2,6 +2,7 @@ require "subtxt/version"
|
|
2
2
|
require 'optparse'
|
3
3
|
require 'logger'
|
4
4
|
require 'fileutils'
|
5
|
+
require 'yaml'
|
5
6
|
|
6
7
|
@ingestdir_def = "."
|
7
8
|
@filext_def = "*"
|
@@ -43,25 +44,32 @@ end
|
|
43
44
|
def subtexts opts
|
44
45
|
patterns = load_patterns(opts[:patterns])
|
45
46
|
@logger.info "Reading patterns from #{@options[:patterns]}"
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
gapspaces = " "
|
52
|
-
fndgap.times do gapspaces += "." end
|
53
|
-
else
|
54
|
-
gapspaces = fndgap
|
55
|
-
end
|
56
|
-
patterns_display += "\n#{rec['fnd']}#{gapspaces}=> #{rec['rep']}"
|
57
|
-
end
|
58
|
-
@logger.info "Using patterns:\n#{patterns_display}\n"
|
47
|
+
routine = {}
|
48
|
+
routine['files_count'] = 0
|
49
|
+
routine['files_changed'] = []
|
50
|
+
routine['files_changed_count'] = 0
|
51
|
+
routine['log'] = []
|
59
52
|
Dir.glob(opts[:ingestpath]) do |f|
|
60
53
|
text = File.read(f)
|
61
54
|
@logger.debug "Processing file: #{File.basename(f)}"
|
55
|
+
sandr = []
|
62
56
|
patterns.each do |rec|
|
63
|
-
|
64
|
-
|
57
|
+
fnd = rec['fnd']
|
58
|
+
rep = rec['rep'].gsub(/\\n/, "\n")
|
59
|
+
if opts[:verbose] or opts[:debug]
|
60
|
+
matches = text.gsub(/#{fnd}/).count
|
61
|
+
syms = text.gsub(/#{fnd}/) {|sym| "-#{sym}-"}
|
62
|
+
if matches > 0
|
63
|
+
sandr << {:pattern => fnd, :matches => matches, :syms => syms}
|
64
|
+
unless routine['files_changed'].include? f
|
65
|
+
routine['files_changed'] << f
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
text.gsub!(/#{fnd}/, rep)
|
70
|
+
end
|
71
|
+
if opts[:verbose] or opts[:debug]
|
72
|
+
routine['log'] << {:file => f, :matchlog => sandr }
|
65
73
|
end
|
66
74
|
unless opts[:expext]
|
67
75
|
outfile = File.basename f
|
@@ -73,10 +81,26 @@ def subtexts opts
|
|
73
81
|
FileUtils::mkdir_p(opts[:expath]) unless File.exists?(opts[:expath])
|
74
82
|
File.open("#{opts[:expath]}/#{outfile}", 'w') { |file| file.write(text) }
|
75
83
|
@logger.debug "File saved (#{outfile})"
|
84
|
+
routine['files_count'] += 1
|
76
85
|
rescue Exception => ex
|
77
86
|
raise "Failure: #{ex}"
|
78
87
|
end
|
79
88
|
end
|
89
|
+
@logger.info display_results(routine)
|
90
|
+
end
|
91
|
+
|
92
|
+
def display_results routine={}
|
93
|
+
raise "NoRecordsFound" unless routine['log'].count
|
94
|
+
output = "Files processed: #{routine['files_count']}"
|
95
|
+
output << "\nFiles changed: #{routine['files_changed'].size}"
|
96
|
+
routine['log'].each do |doc|
|
97
|
+
output << "\nFile: #{doc[:file]}"
|
98
|
+
output << "\nMatches:"
|
99
|
+
doc[:matchlog].each do |mch|
|
100
|
+
output << "\n#{mch[:matches]}: #{mch[:pattern]}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
output
|
80
104
|
end
|
81
105
|
|
82
106
|
parser = OptionParser.new do|opts|
|
@@ -88,7 +112,7 @@ parser = OptionParser.new do|opts|
|
|
88
112
|
Check out http://refiddle.com/ and http://www.rexegg.com/regex-quickstart.html
|
89
113
|
|
90
114
|
Pattern files are formatted in 3-row sets. The first row is the find pattern,
|
91
|
-
the second row is the replace pattern, and
|
115
|
+
the second row is the replace pattern, and the third row delimits the set for
|
92
116
|
the convenience of your eyeballs. Like so:
|
93
117
|
\t---------------------------------------
|
94
118
|
\tfind pattern
|
@@ -102,6 +126,10 @@ parser = OptionParser.new do|opts|
|
|
102
126
|
\t
|
103
127
|
\tEOF
|
104
128
|
\t---------------------------------------\n
|
129
|
+
This procedure generates a copy of each file in a separate directory
|
130
|
+
(#{@expath_def}/ by default) after replacing each matched pattern with
|
131
|
+
its pair.
|
132
|
+
|
105
133
|
Usage: subtxt [path/to/ingest/dir] [options]
|
106
134
|
Options:
|
107
135
|
"""
|
@@ -112,15 +140,15 @@ parser = OptionParser.new do|opts|
|
|
112
140
|
end
|
113
141
|
|
114
142
|
if ARGV[0].split("").first == "-"
|
115
|
-
opts.on('-
|
116
|
-
@options[:
|
143
|
+
opts.on('-p PATH', '--patterns PATH', "Full (relative or absolute) path to a text file\n\t\t\t\t\tcontaining find & replace patterns in the designated format.\n\t\t\t\t\tREQUIRED. Ex: -p path/to/patterns.rgxp") do |n|
|
144
|
+
@options[:patterns] = n;
|
117
145
|
end
|
118
146
|
else # the first arg has no leading - or --, it must be our path
|
119
|
-
@options[:
|
147
|
+
@options[:patterns] = ARGV[0]
|
120
148
|
end
|
121
149
|
|
122
|
-
opts.on('-
|
123
|
-
@options[:
|
150
|
+
opts.on('-s PATH', '--source PATH', "Ingest files from this directory. Defaults to current directory.\n\t\t\t\t\tSuperceded if a path is passed as the first argument\n\t\t\t\t\t(subtxt path/to/files -p patterns.rgx). Ex: -i path/to/ingest/dir") do |n|
|
151
|
+
@options[:ingestdir] = n;
|
124
152
|
end
|
125
153
|
|
126
154
|
## TODO recursion
|
@@ -128,7 +156,7 @@ parser = OptionParser.new do|opts|
|
|
128
156
|
# @options[:recursive] = true
|
129
157
|
# end
|
130
158
|
|
131
|
-
opts.on('-f STRING', '--filext STRING',
|
159
|
+
opts.on('-f STRING', '--filext STRING', "Restrict ingested files to this extension. The first dot (.) is implied.\n\t\t\t\t\tEx: -f htm") do |n|
|
132
160
|
@options[:filext] = n;
|
133
161
|
end
|
134
162
|
|
@@ -136,7 +164,7 @@ parser = OptionParser.new do|opts|
|
|
136
164
|
@options[:expath] = n;
|
137
165
|
end
|
138
166
|
|
139
|
-
opts.on('--expext STRING', "The export file\'s extension to reassign for all files. The first dot (.)
|
167
|
+
opts.on('--expext STRING', "The export file\'s extension to reassign for all files. The first dot (.)\n\t\t\t\t\tis implied. Defaults to same extension as original. Ex: --expext htm") do |n|
|
140
168
|
@options[:expext] = n;
|
141
169
|
end
|
142
170
|
|
@@ -154,7 +182,7 @@ parser = OptionParser.new do|opts|
|
|
154
182
|
end
|
155
183
|
|
156
184
|
opts.on_tail('-v', 'Show Subtxt release version') do
|
157
|
-
puts "You're using Subtxt v#{Subtxt::VERSION}"
|
185
|
+
puts "You're using Subtxt v#{Subtxt::VERSION}. Get the latest with gem update subtxt"
|
158
186
|
exit
|
159
187
|
end
|
160
188
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subtxt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Dominick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|