make_pdf-jekyll 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97065b7cf75979dc847f0d82e96bf54229ea7dc4329eab0216a899ac6c72496a
4
+ data.tar.gz: cc8b2e3a7a25fb598079829a4198679214ac9a9e672a20c7e4fd1ea71f8cbf0e
5
+ SHA512:
6
+ metadata.gz: 5926bb93054a88d248eef00b4c5aa06d00768f3c7ced9d1b79fdcb713de3e732922406f4d0b365cc933d956a912992307e18ee0ac1cbc6fc612f1feb1bf3033a
7
+ data.tar.gz: 18ecbb2c38aabde9c0994fd6a4fb295a23d7b0cf1ef7c3cb7eabc5c508e0e587f7fc3484955f6a5248b0a24c0c8ca332295947ac263fd4803d7c9d3c3675fdc8
@@ -0,0 +1,11 @@
1
+ require "make_pdf/command_based"
2
+
3
+ module MakePDF
4
+ class Chrome < CommandBased::Writer
5
+ COMMAND = 'chrome-headless-render-pdf'
6
+
7
+ def initialize(source_url, output_dif, **options)
8
+ super(source_url, output_dif, command: COMMAND, **options)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+ require 'make_pdf'
4
+
5
+ module MakePDF
6
+ module CommandBased
7
+ module Arguments
8
+ attr_reader :base
9
+
10
+ def make_arguments(options = [], **more)
11
+ options.concat(more.map { |key, value| "#{self.class.prefix}#{key}#{"=#{value}" unless value == true || value.nil?}" })
12
+ end
13
+ end
14
+
15
+ class Writer < PDFWriter
16
+ attr_reader :command, :options
17
+ include Arguments
18
+
19
+ def self.prefix
20
+ '--'
21
+ end
22
+
23
+ def initialize(url, output_dir, command: COMMAND, **options)
24
+ super(url, output_dir, **options)
25
+ @command = command
26
+ @options = options
27
+ end
28
+
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)
34
+ arguments = make_arguments(
35
+ command: @command,
36
+ url:,
37
+ pdf: output
38
+ )
39
+ IO.popen([@command] + arguments, {:err =>[ :child, :out]}) do |pipe|
40
+ std_out = pipe.read
41
+ end
42
+
43
+ raise std_out if ($? != 0)
44
+ logger.info("pdf-writer (#{command}): Wrote #{output}")
45
+ end
46
+
47
+ def output_for(file, base_path:, version: [], **options)
48
+ if @output.nil?
49
+ path = File.dirname(file)
50
+ else
51
+ path = File.expand_path(relative_path(file, base_path), @output)
52
+ end
53
+
54
+ FileUtils.mkdir_p path
55
+
56
+ suffix = if version.empty? then "" else "_#{version.join("_")}" end
57
+ File.expand_path(File.basename(file, ".html") + "#{suffix}.pdf", path)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,31 @@
1
+ require "make_pdf/command_based"
2
+
3
+ # TODO: In progress.
4
+ # Firefox css media don't work well anyway.
5
+ #
6
+ module MakePDF
7
+ class FirefoxPDFWriter < PDFWriter
8
+ include CommandBased::Arguments
9
+
10
+ def self.prefix
11
+ '-'
12
+ end
13
+
14
+ def initialize(options)
15
+ Jekyll.logger.info('MakePDF firefox:', options)
16
+ @args = make_arguments(**options)
17
+ @driver_opts = Selenium::WebDriver::Firefox::Options.new(args: @opts)
18
+ setup
19
+ end
20
+
21
+ def setup
22
+ Jekyll.logger.info('MakePDF firefox: start driver')
23
+ @driver = Selenium::WebDriver.for :firefox, capabilities: @driver_opts
24
+ end
25
+
26
+ def write(url, output)
27
+ @driver.get(url)
28
+ @driver.save_print_page output
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,88 @@
1
+ require 'jekyll'
2
+ require 'make_pdf'
3
+
4
+ module MakePDF
5
+
6
+ # MakePDF Jekyll plugin
7
+ class Jekyll
8
+
9
+ class << self
10
+ include PathManip
11
+ attr_reader :file, :site, :current_doc, :options, :command, :output
12
+
13
+ def setup(current_doc)
14
+ if @site.nil?
15
+ @site = current_doc.site
16
+ @options = @site.config['make-pdf'] || {}
17
+ @opt_in = @options['write-by-default'] || false
18
+ @base_url = @options['source'] || "file:/"
19
+ @base_source = @site.dest
20
+
21
+ ::Jekyll.logger.debug("make_pdf:", "Initialized with #{@options}. #{@base_source}")
22
+ end
23
+
24
+ current_options = @options.merge(current_doc.data)
25
+ ::Jekyll::logger.debug("make-pdf", current_options)
26
+ bail = lambda do |error|
27
+ ::Jekyll.logger.debug("make_pdf:", error)
28
+ false
29
+ end
30
+
31
+ writer = current_options['writer']
32
+ return bail.call("No writer defined for #{current_doc.name} (#{writer})") if writer.nil?
33
+
34
+ @writer = MakePDF.const_get(current_options['writer'].capitalize)
35
+
36
+ file = current_doc.destination(@base_source)
37
+ output_dir = @options['output_dir'] || path_of(file).dirname
38
+
39
+ return bail.call("#{file} is not an html") if File.extname(file) != '.html'
40
+ return bail.call("#{current_doc.name} has not opted in") if current_doc.data['make-pdf'].nil? && !@opt_in
41
+ return bail.call("#{current_doc.name} has opted out")if current_doc.data['make-pdf'] == false
42
+
43
+ ::Jekyll.logger.info("make_pdf:", " processing #{current_doc.name}")
44
+
45
+ @writer.new(file, output, logger: ::Jekyll.logger, **current_options)
46
+ end
47
+
48
+ def process(current_doc)
49
+ writer = setup(current_doc)
50
+
51
+ ::Jekyll::logger.debug("make_pdf:", " Ignoring #{current_doc.destination("")}")
52
+ return if writer === false
53
+
54
+ options = current_doc.data['targets']&.split(';') || []
55
+ file = current_doc.destination(@site.dest)
56
+
57
+ render_option(writer, file, base_path: @site.dest, base_url: @base_url)
58
+ options.each { |option| render_option(writer, file, base_path: @site.dest, base_url: @base_url, version: option.split(",")) }
59
+ end
60
+
61
+ def render_option(writer, file, **options)
62
+ ::Jekyll.logger.debug('MakePDF options:', options)
63
+
64
+ raise "File #{file} is not accessible" unless File.readable?(file)
65
+
66
+ attempted = 0
67
+ begin
68
+ writer.write(file, **options)
69
+ rescue => error
70
+ attempted += 1
71
+ if attempted <= 2
72
+ ::Jekyll.logger.warn("MakePDF: Failed to generate #{file} retrying #{attempted}")
73
+ ::Jekyll.logger.warn("MakePDF: ERROR: #{error}")
74
+ retry
75
+ else
76
+ ::Jekyll.logger.error("MakePDF: Skipping generation of #{file} with #{options}")
77
+ raise error
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ ::Jekyll.logger.info('MakePDF:', "loaded")
85
+ ::Jekyll::Hooks.register [:pages, :documents, :posts], :post_write do |doc|
86
+ MakePDF::Jekyll.process(doc)
87
+ end
88
+ end
data/lib/make_pdf.rb ADDED
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+
5
+ module MakePDF
6
+ 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
+
19
+ def relative_path(file, base_path)
20
+ path_of(file).relative_path_from(path_of(base_path))
21
+ end
22
+ end
23
+
24
+ class Logger
25
+ def debug(*args)
26
+ end
27
+
28
+ alias info debug
29
+ alias warn debug
30
+ alias error debug
31
+ end
32
+
33
+ class PDFWriter
34
+ include PathManip
35
+ attr_reader :output_dir, :source_url, :logger
36
+
37
+ def initialize(source_url, output_dir, logger: Logger.new() ,**options)
38
+ @source_url = source_url
39
+ @output_dir = path_of(output_dir)
40
+ @logger = logger
41
+ @options = options
42
+ end
43
+
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}"
53
+ end
54
+
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
+ end
59
+
60
+ def process(file, version: [], **options)
61
+ options = @options.merge(options)
62
+ write(
63
+ source_url(file, vesrsion: version, **options),
64
+ output_for(file, version: version, **options),
65
+ **options
66
+ )
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ Dir[File.join(__dir__, 'make_pdf/', '**', '*.rb')].each do |file|
73
+ print "#{file}\n"
74
+ require file
75
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: make_pdf-jekyll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Victor Bogado da Silva Lins
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Allows that some documents, or pages to have a pdf version pre generated.
13
+ email: 'victor@bogado.net '
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - lib/make_pdf.rb
19
+ - lib/make_pdf/chrome.rb
20
+ - lib/make_pdf/command_based.rb
21
+ - lib/make_pdf/firefox.rb
22
+ - lib/make_pdf/jekyll.rb
23
+ homepage: https://rubygems.org/gems/make_pdf-jekyll
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.6.7
42
+ specification_version: 4
43
+ summary: Create PDF along side of HTML files for site.
44
+ test_files: []