make_pdf-jekyll 0.0.2 → 0.0.3

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: 97065b7cf75979dc847f0d82e96bf54229ea7dc4329eab0216a899ac6c72496a
4
- data.tar.gz: cc8b2e3a7a25fb598079829a4198679214ac9a9e672a20c7e4fd1ea71f8cbf0e
3
+ metadata.gz: 1d09dc6c6f9822fd4b47f10deded424f1cbe2f7766de5fc928ff3a9dd6a6469b
4
+ data.tar.gz: 06c2c69b10b75f5034629c315a376cd7c47f3606df2f77fb2f3405cbec12d092
5
5
  SHA512:
6
- metadata.gz: 5926bb93054a88d248eef00b4c5aa06d00768f3c7ced9d1b79fdcb713de3e732922406f4d0b365cc933d956a912992307e18ee0ac1cbc6fc612f1feb1bf3033a
7
- data.tar.gz: 18ecbb2c38aabde9c0994fd6a4fb295a23d7b0cf1ef7c3cb7eabc5c508e0e587f7fc3484955f6a5248b0a24c0c8ca332295947ac263fd4803d7c9d3c3675fdc8
6
+ metadata.gz: 862703fcd0e8f7d88ec5a2e25ed96b3ad9b96411556863656d5f3b367d8de08d87ccfe51ca9e38918d1a0d3fb4d66dac69d8c5cd2917c461cbf8492ed8f4fa23
7
+ data.tar.gz: de78393e5aed6ae9543b6f9ea799675a795ee65c4472802f8e9602277e373173900bf8376841526c269812f7fbc5e1839af70e40579140f3289819f011e95eb2
@@ -3,9 +3,48 @@ require "make_pdf/command_based"
3
3
  module MakePDF
4
4
  class Chrome < CommandBased::Writer
5
5
  COMMAND = 'chrome-headless-render-pdf'
6
+ DEFAULT_OPTIONS = { # options from 'chrome-headless-render-pdf --help'
7
+ "chrome-binary": nil,
8
+ "chrome-option": nil,
9
+ "remote-host": nil,
10
+ "remote-port": nil,
11
+ "no-margins": true,
12
+ "include-background": true,
13
+ "landscape": false,
14
+ "window-size": nil,
15
+ "paper-width": nil,
16
+ "paper-height": nil,
17
+ "page-ranges": nil,
18
+ "scale": nil,
19
+ "display-header-footer": false,
20
+ "header-template": nil,
21
+ "footer": nil,
22
+ "js-time-budget": nil,
23
+ "animation-time-budget": nil,
24
+ }
6
25
 
7
- def initialize(source_url, output_dif, **options)
8
- super(source_url, output_dif, command: COMMAND, **options)
26
+ def initialize(source_url, output_dir, **options)
27
+ super(source_url, output_dir, command: COMMAND, **options.merge(DEFAULT_OPTIONS))
28
+ end
29
+
30
+ def map_key(key, value)
31
+ case key
32
+ when 'source_url'
33
+ [ "--url", value ]
34
+ when 'output_filename'
35
+ [ "--pdf", value ]
36
+ when DEFAULT_OPTIONS.keys.method(:include?)
37
+ case value
38
+ when false, nil?
39
+ []
40
+ when true, ""
41
+ [ "--#{key.to_s}" ]
42
+ else
43
+ [ "--#{key.to_s}", value ]
44
+ end
45
+ else
46
+ []
47
+ end
9
48
  end
10
49
  end
11
50
  end
@@ -7,8 +7,12 @@ module MakePDF
7
7
  module Arguments
8
8
  attr_reader :base
9
9
 
10
- def make_arguments(options = [], **more)
11
- options.concat(more.map { |key, value| "#{self.class.prefix}#{key}#{"=#{value}" unless value == true || value.nil?}" })
10
+ def make_arguments(*options, **more)
11
+ [ options, more.map do |key, value|
12
+ map_key(key.to_s, value)
13
+ end ].flatten.map do |arg|
14
+ arg.to_s
15
+ end
12
16
  end
13
17
  end
14
18
 
@@ -26,26 +30,25 @@ module MakePDF
26
30
  @options = options
27
31
  end
28
32
 
29
- def write(file, base_path:, **options)
30
- logger.info("pdf-writer (#{command}): converting #{file}")
31
-
32
- url = source_url(file, base_path:, **options),
33
- output = output_for(file, base_path:, **options)
33
+ def write(source_url, output_filename, base_path:, **options)
34
+ logger.info("converting #{source_url} with #{@command}")
34
35
  arguments = make_arguments(
35
36
  command: @command,
36
- url:,
37
- pdf: output
37
+ source_url:,
38
+ output_filename:,
39
+ **options
38
40
  )
39
- IO.popen([@command] + arguments, {:err =>[ :child, :out]}) do |pipe|
40
- std_out = pipe.read
41
+ logger.debug("Executing #{@command} #{arguments}")
42
+ std_out = IO.popen([@command] + arguments, {:err =>[ :child, :out]}) do |pipe|
43
+ pipe.read
41
44
  end
42
-
43
- raise std_out if ($? != 0)
44
- logger.info("pdf-writer (#{command}): Wrote #{output}")
45
+ status = $?
46
+ raise RuntimeError.new("Failure executing #{command} with #{arguments}.\n\noutput:\n\n---\n#{std_out}\n---\n") if status != 0
47
+ logger.info("pdf-writer: Wrote #{output_filename}")
45
48
  end
46
49
 
47
50
  def output_for(file, base_path:, version: [], **options)
48
- if @output.nil?
51
+ if @output_dir.nil?
49
52
  path = File.dirname(file)
50
53
  else
51
54
  path = File.expand_path(relative_path(file, base_path), @output)
@@ -3,9 +3,33 @@ require 'make_pdf'
3
3
 
4
4
  module MakePDF
5
5
 
6
+ LOG_NAME = "make_pdf:"
6
7
  # MakePDF Jekyll plugin
7
8
  class Jekyll
8
9
 
10
+ class Logger
11
+
12
+ def initialize(logger)
13
+ @logger = logger
14
+ end
15
+
16
+ def message(*args, **options)
17
+ @logger.message(LOG_NAME, *args, **options)
18
+ end
19
+
20
+ def warn(*args, **options)
21
+ @logger.warn(LOG_NAME, *args, **options)
22
+ end
23
+
24
+ def info(*args, **options)
25
+ @logger.info(LOG_NAME, *args, **options)
26
+ end
27
+
28
+ def debug(*args, **options)
29
+ @logger.debug(LOG_NAME, *args, **options)
30
+ end
31
+ end
32
+
9
33
  class << self
10
34
  include PathManip
11
35
  attr_reader :file, :site, :current_doc, :options, :command, :output
@@ -15,16 +39,17 @@ module MakePDF
15
39
  @site = current_doc.site
16
40
  @options = @site.config['make-pdf'] || {}
17
41
  @opt_in = @options['write-by-default'] || false
18
- @base_url = @options['source'] || "file:/"
42
+ @base_url = @options['source'] || "file:"
19
43
  @base_source = @site.dest
44
+ @logger = Logger.new(::Jekyll.logger)
20
45
 
21
- ::Jekyll.logger.debug("make_pdf:", "Initialized with #{@options}. #{@base_source}")
46
+ @logger.debug("Initialized with #{@options}. #{@base_source}")
22
47
  end
23
-
48
+
24
49
  current_options = @options.merge(current_doc.data)
25
- ::Jekyll::logger.debug("make-pdf", current_options)
50
+ @logger.debug(current_options)
26
51
  bail = lambda do |error|
27
- ::Jekyll.logger.debug("make_pdf:", error)
52
+ @logger.debug(error)
28
53
  false
29
54
  end
30
55
 
@@ -40,15 +65,15 @@ module MakePDF
40
65
  return bail.call("#{current_doc.name} has not opted in") if current_doc.data['make-pdf'].nil? && !@opt_in
41
66
  return bail.call("#{current_doc.name} has opted out")if current_doc.data['make-pdf'] == false
42
67
 
43
- ::Jekyll.logger.info("make_pdf:", " processing #{current_doc.name}")
68
+ @logger.info(" processing #{current_doc.name}")
44
69
 
45
- @writer.new(file, output, logger: ::Jekyll.logger, **current_options)
70
+ @writer.new(file, output_dir, base_source: @base_source, logger: @logger, **current_options)
46
71
  end
47
72
 
48
73
  def process(current_doc)
49
74
  writer = setup(current_doc)
50
75
 
51
- ::Jekyll::logger.debug("make_pdf:", " Ignoring #{current_doc.destination("")}")
76
+ @logger.debug(" Ignoring #{current_doc.destination("")}")
52
77
  return if writer === false
53
78
 
54
79
  options = current_doc.data['targets']&.split(';') || []
@@ -59,21 +84,22 @@ module MakePDF
59
84
  end
60
85
 
61
86
  def render_option(writer, file, **options)
62
- ::Jekyll.logger.debug('MakePDF options:', options)
87
+ @logger.debug("MakePDF options: #{options}")
63
88
 
64
89
  raise "File #{file} is not accessible" unless File.readable?(file)
65
90
 
66
91
  attempted = 0
92
+
67
93
  begin
68
94
  writer.write(file, **options)
69
95
  rescue => error
70
96
  attempted += 1
71
97
  if attempted <= 2
72
- ::Jekyll.logger.warn("MakePDF: Failed to generate #{file} retrying #{attempted}")
73
- ::Jekyll.logger.warn("MakePDF: ERROR: #{error}")
98
+ @logger.warn("Failed to generate #{file} retrying #{attempted}")
99
+ @logger.warn("ERROR: #{error}")
74
100
  retry
75
101
  else
76
- ::Jekyll.logger.error("MakePDF: Skipping generation of #{file} with #{options}")
102
+ @logger.warn("Skipping generation of #{file} with #{options}")
77
103
  raise error
78
104
  end
79
105
  end
@@ -81,7 +107,7 @@ module MakePDF
81
107
  end
82
108
  end
83
109
 
84
- ::Jekyll.logger.info('MakePDF:', "loaded")
110
+ ::Jekyll.logger.info(LOG_NAME, "loaded")
85
111
  ::Jekyll::Hooks.register [:pages, :documents, :posts], :post_write do |doc|
86
112
  MakePDF::Jekyll.process(doc)
87
113
  end
data/lib/make_pdf.rb CHANGED
@@ -40,30 +40,37 @@ module MakePDF
40
40
  @logger = logger
41
41
  @options = options
42
42
  end
43
+
44
+ def make_source_url(file, base_path:, base_url: nil, **options)
45
+ base_path = Pathname.new(base_path).realpath
46
+ target_file = Pathname.new(file).realpath.relative_path_from(base_path)
47
+ if (base_url != "file:")
48
+ return base_url + target_file.to_s
49
+ else
50
+ return base_path + target_file.to_s
51
+ end
52
+ end
43
53
 
44
- def source_url(file, base_path:Pathname.new("."), base_url: "file:/", version: [], separator: ",", **options)
45
- file_path = path_of(file)
46
-
47
- options = unless version.empty?
48
- "##{version.join(separator)}"
49
- else
50
- ""
51
- end
52
- "#{base_url}#{relative_path(file_path, base_path)}/#{file_path.basename}#{options}"
54
+ def make_pdf_filename(file, **options)
55
+ path_of(file).basename.sub_ext(".pdf")
53
56
  end
54
57
 
55
- def output_for(file, version, **options)
56
- output = path_of(file).basename(".pdf").to_s
57
- path_of(@output_dir, output + version.join("_") + ".pdf")
58
+ def make_output_filename(file, base_path:, **options)
59
+ filename = make_pdf_filename(file, base_path:, **options)
60
+ output = @output_dir / filename
61
+ @logger.debug("filenane : #{filename} output_dir: #{output_dir} output_filename: #{output}")
62
+ output
58
63
  end
59
64
 
60
65
  def process(file, version: [], **options)
61
66
  options = @options.merge(options)
67
+ output_filename = make_output_filename(file, version: version, **options)
62
68
  write(
63
- source_url(file, vesrsion: version, **options),
64
- output_for(file, version: version, **options),
69
+ make_source_url(file, vesrsion: version, **options),
70
+ output_filename,
65
71
  **options
66
72
  )
73
+ output_filename
67
74
  end
68
75
  end
69
76
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_pdf-jekyll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Bogado da Silva Lins