-
Notifications
You must be signed in to change notification settings - Fork 2
/
docx_rm_blank_paragraphs.rb
executable file
·168 lines (126 loc) · 3.36 KB
/
docx_rm_blank_paragraphs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env ruby
# encoding: utf-8
##########################################################
###
## File: docx_rm_blank_paragraphs.rb
## Desc: Removes blank paragraphs from an MS *.docx file
## By: Dewayne VanHoozer (dvanhoozer@gmail.com)
#
require 'debug_me' # A tool to print the labeled value of variables.
require 'fileutils' # STDLIB
require 'pathname' # STDLIB
require 'pathname_helpers'
require 'docx' # a ruby library/gem for interacting with .docx files
require 'docx_helpers'
include DocxHelpers
me = Pathname.new(__FILE__).realpath
my_dir = me.parent
my_name = me.basename.to_s
$options = {
verbose: false,
in_filenames: []
}
def verbose?
$options[:verbose]
end
usage = <<EOS
Removes blank paragraphs from an MS *.docx file
Usage: #{my_name} [options] filenames+
Where:
options Do This
-h or --help Display this message
-v or --verbose Display progress
filenames+ One or more *.docx filenames
NOTE:
Something_imporatant
EOS
# Check command line for Problems with Parameters
$errors = []
$warnings = []
# Get the next ARGV parameter after param_index
def get_next_parameter(param_index)
next_parameter = nil
if param_index+1 >= ARGV.size
$errors << "#{ARGV[param_index]} specified without parameter"
else
next_parameter = ARGV[param_index+1]
ARGV[param_index+1] = nil
end
ARGV[param_index] = nil
return next_parameter
end # def get_next_parameter(param_index)
# Get $options[:out_filename]
def get_out_filename(param_index)
filename_str = get_next_parameter(param_index)
$options[:out_filename] = Pathname.new( filename_str ) unless filename_str.nil?
end # def get_out_filename(param_index)
# Display global warnings and errors arrays and exit if necessary
def abort_if_errors
unless $warnings.empty?
STDERR.puts
STDERR.puts "The following warnings were generated:"
STDERR.puts
$warnings.each do |w|
STDERR.puts "\tWarning: #{w}"
end
STDERR.puts
end
unless $errors.empty?
STDERR.puts
STDERR.puts "Correct the following errors and try again:"
STDERR.puts
$errors.each do |e|
STDERR.puts "\t#{e}"
end
STDERR.puts
exit(-1)
end
end # def abort_if_errors
# Display the usage info
if ARGV.empty? ||
ARGV.include?('-h') ||
ARGV.include?('--help')
puts usage
exit
end
%w[ -v --verbose ].each do |param|
if ARGV.include? param
$options[:verbose] = true
ARGV[ ARGV.index(param) ] = nil
end
end
ARGV.compact!
ARGV.each do |a_file|
a_path = Pathname.new a_file
unless '.docx' == a_path.extname
$warnings << "Not a *.docx file: #{a_file}"
else
$options[:in_filenames] << a_path.realpath
end
end
if $options[:in_filenames].empty?
$errors << "No *.docx files were specified"
end
abort_if_errors
######################################################
# Local methods
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
pp $options
$options[:in_filenames].each do |a_path|
print "Processing #{a_path.basename} ... " if verbose?
a_path.backup
docx = open_docx(a_path.backup_path)
docx.paragraphs.each do |a_para|
content = a_para.text.chomp.strip
delete_paragraph(a_para) if content.empty?
end
docx.save(a_path)
print '.' unless verbose?
puts "done" if verbose?
end