Module: Mn2pdf
- Defined in:
- lib/mn2pdf.rb,
lib/mn2pdf/version.rb
Constant Summary collapse
- FONTS_MANIFEST =
:font_manifest
- VERSION =
"2.10".freeze
- MN2PDF_JAR_VERSION =
VERSION
Class Method Summary collapse
- .build_args(url, output, xslt) ⇒ Object
- .convert(url_path, output_path, xsl_stylesheet, options = {}) ⇒ Object
- .dump_fontist_manifest_locations(manifest) ⇒ Object
- .help ⇒ Object
- .mn2pdf(args) ⇒ Object
- .mn2pdf_hash(args, options) ⇒ Object
- .options_to_cmd(options, cmd) ⇒ Object
- .parse_error_msg(stderr) ⇒ Object
- .puts_error_log(stdout, stderr) ⇒ Object
- .quote(str) ⇒ Object
- .version ⇒ Object
Class Method Details
.build_args(url, output, xslt) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/mn2pdf.rb', line 37 def self.build_args(url, output, xslt) return if url.nil? || output.nil? || xslt.nil? ["--xml-file", quote(url), "--xsl-file", quote(xslt), "--pdf-file", quote(output)] end |
.convert(url_path, output_path, xsl_stylesheet, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mn2pdf.rb', line 22 def self.convert(url_path, output_path, xsl_stylesheet, = {}) args = build_args(url_path, output_path, xsl_stylesheet) return unless args case when String mn2pdf(args + []) when Hash mn2pdf_hash(args, ) else warn "Unsupported options type #{.class}" end end |
.dump_fontist_manifest_locations(manifest) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/mn2pdf.rb', line 89 def self.dump_fontist_manifest_locations(manifest) Tempfile.create(["fontist_locations", ".yml"]) do |f| f.write manifest.to_yaml f.flush yield f.path end end |
.help ⇒ Object
11 12 13 14 15 |
# File 'lib/mn2pdf.rb', line 11 def self.help # help message goes to STDERR (from mn2pdf v1.36) _, , = Jvm.run end |
.mn2pdf(args) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/mn2pdf.rb', line 58 def self.mn2pdf(args) stdout, stderr, status = Jvm.run(args) unless status.success? puts_error_log(stdout, stderr) raise StandardError.new("mn2pdf failed! #{parse_error_msg(stderr)}") end end |
.mn2pdf_hash(args, options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mn2pdf.rb', line 45 def self.mn2pdf_hash(args, ) manifest = .delete(FONTS_MANIFEST) (, args) if manifest dump_fontist_manifest_locations(manifest) do |manifest_path| args << "--font-manifest" << quote(manifest_path) mn2pdf(args) end else mn2pdf(args) end end |
.options_to_cmd(options, cmd) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/mn2pdf.rb', line 98 def self.(, cmd) .each do |k, v| sep = k.to_s.end_with?("=") ? "" : " " cmd << "#{k}#{sep}#{quote(v)}" end end |
.parse_error_msg(stderr) ⇒ Object
76 77 78 79 80 |
# File 'lib/mn2pdf.rb', line 76 def self.parse_error_msg(stderr) err = stderr.split("\n").detect { |line| line.start_with? "Error: " } err ? err[7..-1] : stderr.strip end |
.puts_error_log(stdout, stderr) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/mn2pdf.rb', line 68 def self.puts_error_log(stdout, stderr) puts ["Fatal error!", "STDOUT:", stdout.strip, "STDERR:", stderr.strip] .join("\n") .split("\n") .map { |line| "[mn2pdf] #{line}" } .join("\n") end |
.quote(str) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/mn2pdf.rb', line 82 def self.quote(str) return "" if str.nil? || str.empty? return str if /^'.*'$/.match(str) || /^".*"$/.match(str) %("#{str}") end |