subtxt 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/subtxt/version.rb +1 -1
  3. data/lib/subtxt.rb +52 -24
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d499818212c731ddc510e2261c789f828e8a81d9
4
- data.tar.gz: 55bb3fef7817cd2d3979dc5bf2a1b33c9ecff1a5
3
+ metadata.gz: af5ad0c33acaeb9226bdb7d86010e844de05434a
4
+ data.tar.gz: d18798cf48643489886cdd8527e610e425b4f83f
5
5
  SHA512:
6
- metadata.gz: a781d64ae1d7fddfa1ad39add43fab6248262b7593fbf83ed6ace1ca55d162f8462744290e8970e394793799cb118245895a581e0e7348a97480c37d6262e02d
7
- data.tar.gz: 43407887e5a0613fddfea03310024645651c75424a07bf88fc6dc9e79b35ef9f666838fa23759a516d7b9749480e8509918d82098754532115817ff15f2c3098
6
+ metadata.gz: c3809a4778b976da2fb552f2cc4923f0be6d8d002c42cb2c5919691ae3f69194bce35d227fc8d73980da11e7cdba5ecc7fe36dc3b207dd889df734a84a3bb6c3
7
+ data.tar.gz: 8abb197e20d14012e34545735b53f3ec45c3c8776b78a931c6b047d11942304f413f2e6f3eef09448e58c65748fcaa1b2a13ae58f26e3ce867da22abaed0d986
@@ -1,3 +1,3 @@
1
1
  module Subtxt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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
- patterns_display = ""
47
- patterns.each do |rec|
48
- fndsize = rec['fnd'].size
49
- fndgap = 90 - fndsize
50
- if fndgap > 5
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
- replace = rec['rep'].gsub(/\\n/, "\n")
64
- text.gsub!(/#{rec['fnd']}/, replace)
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 he third row delimits the set for
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('-i', '--ingestdir', "Ingest files from this directory. Defaults to current directory. Superceded if a path is passed as\n\t\t\t\t\tthe first argument (subtxt path/to/files -p patterns.rgx). Ex: -i path/to/ingest/dir") do |n|
116
- @options[:ingestdir] = n;
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[:ingestdir] = ARGV[0]
147
+ @options[:patterns] = ARGV[0]
120
148
  end
121
149
 
122
- opts.on('-p PATH', '--patterns PATH', "Full (relative or absolute) path to a text file containing find & replace patterns in the\n\t\t\t\t\tdesignated format. REQUIRED. Ex: -p path/to/patterns.rgxp") do |n|
123
- @options[:patterns] = n;
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', 'Restrict ingested files to this extension. The first dot (.) is implied. Ex: -f htm') do |n|
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 (.) is implied. Defaults to same\n\t\t\t\t\textension as original. Defaults to #{@expath_def} Ex: --expext htm") do |n|
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.1.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-05 00:00:00.000000000 Z
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler