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 +4 -4
- data/lib/make_pdf/chrome.rb +7 -7
- data/lib/make_pdf/command_based.rb +17 -12
- data/lib/make_pdf/jekyll.rb +119 -80
- data/lib/make_pdf.rb +75 -22
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5a21bc7883012692bab3b448615f5a40c58a37566c23d678e67f7f4cad520de
|
4
|
+
data.tar.gz: 905cf312025602478eebabd00a45477603f40e2785261297e5312321bf3c1d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80b67fdfb51e5b34d3eebac122a4d6793d543c80c27ff9b416d63cee957928c229605a4ec4f81dd71099f0c403bfa74c4a5112ba93c1ded4cb0e4f7a1549e4a0
|
7
|
+
data.tar.gz: b30b0279151ba01a329c5f0e1d99c3ddc5e2677ba85704065c0fe187c080ad327cc5661bbd3014c6a94970b97df34aa527ce2525043fb87af009033c119a64d8
|
data/lib/make_pdf/chrome.rb
CHANGED
@@ -23,16 +23,16 @@ module MakePDF
|
|
23
23
|
"animation-time-budget": nil,
|
24
24
|
}
|
25
25
|
|
26
|
-
def initialize(
|
27
|
-
super(
|
26
|
+
def initialize(**options)
|
27
|
+
super(command: COMMAND, **options.merge(DEFAULT_OPTIONS))
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def map_option_key(key, value)
|
31
31
|
case key
|
32
|
-
when '
|
33
|
-
[
|
34
|
-
when '
|
35
|
-
[
|
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
|
-
[
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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(
|
28
|
-
super(
|
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,
|
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,
|
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 =
|
57
|
+
path = output_base_path / file.dirname
|
53
58
|
else
|
54
|
-
path = File.expand_path(relative_path(file,
|
59
|
+
path = File.expand_path(relative_path(file, output_base_path), @output_dir)
|
55
60
|
end
|
56
61
|
|
57
62
|
FileUtils.mkdir_p path
|
data/lib/make_pdf/jekyll.rb
CHANGED
@@ -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
|
-
|
11
|
+
def valid?
|
12
|
+
@reason.nil?
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
def check_failure(condition, message)
|
16
|
+
@reason = message if condition
|
17
|
+
condition
|
18
|
+
end
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
106
|
+
def make_options(options, *more_options)
|
107
|
+
return {} if options.nil?
|
69
108
|
|
70
|
-
|
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
|
74
|
-
|
116
|
+
def logger(**options)
|
117
|
+
@logger ||= MakePDF::Logger.new(**options)
|
118
|
+
end
|
75
119
|
|
76
|
-
|
77
|
-
return
|
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
|
-
|
80
|
-
|
134
|
+
def process(current_doc, **options)
|
135
|
+
setup(current_doc.site, **options) if @site.nil?
|
81
136
|
|
82
|
-
|
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
|
-
|
87
|
-
|
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
|
-
|
111
|
-
|
112
|
-
|
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 '
|
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
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
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(
|
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,
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
90
|
+
return input_base_path + target_file.to_s
|
51
91
|
end
|
52
92
|
end
|
53
93
|
|
54
|
-
def
|
55
|
-
|
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
|
59
|
-
|
60
|
-
|
61
|
-
|
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.
|
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:
|
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.
|
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: []
|