make_pdf-jekyll 0.0.3 → 0.0.4

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: 1d09dc6c6f9822fd4b47f10deded424f1cbe2f7766de5fc928ff3a9dd6a6469b
4
- data.tar.gz: 06c2c69b10b75f5034629c315a376cd7c47f3606df2f77fb2f3405cbec12d092
3
+ metadata.gz: e5a21bc7883012692bab3b448615f5a40c58a37566c23d678e67f7f4cad520de
4
+ data.tar.gz: 905cf312025602478eebabd00a45477603f40e2785261297e5312321bf3c1d90
5
5
  SHA512:
6
- metadata.gz: 862703fcd0e8f7d88ec5a2e25ed96b3ad9b96411556863656d5f3b367d8de08d87ccfe51ca9e38918d1a0d3fb4d66dac69d8c5cd2917c461cbf8492ed8f4fa23
7
- data.tar.gz: de78393e5aed6ae9543b6f9ea799675a795ee65c4472802f8e9602277e373173900bf8376841526c269812f7fbc5e1839af70e40579140f3289819f011e95eb2
6
+ metadata.gz: 80b67fdfb51e5b34d3eebac122a4d6793d543c80c27ff9b416d63cee957928c229605a4ec4f81dd71099f0c403bfa74c4a5112ba93c1ded4cb0e4f7a1549e4a0
7
+ data.tar.gz: b30b0279151ba01a329c5f0e1d99c3ddc5e2677ba85704065c0fe187c080ad327cc5661bbd3014c6a94970b97df34aa527ce2525043fb87af009033c119a64d8
@@ -23,16 +23,16 @@ module MakePDF
23
23
  "animation-time-budget": nil,
24
24
  }
25
25
 
26
- def initialize(source_url, output_dir, **options)
27
- super(source_url, output_dir, command: COMMAND, **options.merge(DEFAULT_OPTIONS))
26
+ def initialize(**options)
27
+ super(command: COMMAND, **options.merge(DEFAULT_OPTIONS))
28
28
  end
29
29
 
30
- def map_key(key, value)
30
+ def map_option_key(key, value)
31
31
  case key
32
- when 'source_url'
33
- [ "--url", value ]
34
- when 'output_filename'
35
- [ "--pdf", value ]
32
+ when 'source-url'
33
+ [ '--url', value ]
34
+ when 'output-filename'
35
+ [ '--pdf', value ]
36
36
  when DEFAULT_OPTIONS.keys.method(:include?)
37
37
  case value
38
38
  when false, nil?
@@ -8,11 +8,14 @@ module MakePDF
8
8
  attr_reader :base
9
9
 
10
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
11
+ [
12
+ options.map { |val| val.to_s },
13
+ more
14
+ .transform_keys { |key| key.to_s.gsub('_','-') }
15
+ .filter_map do |key, value|
16
+ map_option_key(key, value.to_s)
17
+ end
18
+ ].flatten
16
19
  end
17
20
  end
18
21
 
@@ -24,13 +27,12 @@ module MakePDF
24
27
  '--'
25
28
  end
26
29
 
27
- def initialize(url, output_dir, command: COMMAND, **options)
28
- super(url, output_dir, **options)
30
+ def initialize(command: COMMAND, **options)
31
+ super(**options)
29
32
  @command = command
30
- @options = options
31
33
  end
32
34
 
33
- def write(source_url, output_filename, base_path:, **options)
35
+ def write(source_url, output_filename, **options)
34
36
  logger.info("converting #{source_url} with #{@command}")
35
37
  arguments = make_arguments(
36
38
  command: @command,
@@ -47,11 +49,14 @@ module MakePDF
47
49
  logger.info("pdf-writer: Wrote #{output_filename}")
48
50
  end
49
51
 
50
- def output_for(file, base_path:, version: [], **options)
52
+ def output_for(file, output_base_path: ".", version: [], **options)
53
+ output_base_path = Pathname.new(output_base_path)
54
+ file = Pathname.new(file)
55
+
51
56
  if @output_dir.nil?
52
- path = File.dirname(file)
57
+ path = output_base_path / file.dirname
53
58
  else
54
- path = File.expand_path(relative_path(file, base_path), @output)
59
+ path = File.expand_path(relative_path(file, output_base_path), @output_dir)
55
60
  end
56
61
 
57
62
  FileUtils.mkdir_p path
@@ -2,113 +2,152 @@ require 'jekyll'
2
2
  require 'make_pdf'
3
3
 
4
4
  module MakePDF
5
-
6
5
  LOG_NAME = "make_pdf:"
6
+
7
7
  # MakePDF Jekyll plugin
8
8
  class Jekyll
9
+ attr_reader :reason
9
10
 
10
- class Logger
11
+ def valid?
12
+ @reason.nil?
13
+ end
11
14
 
12
- def initialize(logger)
13
- @logger = logger
14
- end
15
+ def check_failure(condition, message)
16
+ @reason = message if condition
17
+ condition
18
+ end
15
19
 
16
- def message(*args, **options)
17
- @logger.message(LOG_NAME, *args, **options)
18
- end
20
+ def filter_options(document, **options)
21
+ document.data.filter_map do |key, value|
22
+ key = key.to_s
23
+ if key == "make-pdf"
24
+ possible = {
25
+ "" => true,
26
+ "true" => true,
27
+ "yes" => true,
28
+ "false" => false,
29
+ "no" => false
30
+ }
31
+
32
+ [ key, possible[value.downcase] ]
33
+ else
34
+ [ key.sub("make-pdf-", "").to_sym, value ] if key.start_with?("make-pdf-")
35
+ end
36
+ end.to_h.merge(options)
37
+ end
19
38
 
20
- def warn(*args, **options)
21
- @logger.warn(LOG_NAME, *args, **options)
22
- end
39
+ def initialize(current_doc, **options)
40
+ @file = current_doc.destination(@base_source)
41
+ @options = filter_options(current_doc, **options)
42
+ logger.debug("base_paths: input → #{@options[:input_base_path]} output → #{@options[:output_base_path]}")
23
43
 
24
- def info(*args, **options)
25
- @logger.info(LOG_NAME, *args, **options)
26
- end
44
+ current_options = make_options(@options, site_options, filter_options(current_doc))
45
+ output_dir = @options[:output_dir] || path_of(site.dest).dirname
46
+
47
+ logger.debug("options : #{current_options}")
48
+
49
+ return if check_failure(File.extname(@file) != '.html', "#{@file} is not an html")
50
+ return if check_failure(current_options[:make_pdf].nil? && !@opt_in, "#{current_doc.name} has not opted in")
51
+ return if check_failure(current_options[:make_pdf] == false, "#{current_doc.name} has opted out")
27
52
 
28
- def debug(*args, **options)
29
- @logger.debug(LOG_NAME, *args, **options)
53
+ writer = current_options[:writer]
54
+ return if check_failure(writer.nil?, "No writer defined for #{current_doc.name} (#{writer})")
55
+
56
+ logger.info(" processing #{current_doc.name}")
57
+ @writer = MakePDF.const_get(writer.capitalize).new(logger:, **current_options)
58
+ end
59
+
60
+ def targets
61
+ @options[:targets] || ""
62
+ end
63
+
64
+ def method_missing(method_name, *args, **options)
65
+ if not Jekyll.site_options.nil? and Jekyll.site_options.include?(method_name)
66
+ return Jekyll.site_options[method_name]
67
+ elsif Jekyll.respond_to?(method_name, false)
68
+ return Jekyll.send(method_name, *args, **options)
69
+ else
70
+ super
30
71
  end
31
72
  end
32
73
 
33
- class << self
34
- include PathManip
35
- attr_reader :file, :site, :current_doc, :options, :command, :output
36
-
37
- def setup(current_doc)
38
- if @site.nil?
39
- @site = current_doc.site
40
- @options = @site.config['make-pdf'] || {}
41
- @opt_in = @options['write-by-default'] || false
42
- @base_url = @options['source'] || "file:"
43
- @base_source = @site.dest
44
- @logger = Logger.new(::Jekyll.logger)
45
-
46
- @logger.debug("Initialized with #{@options}. #{@base_source}")
74
+ def render_option(**options)
75
+ logger.debug("MakePDF rendering options #{options}")
76
+
77
+ attempted = 0
78
+ begin
79
+ @writer.process(@file, **options.merge(@options))
80
+ rescue => error
81
+ attempted += 1
82
+ if attempted <= 2
83
+ logger.warn("Failed to generate #{@file} retrying #{attempted}")
84
+ logger.warn("ERROR: #{error}")
85
+ retry
86
+ else
87
+ logger.warn("Skipping generation of #{@file} with #{options}")
88
+ raise error
47
89
  end
90
+ end
91
+ end
48
92
 
49
- current_options = @options.merge(current_doc.data)
50
- @logger.debug(current_options)
51
- bail = lambda do |error|
52
- @logger.debug(error)
53
- false
93
+ def process(**options)
94
+ render_option(**options)
95
+ unless self.targets.nil?
96
+ self.targets.split(",").each do |option|
97
+ render_option(version: option.split(","), **options)
54
98
  end
99
+ end
100
+ end
55
101
 
56
- writer = current_options['writer']
57
- return bail.call("No writer defined for #{current_doc.name} (#{writer})") if writer.nil?
58
-
59
- @writer = MakePDF.const_get(current_options['writer'].capitalize)
60
-
61
- file = current_doc.destination(@base_source)
62
- output_dir = @options['output_dir'] || path_of(file).dirname
63
-
64
- return bail.call("#{file} is not an html") if File.extname(file) != '.html'
65
- return bail.call("#{current_doc.name} has not opted in") if current_doc.data['make-pdf'].nil? && !@opt_in
66
- return bail.call("#{current_doc.name} has opted out")if current_doc.data['make-pdf'] == false
102
+ class << self
103
+ include PathManip
104
+ attr_reader :site_options, :site
67
105
 
68
- @logger.info(" processing #{current_doc.name}")
106
+ def make_options(options, *more_options)
107
+ return {} if options.nil?
69
108
 
70
- @writer.new(file, output_dir, base_source: @base_source, logger: @logger, **current_options)
109
+ [ options, more_options ].flatten
110
+ .reduce(:merge)
111
+ .transform_keys do |key|
112
+ key.to_s.sub("-", "_").to_sym
113
+ end
71
114
  end
72
115
 
73
- def process(current_doc)
74
- writer = setup(current_doc)
116
+ def logger(**options)
117
+ @logger ||= MakePDF::Logger.new(**options)
118
+ end
75
119
 
76
- @logger.debug(" Ignoring #{current_doc.destination("")}")
77
- return if writer === false
120
+ def setup(site, **options)
121
+ return unless @site.nil?
122
+
123
+ config = site.config["make-pdf"]||{}
124
+ logger(logger: ::Jekyll.logger, level: (config["log-map-level"] || :debug).to_sym, verbose: config['log-verbose'])
125
+ @site = site
126
+ @site_options = {
127
+ :output_base_path => site.source,
128
+ :input_base_path => site.dest,
129
+ :input_scheme => "file"
130
+ }.merge(make_options(@site.config["make-pdf"], options))
131
+ logger.debug("Initialized with #{self.site_options}.")
132
+ end
78
133
 
79
- options = current_doc.data['targets']&.split(';') || []
80
- file = current_doc.destination(@site.dest)
134
+ def process(current_doc, **options)
135
+ setup(current_doc.site, **options) if @site.nil?
81
136
 
82
- render_option(writer, file, base_path: @site.dest, base_url: @base_url)
83
- options.each { |option| render_option(writer, file, base_path: @site.dest, base_url: @base_url, version: option.split(",")) }
84
- end
137
+ processor = self.new(current_doc)
85
138
 
86
- def render_option(writer, file, **options)
87
- @logger.debug("MakePDF options: #{options}")
88
-
89
- raise "File #{file} is not accessible" unless File.readable?(file)
90
-
91
- attempted = 0
92
-
93
- begin
94
- writer.write(file, **options)
95
- rescue => error
96
- attempted += 1
97
- if attempted <= 2
98
- @logger.warn("Failed to generate #{file} retrying #{attempted}")
99
- @logger.warn("ERROR: #{error}")
100
- retry
101
- else
102
- @logger.warn("Skipping generation of #{file} with #{options}")
103
- raise error
104
- end
139
+ unless processor.valid?
140
+ logger.debug "Ignoring #{current_doc.name} #{processor.reason}"
141
+ return false
105
142
  end
143
+
144
+ processor.process(**@site_options)
106
145
  end
107
146
  end
108
147
  end
148
+ end
109
149
 
110
- ::Jekyll.logger.info(LOG_NAME, "loaded")
111
- ::Jekyll::Hooks.register [:pages, :documents, :posts], :post_write do |doc|
112
- MakePDF::Jekyll.process(doc)
113
- end
150
+ ::Jekyll.logger.info("Loaded #{MakePDF::LOG_NAME} plugin")
151
+ ::Jekyll::Hooks.register [:pages, :documents, :posts], :post_write do |doc|
152
+ MakePDF::Jekyll.process(doc)
114
153
  end
data/lib/make_pdf.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'selenium-webdriver'
3
+ require 'fileutils'
4
4
 
5
5
  module MakePDF
6
6
  module PathManip
@@ -22,43 +22,97 @@ module MakePDF
22
22
  end
23
23
 
24
24
  class Logger
25
- def debug(*args)
25
+ LEVELS = [ :debug, :info, :warn, :error ]
26
+
27
+ def level
28
+ @min_level || 0
29
+ end
30
+
31
+ def initialize(logger: nil, level: :debug, verbose: false)
32
+ @logger = logger
33
+ @min_level = LEVELS.index(level) || 2
34
+ @verbose = verbose
35
+ write(:debug, "logging at least level #{LEVELS[@min_level].to_s} with #{logger}")
36
+ end
37
+
38
+ def write(level, *args)
39
+ print("#{level.to_s} : ", *args.map do |msg|
40
+ if (msg.size > 80)
41
+ msg[0..79] + "…"
42
+ else
43
+ msg
44
+ end
45
+ end.join("\n"), "\n")
46
+ return
26
47
  end
27
48
 
28
- alias info debug
29
- alias warn debug
30
- alias error debug
49
+ def verbose(*args)
50
+ if (@verbose)
51
+ @logger.send(LEVELS[self.level], LOG_NAME, *args)
52
+ end
53
+ end
54
+
55
+ def method_missing(method_name, *args, **options)
56
+ if @logger.nil? and LEVELS.include?(method_name)
57
+ return write(method_name, *args)
58
+ end
59
+
60
+ if accepts?(method_name)
61
+ @logger.send(LEVELS[[LEVELS.index(method_name), self.level].max], LOG_NAME, *args, **options)
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ def accepts?(method_name)
68
+ LEVELS.include?(method_name) and @logger.respond_to?(method_name, false)
69
+ end
70
+
71
+ def respond_to_missing?(method_name, include_private = false)
72
+ accepts?(method_name) || super
73
+ end
31
74
  end
32
75
 
33
76
  class PDFWriter
34
77
  include PathManip
35
78
  attr_reader :output_dir, :source_url, :logger
36
79
 
37
- def initialize(source_url, output_dir, logger: Logger.new() ,**options)
38
- @source_url = source_url
39
- @output_dir = path_of(output_dir)
80
+ def initialize(input_base_path:, output_base_path:, input_scheme: "file", logger: Logger.new() ,**options)
40
81
  @logger = logger
41
- @options = options
82
+ @options = options.merge({ input_base_path:, output_base_path:, input_scheme: })
42
83
  end
43
84
 
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
85
+ def make_source_url(file, input_base_path:, output_base_path:, input_scheme: , **options)
86
+ target_file = relative_path(file, input_base_path:, **options)
87
+ if (input_scheme != "file")
88
+ return input_scheme + "://" + output_base_path + target_file.to_s
49
89
  else
50
- return base_path + target_file.to_s
90
+ return input_base_path + target_file.to_s
51
91
  end
52
92
  end
53
93
 
54
- def make_pdf_filename(file, **options)
55
- path_of(file).basename.sub_ext(".pdf")
94
+ def relative_path(file, input_base_path:, **options)
95
+ base_path = Pathname.new(input_base_path)
96
+ result = Pathname.new(file).relative_path_from(base_path).dirname
97
+ @logger.verbose("relative_path(#{file}, #{input_base_path}) → base_path: #{base_path} ⇒ #{result}")
98
+ result
56
99
  end
57
100
 
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}")
101
+ def make_pdf_filename(file, input_base_path:, **options)
102
+ base_path = relative_path(file, input_base_path:, **options)
103
+ filename = Pathname.new(file).basename.sub_ext(".pdf")
104
+ result = base_path / filename
105
+ @logger.verbose("make_pdf_filename(#{file}, #{input_base_path}) → base_path: #{base_path}, filename: #{filename} ⇒ #{result}")
106
+ result
107
+ end
108
+
109
+ def make_output_filename(file, input_base_path:, output_base_path:, output_dir: ".", **options)
110
+ @logger.verbose("make_output_filename(#{file}, #{input_base_path}, #{output_base_path})")
111
+ filename = make_pdf_filename(file, input_base_path:, **options)
112
+ output_base_path = Pathname.new(output_base_path)
113
+ output = output_base_path / Pathname.new(output_dir) / filename
114
+ FileUtils::mkdir_p(output.dirname)
115
+ @logger.debug("filename: #{filename} ⇒ #{output}")
62
116
  output
63
117
  end
64
118
 
@@ -73,7 +127,6 @@ module MakePDF
73
127
  output_filename
74
128
  end
75
129
  end
76
-
77
130
  end
78
131
 
79
132
  Dir[File.join(__dir__, 'make_pdf/', '**', '*.rb')].each do |file|
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_pdf-jekyll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Bogado da Silva Lins
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2025-08-27 00:00:00.000000000 Z
11
12
  dependencies: []
12
13
  description: Allows that some documents, or pages to have a pdf version pre generated.
13
14
  email: 'victor@bogado.net '
@@ -24,6 +25,7 @@ homepage: https://rubygems.org/gems/make_pdf-jekyll
24
25
  licenses:
25
26
  - MIT
26
27
  metadata: {}
28
+ post_install_message:
27
29
  rdoc_options: []
28
30
  require_paths:
29
31
  - lib
@@ -38,7 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
40
  - !ruby/object:Gem::Version
39
41
  version: '0'
40
42
  requirements: []
41
- rubygems_version: 3.6.7
43
+ rubygems_version: 3.3.25
44
+ signing_key:
42
45
  specification_version: 4
43
46
  summary: Create PDF along side of HTML files for site.
44
47
  test_files: []