nguyen 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f506bb08df03fd015f7b9abac64a8788717daf5c3f3c7eaacc58f9c96590c7ef
4
- data.tar.gz: 7f425f695f794dbddef3e7550e212e845b9cbb85cd6c5e121c9f066e3b1beb53
3
+ metadata.gz: 74a1c2e3015f9f5338b2e3140a85f26bc7190d6884f5fcfcd22b76bf0e5ed23a
4
+ data.tar.gz: 9704aa598ac801cd59171aa081ca3c3d9b76d2dc3d9d0001ea33e9dceba9e217
5
5
  SHA512:
6
- metadata.gz: 0ed2625129d36a1175e10316e771591815b6f1ed71dd85c9d6a07c38ec237cc80dad4fd6df20c2fb6de4a165d067434bafb095ab1d93d70bc3c7a58fe3da0f07
7
- data.tar.gz: 4ceb1deadd047804d440e31230eeb49eaca8ddd6be5cf2c4295b6dbedb6001ea7aa9c9e63cc7c4336baa20a7ccb7c7dd803cff847f5ef60128910b5236ec9135
6
+ metadata.gz: 22d0fe163418da9d6023d757d1a2a48ccd5dd976ee186d6c68e5a5965d8b49ddba1638e324e286dfeee536bd310e5ec506b62270a55fe33472119c15b6bde85f
7
+ data.tar.gz: 8b8328c35cd0a98be573d9dd50fb6ddb79e27b9275df57be34b682016ca7c85ecc7e88e5e966f36a99e62d2de4e107018e1927eec23b13aaf83a494c39b63b8e
@@ -1,4 +1,6 @@
1
1
  require 'tempfile'
2
+ require 'open3'
3
+ require 'shellwords'
2
4
  module Nguyen
3
5
  class PdftkError < StandardError
4
6
  end
@@ -19,10 +21,10 @@ module Nguyen
19
21
  tmp = Tempfile.new('pdf_forms-fdf')
20
22
  tmp.close
21
23
  form_data.respond_to?(:save_to) ? form_data.save_to(tmp.path) : File.write(tmp.path, form_data)
22
- command = pdftk_command %Q("#{template}"), 'fill_form', %Q("#{tmp.path}"), 'output', destination, add_options(tmp.path)
23
- output = %x{#{command}}
24
+ args = [template, 'fill_form', tmp.path, 'output', destination, add_options(tmp.path)]
25
+ output = call_pdftk(*args)
24
26
  unless File.readable?(destination) && File.size(destination) > 0
25
- raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}")
27
+ raise PdftkError.new("failed to fill form with command\n#{describe_command(args)}\ncommand output was:\n#{output}")
26
28
  end
27
29
  ensure
28
30
  tmp.unlink if tmp
@@ -50,28 +52,37 @@ module Nguyen
50
52
  read(template).fields
51
53
  end
52
54
 
55
+ # invokes pdftk with the given arguments and returns its output (stdout
56
+ # and stderr combined); arguments are passed to the process verbatim,
57
+ # without any shell involved, so paths need no quoting or escaping
53
58
  def call_pdftk(*args)
54
- %x{#{pdftk_command args}}
59
+ output, _status = Open3.capture2e(pdftk, *normalize_args(args))
60
+ output
55
61
  end
56
62
 
57
- def cat(*files,output)
58
- files = files[0] if files[0].class == Array
59
- input = files.map{|f| %Q(#{f})}
60
- call_pdftk(*input,'output',output)
63
+ def cat(*files, output)
64
+ files = files.flatten
65
+ input = files.flat_map { |f| f.to_s.include?('*') ? Dir.glob(f) : [f] }
66
+ call_pdftk(*input, 'output', output)
61
67
  end
62
68
 
63
69
  protected
64
70
 
65
71
  def stamp_operation(operation, template, stamp_file, destination)
66
- command = pdftk_command %Q("#{template}"), operation, %Q("#{stamp_file}"), 'output', %Q("#{destination}")
67
- output = %x{#{command}}
72
+ args = [template, operation, stamp_file, 'output', destination]
73
+ output = call_pdftk(*args)
68
74
  unless File.readable?(destination) && File.size(destination) > 0
69
- raise PdftkError.new("failed to #{operation} with command\n#{command}\ncommand output was:\n#{output}")
75
+ raise PdftkError.new("failed to #{operation} with command\n#{describe_command(args)}\ncommand output was:\n#{output}")
70
76
  end
71
77
  end
72
78
 
73
- def pdftk_command(*args)
74
- "#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
79
+ def normalize_args(args)
80
+ args.flatten.compact.map(&:to_s)
81
+ end
82
+
83
+ # human readable rendition of a pdftk invocation, for error messages
84
+ def describe_command(args)
85
+ Shellwords.join([pdftk, *normalize_args(args)])
75
86
  end
76
87
 
77
88
  def add_options(pwd)
@@ -81,7 +92,8 @@ module Nguyen
81
92
  opt_args << 'flatten'
82
93
  end
83
94
  if options[:encrypt]
84
- opt_args.concat ['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
95
+ opt_args.concat ['encrypt_128bit', 'owner_pw', pwd]
96
+ opt_args.concat Shellwords.split(options[:encrypt_options]) if options[:encrypt_options]
85
97
  end
86
98
  opt_args
87
99
  end
@@ -1,3 +1,3 @@
1
1
  module Nguyen
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nguyen
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trung Lê