-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgrepdocx
More file actions
executable file
·77 lines (66 loc) · 1.53 KB
/
grepdocx
File metadata and controls
executable file
·77 lines (66 loc) · 1.53 KB
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
#!/usr/bin/env ruby
$stderr.sync = true
begin
require "docopt"
rescue LoadError
STDERR.puts "The docopt gem could not be loaded, is it installed?"
STDERR.puts "-> gem install docopt"
exit 1
end
begin
require 'docx'
rescue LoadError
STDERR.puts "The docx gem could not be loaded, is it installed?"
STDERR.puts "-> gem install docx"
exit 1
end
doc = <<DOCOPT
Search any given Microsoft Word documents, selecting paragraphs that match a pattern.
Note that tables, headings etc are not searched, only paragraphs.
Usage:
#{__FILE__} [-i] <pattern> <docx>...
#{__FILE__} -h | --help
Options:
-i, --ignore-case Perform case insensitive matching.
-h, --help Show this output.
DOCOPT
begin
options = Docopt::docopt(doc)
rescue Docopt::Exit => e
STDERR.puts e.message
exit 1
end
# check arguments
options['<docx>'].each do |file|
if not File.exists?(file)
STDERR.puts "[!] #{file} does not exist!"
exit 1
end
end
options['<docx>'].each do |file|
begin
docx = Docx::Document.open(file)
rescue
STDERR.puts "[!] Error opening #{file}"
next
end
docx.each_paragraph do |p|
if options['--ignore-case']
if p.to_s.match(/#{options['<pattern>']}/i)
if options['<docx>'].length > 1
puts "#{file}: #{p.to_s}"
else
puts p
end
end
else
if p.to_s.match(/#{options['<pattern>']}/)
if options['<docx>'].length > 1
puts "#{file}: #{p.to_s}"
else
puts p
end
end
end
end
end