subtxt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d499818212c731ddc510e2261c789f828e8a81d9
4
+ data.tar.gz: 55bb3fef7817cd2d3979dc5bf2a1b33c9ecff1a5
5
+ SHA512:
6
+ metadata.gz: a781d64ae1d7fddfa1ad39add43fab6248262b7593fbf83ed6ace1ca55d162f8462744290e8970e394793799cb118245895a581e0e7348a97480c37d6262e02d
7
+ data.tar.gz: 43407887e5a0613fddfea03310024645651c75424a07bf88fc6dc9e79b35ef9f666838fa23759a516d7b9749480e8509918d82098754532115817ff15f2c3098
data/bin/subtxt ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'subtxt'
data/lib/subtxt.rb ADDED
@@ -0,0 +1,173 @@
1
+ require "subtxt/version"
2
+ require 'optparse'
3
+ require 'logger'
4
+ require 'fileutils'
5
+
6
+ @ingestdir_def = "."
7
+ @filext_def = "*"
8
+ @expath_def = "_subtxt/output"
9
+
10
+ @options = {}
11
+ @options[:ingestdir] = @ingestdir_def
12
+ @options[:filext] = @filext_def
13
+ @options[:expath] = @expath_def
14
+
15
+ @logger = Logger.new(STDOUT)
16
+ @logger.level = Logger::WARN
17
+ @logger.formatter = proc do |severity, datetime, progname, msg|
18
+ "#{severity}: #{msg}\n"
19
+ end
20
+
21
+ def load_patterns pfile
22
+ records = []
23
+ File.open(pfile, "r") do |pats|
24
+ pair = {}
25
+ rowct = 0
26
+ pats.each_line do |row|
27
+ break if row.chomp.downcase == "eof"
28
+ unless pair['fnd'] and pair['rep']
29
+ unless pair['fnd']
30
+ pair['fnd'] = row.chomp
31
+ else
32
+ pair['rep'] = row.chomp
33
+ end
34
+ else
35
+ records << pair
36
+ pair = {}
37
+ end
38
+ end
39
+ end
40
+ return records
41
+ end
42
+
43
+ def subtexts opts
44
+ patterns = load_patterns(opts[:patterns])
45
+ @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"
59
+ Dir.glob(opts[:ingestpath]) do |f|
60
+ text = File.read(f)
61
+ @logger.debug "Processing file: #{File.basename(f)}"
62
+ patterns.each do |rec|
63
+ replace = rec['rep'].gsub(/\\n/, "\n")
64
+ text.gsub!(/#{rec['fnd']}/, replace)
65
+ end
66
+ unless opts[:expext]
67
+ outfile = File.basename f
68
+ else
69
+ fn = File.basename(f,".*")
70
+ outfile = "#{fn}.#{opts[:expext]}"
71
+ end
72
+ begin
73
+ FileUtils::mkdir_p(opts[:expath]) unless File.exists?(opts[:expath])
74
+ File.open("#{opts[:expath]}/#{outfile}", 'w') { |file| file.write(text) }
75
+ @logger.debug "File saved (#{outfile})"
76
+ rescue Exception => ex
77
+ raise "Failure: #{ex}"
78
+ end
79
+ end
80
+ end
81
+
82
+ parser = OptionParser.new do|opts|
83
+ opts.banner = """
84
+ Subtxt is a simple utility for matching and replacing patterns in target text.
85
+ It searches all of the files in a directory (optionally one type at a time)
86
+ for multiple patterns, each with its own dynamic replcement. Subtxt uses Ruby
87
+ regular expression (regex) patterns for matching and substituting text.
88
+ Check out http://refiddle.com/ and http://www.rexegg.com/regex-quickstart.html
89
+
90
+ 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
92
+ the convenience of your eyeballs. Like so:
93
+ \t---------------------------------------
94
+ \tfind pattern
95
+ \treplace pattern
96
+ \t
97
+ \t(pattern|string|content)-(to)-(find)
98
+ \t$1 $2 replace
99
+ \t
100
+ \tregular expression pattern
101
+ \ttokenized substitute output
102
+ \t
103
+ \tEOF
104
+ \t---------------------------------------\n
105
+ Usage: subtxt [path/to/ingest/dir] [options]
106
+ Options:
107
+ """
108
+
109
+ unless ARGV[0]
110
+ @logger.error "You must at least provide a patterns file option. For help, use\nsubtxt --help"
111
+ exit
112
+ end
113
+
114
+ 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;
117
+ end
118
+ else # the first arg has no leading - or --, it must be our path
119
+ @options[:ingestdir] = ARGV[0]
120
+ end
121
+
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;
124
+ end
125
+
126
+ ## TODO recursion
127
+ # opts.on('-r', '--recursive', 'Whether to process the input directory recursively (traverse subdirectories).') do
128
+ # @options[:recursive] = true
129
+ # end
130
+
131
+ opts.on('-f STRING', '--filext STRING', 'Restrict ingested files to this extension. The first dot (.) is implied. Ex: -f htm') do |n|
132
+ @options[:filext] = n;
133
+ end
134
+
135
+ opts.on('-x PATH', '--expath PATH', 'Location for saving the converted files. Ex: -x processed/files/dir') do |n|
136
+ @options[:expath] = n;
137
+ end
138
+
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|
140
+ @options[:expext] = n;
141
+ end
142
+
143
+ opts.on('--verbose', 'Print INFO level logs to console.') do
144
+ @options[:verbose] = true
145
+ end
146
+
147
+ opts.on('--debug', 'Print DEBUG (and INFO) level logs to console.') do
148
+ @options[:debug] = true
149
+ end
150
+
151
+ opts.on('-h', '--help', 'Displays help menu') do
152
+ puts opts
153
+ exit
154
+ end
155
+
156
+ opts.on_tail('-v', 'Show Subtxt release version') do
157
+ puts "You're using Subtxt v#{Subtxt::VERSION}"
158
+ exit
159
+ end
160
+ end
161
+ parser.parse!
162
+
163
+ # options postprocessing
164
+ @options[:ingestpath] = "#{@options[:ingestdir]}/*.#{@options[:filext]}"
165
+ if @options[:verbose]
166
+ @logger.level = Logger::INFO
167
+ end
168
+ if @options[:debug]
169
+ @logger.level = Logger::DEBUG
170
+ end
171
+
172
+ # call the proc
173
+ subtexts(@options)
@@ -0,0 +1,3 @@
1
+ module Subtxt
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subtxt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Dominick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A simple text conversion utility using regular expressions for searching
42
+ and replacing multiple strings across multiple files, for conversion projects.
43
+ email:
44
+ - badominick@gmail.com
45
+ executables:
46
+ - subtxt
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - bin/subtxt
51
+ - lib/subtxt.rb
52
+ - lib/subtxt/version.rb
53
+ homepage: https://github.com/DocOps/subtxt
54
+ licenses: []
55
+ metadata:
56
+ allowed_push_host: https://rubygems.org
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.8
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: A simple utility for converting multiple strings across multiple files, for
77
+ conversion projects.
78
+ test_files: []