make_pdf-jekyll 0.0.4 → 0.0.6

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: e5a21bc7883012692bab3b448615f5a40c58a37566c23d678e67f7f4cad520de
4
- data.tar.gz: 905cf312025602478eebabd00a45477603f40e2785261297e5312321bf3c1d90
3
+ metadata.gz: 68f4407fd68b80e7a2f87419480396c0eb7b543ddf86e5bcd8a8b4c60f9d71a6
4
+ data.tar.gz: 85e8793030dc76e217a4f1882d3befd3fc4a7d49ed01efe888a6789198d6730f
5
5
  SHA512:
6
- metadata.gz: 80b67fdfb51e5b34d3eebac122a4d6793d543c80c27ff9b416d63cee957928c229605a4ec4f81dd71099f0c403bfa74c4a5112ba93c1ded4cb0e4f7a1549e4a0
7
- data.tar.gz: b30b0279151ba01a329c5f0e1d99c3ddc5e2677ba85704065c0fe187c080ad327cc5661bbd3014c6a94970b97df34aa527ce2525043fb87af009033c119a64d8
6
+ metadata.gz: 0f38ee9054a2ae250228ebac98ef1a97fe7e13fadeee5dba66aa1b9afb302e0f4e9cc4a9a9d35e21e28dded6af688c66362793add3fd488b877fa27a316f3a52
7
+ data.tar.gz: fc8c51cd0bb88aacf8ba5dfce138eeca83e71fed79ea80c60ea02e37eac7f71d1393d7c4cbd5ebece01dcc1bb8990983761c2da391ed3b932fe07f7fa9dcac14
@@ -12,14 +12,14 @@ module MakePDF
12
12
  end
13
13
 
14
14
  def initialize(options)
15
- Jekyll.logger.info('MakePDF firefox:', options)
15
+ Processor.logger.info('MakePDF firefox:', options)
16
16
  @args = make_arguments(**options)
17
17
  @driver_opts = Selenium::WebDriver::Firefox::Options.new(args: @opts)
18
18
  setup
19
19
  end
20
20
 
21
21
  def setup
22
- Jekyll.logger.info('MakePDF firefox: start driver')
22
+ Processor.logger.info('MakePDF firefox: start driver')
23
23
  @driver = Selenium::WebDriver.for :firefox, capabilities: @driver_opts
24
24
  end
25
25
 
@@ -1,12 +1,13 @@
1
1
  require 'jekyll'
2
2
  require 'make_pdf'
3
+ require 'path_of'
3
4
 
4
5
  module MakePDF
5
6
  LOG_NAME = "make_pdf:"
6
7
 
7
8
  # MakePDF Jekyll plugin
8
- class Jekyll
9
- attr_reader :reason
9
+ class Processor
10
+ attr_reader :reason, :doc, :name
10
11
 
11
12
  def valid?
12
13
  @reason.nil?
@@ -29,32 +30,42 @@ module MakePDF
29
30
  "no" => false
30
31
  }
31
32
 
32
- [ key, possible[value.downcase] ]
33
+ [ key, possible[value.to_s.downcase] ]
33
34
  else
34
35
  [ key.sub("make-pdf-", "").to_sym, value ] if key.start_with?("make-pdf-")
35
36
  end
36
37
  end.to_h.merge(options)
37
38
  end
38
39
 
39
- def initialize(current_doc, **options)
40
- @file = current_doc.destination(@base_source)
40
+ def initialize(site, current_doc, **options)
41
+ @site = site
42
+ @file = current_doc.destination(@base_source)
41
43
  @options = filter_options(current_doc, **options)
42
- logger.debug("base_paths: input → #{@options[:input_base_path]} output → #{@options[:output_base_path]}")
44
+ @name = current_doc.name
43
45
 
44
- current_options = make_options(@options, site_options, filter_options(current_doc))
45
- output_dir = @options[:output_dir] || path_of(site.dest).dirname
46
+ logger.debug("base_paths: input → #{@options[:input_base_url]} output → #{@options[:output_base_path]} host → #{@options[:input_host]}")
47
+
48
+ current_options = make_options(@options, options, filter_options(current_doc))
46
49
 
47
50
  logger.debug("options : #{current_options}")
48
51
 
52
+ return if check_failure(current_options[:disabled], "MakePDF disabled")
53
+
49
54
  return if check_failure(File.extname(@file) != '.html', "#{@file} is not an html")
55
+
50
56
  return if check_failure(current_options[:make_pdf].nil? && !@opt_in, "#{current_doc.name} has not opted in")
57
+
51
58
  return if check_failure(current_options[:make_pdf] == false, "#{current_doc.name} has opted out")
52
59
 
53
- writer = current_options[:writer]
60
+ writer = current_options[:writer] || site.options[:writer]
54
61
  return if check_failure(writer.nil?, "No writer defined for #{current_doc.name} (#{writer})")
55
62
 
56
- logger.info(" processing #{current_doc.name}")
57
63
  @writer = MakePDF.const_get(writer.capitalize).new(logger:, **current_options)
64
+ @doc = current_doc
65
+ end
66
+
67
+ def output_dir
68
+ @writer.output_dir
58
69
  end
59
70
 
60
71
  def targets
@@ -62,10 +73,12 @@ module MakePDF
62
73
  end
63
74
 
64
75
  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)
76
+ if @options.include?(method_name)
77
+ return @options[method_name]
78
+ elsif not @site.options.nil? and @site.options.include?(method_name)
79
+ return @site.options[method_name]
80
+ elsif @site.respond_to?(method_name, false)
81
+ return @site.send(method_name, *args, **options)
69
82
  else
70
83
  super
71
84
  end
@@ -76,6 +89,7 @@ module MakePDF
76
89
 
77
90
  attempted = 0
78
91
  begin
92
+ logger.info("processing #{@file}")
79
93
  @writer.process(@file, **options.merge(@options))
80
94
  rescue => error
81
95
  attempted += 1
@@ -99,9 +113,19 @@ module MakePDF
99
113
  end
100
114
  end
101
115
 
102
- class << self
116
+ class Site
103
117
  include PathManip
104
- attr_reader :site_options, :site
118
+ attr_reader :options, :site, :logger
119
+
120
+ def default_options
121
+ return {
122
+ output_base_path: site.source,
123
+ input_location: path_of(site.dest),
124
+ input_base_url: relative_path_of(site.baseurl[1..]),
125
+ input_host: site.config["url"].match(Regexp.new("^[^:]*://\([^/]+\)/?.*$"))[1],
126
+ input_scheme: "file"
127
+ }.freeze
128
+ end
105
129
 
106
130
  def make_options(options, *more_options)
107
131
  return {} if options.nil?
@@ -113,41 +137,49 @@ module MakePDF
113
137
  end
114
138
  end
115
139
 
116
- def logger(**options)
117
- @logger ||= MakePDF::Logger.new(**options)
118
- end
119
-
120
- def setup(site, **options)
121
- return unless @site.nil?
122
-
140
+ def initialize(site, **options)
123
141
  config = site.config["make-pdf"]||{}
124
- logger(logger: ::Jekyll.logger, level: (config["log-map-level"] || :debug).to_sym, verbose: config['log-verbose'])
142
+ @logger = MakePDF::Logger.new(logger: ::Jekyll.logger, level: (config["log-map-level"] || :debug))
125
143
  @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}.")
144
+ @options = default_options.merge(make_options(@site.config["make-pdf"], options))
145
+ @queue = []
146
+ logger.debug("Initialized with #{self.options}.")
132
147
  end
133
148
 
134
- def process(current_doc, **options)
135
- setup(current_doc.site, **options) if @site.nil?
136
-
137
- processor = self.new(current_doc)
149
+ def queue(processor)
150
+ logger.info("Adding #{processor.name} to queue")
151
+ @queue.push(processor)
152
+ end
138
153
 
139
- unless processor.valid?
140
- logger.debug "Ignoring #{current_doc.name} #{processor.reason}"
141
- return false
154
+ def <<(doc)
155
+ processor = Processor.new(self, doc, **@options)
156
+ if processor.valid?
157
+ queue(processor)
158
+ else
159
+ logger.info("Skip #{doc.name} => #{processor.reason}")
142
160
  end
161
+ end
143
162
 
144
- processor.process(**@site_options)
163
+ def process
164
+ @queue.each do |processor|
165
+ processor.process(**@options)
166
+ end
145
167
  end
146
168
  end
147
169
  end
148
- end
149
170
 
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)
171
+ ::Jekyll.logger.info("Loaded #{MakePDF::LOG_NAME} plugin")
172
+
173
+ ::Jekyll::Hooks.register [:site], :after_init do |site|
174
+ @@site = MakePDF::Processor::Site.new(site)
175
+ @@site.logger.info("site :after_init #{@@site}")
176
+ end
177
+
178
+ ::Jekyll::Hooks.register [:pages, :documents, :posts], :post_write do |doc|
179
+ @@site << doc
180
+ end
181
+
182
+ ::Jekyll::Hooks.register [:site], :post_write do |site|
183
+ @@site.process
184
+ end
153
185
  end
data/lib/make_pdf.rb CHANGED
@@ -1,20 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'fileutils'
4
+ require 'path_of'
4
5
 
5
6
  module MakePDF
6
7
  module PathManip
7
- def path_of(base, *path_components)
8
- other = unless path_components.empty?
9
- path_components
10
- .map { |component| path_of(component) }
11
- .sum Pathname.new(".")
12
- else
13
- Pathname.new("")
14
- end
15
- base = Pathname.new(".") if base.nil?
16
- if base.instance_of?(Pathname) then base else Pathname.new(base) end + other
17
- end
18
8
 
19
9
  def relative_path(file, base_path)
20
10
  path_of(file).relative_path_from(path_of(base_path))
@@ -77,40 +67,41 @@ module MakePDF
77
67
  include PathManip
78
68
  attr_reader :output_dir, :source_url, :logger
79
69
 
80
- def initialize(input_base_path:, output_base_path:, input_scheme: "file", logger: Logger.new() ,**options)
70
+ def initialize(input_base_url:, output_base_path:, input_scheme: "file", input_host: nil, logger: Logger.new() ,**options)
81
71
  @logger = logger
82
- @options = options.merge({ input_base_path:, output_base_path:, input_scheme: })
72
+ raise ArgumentError.new("Scheme `#{input_scheme}` requires an `input_host`.") if input_scheme != "file" && input_host.nil?
73
+ @options = options.merge({ input_base_url:, output_base_path:, input_scheme:, input_host: })
74
+
83
75
  end
84
76
 
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)
77
+ def make_relative_file(file, input_location:, **options)
78
+ path_of(file).relative_path_from(path_of(input_location))
79
+ end
80
+
81
+ def make_source_url(file, input_base_url:, output_base_path:, input_scheme: , input_host: , input_location:, **options)
82
+ target_file = make_relative_file(file, input_location:, **options)
83
+ input_location = path_of(input_location)
84
+ input_base_url = relative_path_of(input_base_url)
87
85
  if (input_scheme != "file")
88
- return input_scheme + "://" + output_base_path + target_file.to_s
86
+ return input_scheme + "://" + input_host + "/" + (input_base_url / target_file).to_path
89
87
  else
90
- return input_base_path + target_file.to_s
88
+ return (input_location / input_location / target_file).to_path
91
89
  end
92
90
  end
93
91
 
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
99
- end
100
-
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}")
92
+ def make_pdf_filename(file, output_base_path:, input_location:, output_dir:, **options)
93
+ base_path = path_of(output_base_path)
94
+ filepath = make_relative_file(file, input_location:).sub_ext(".pdf")
95
+ result = base_path / relative_path_of(output_dir) / filepath
96
+ @logger.verbose("make_pdf_filename(#{file}, #{output_base_path}) → base_path: #{base_path}, filepath: #{filepath} ⇒ #{result}")
106
97
  result
107
98
  end
108
99
 
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
100
+ def make_output_filename(file, input_location:, output_base_path:, output_dir: ".", **options)
101
+ @logger.verbose("make_output_filename(#{file}, #{input_location}, #{output_base_path})")
102
+ filename = make_pdf_filename(file, output_base_path:, output_dir:, input_location:, **options)
103
+ output_base_path = path_of(output_base_path)
104
+ output = output_base_path / relative_path_of(output_dir) / filename
114
105
  FileUtils::mkdir_p(output.dirname)
115
106
  @logger.debug("filename: #{filename} ⇒ #{output}")
116
107
  output
@@ -130,6 +121,5 @@ module MakePDF
130
121
  end
131
122
 
132
123
  Dir[File.join(__dir__, 'make_pdf/', '**', '*.rb')].each do |file|
133
- print "#{file}\n"
134
124
  require file
135
125
  end
metadata CHANGED
@@ -1,14 +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.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Bogado da Silva Lins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-08-27 00:00:00.000000000 Z
11
+ date: 2025-09-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Allows that some documents, or pages to have a pdf version pre generated.
14
14
  email: 'victor@bogado.net '