Class: MakePDF::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/make_pdf/jekyll.rb

Overview

MakePDF Jekyll plugin

Defined Under Namespace

Classes: Site

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, current_doc, **options) ⇒ Processor

Returns a new instance of Processor.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/make_pdf/jekyll.rb', line 40

def initialize(site, current_doc, **options)
  @site    = site
  @file    = current_doc.destination(@base_source)
  @options = filter_options(current_doc, **options)
  @name    = current_doc.name

  logger.debug("base_paths: input → #{@options[:input_base_url]} output → #{@options[:output_base_path]} host → #{@options[:input_host]}")

  current_options = make_options(@options, options, filter_options(current_doc))

  logger.debug("options : #{current_options}")

  return if check_failure(current_options[:disabled], "MakePDF disabled")

  return if check_failure(File.extname(@file) != '.html', "#{@file} is not an html")

  return if check_failure(current_options[:make_pdf].nil? && !@opt_in, "#{current_doc.name} has not opted in")

  return if check_failure(current_options[:make_pdf] == false, "#{current_doc.name} has opted out")

  writer = current_options[:writer] || site.options[:writer]
  return if check_failure(writer.nil?, "No writer defined for #{current_doc.name} (#{writer})")

  @writer = MakePDF.const_get(writer.capitalize).new(logger:, **current_options)
  @doc = current_doc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/make_pdf/jekyll.rb', line 75

def method_missing(method_name, *args, **options)
  if @options.include?(method_name)
    return @options[method_name]
  elsif not @site.options.nil? and @site.options.include?(method_name)
    return @site.options[method_name]
  elsif @site.respond_to?(method_name, false)
    return @site.send(method_name, *args, **options)
  else
    super
  end
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



10
11
12
# File 'lib/make_pdf/jekyll.rb', line 10

def doc
  @doc
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/make_pdf/jekyll.rb', line 10

def name
  @name
end

#reasonObject (readonly)

Returns the value of attribute reason.



10
11
12
# File 'lib/make_pdf/jekyll.rb', line 10

def reason
  @reason
end

Instance Method Details

#check_failure(condition, message) ⇒ Object



16
17
18
19
# File 'lib/make_pdf/jekyll.rb', line 16

def check_failure(condition, message)
  @reason = message if condition
  condition
end

#filter_options(document, **options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/make_pdf/jekyll.rb', line 21

def filter_options(document, **options)
  document.data.filter_map do |key, value|
    key = key.to_s
    if key == "make-pdf"
      possible = {
        "" => true,
        "true" => true,
        "yes" => true,
        "false" => false,
        "no" => false
      }

      [ key, possible[value.to_s.downcase] ]
    else
      [ key.sub("make-pdf-", "").to_sym, value ] if key.start_with?("make-pdf-")
    end
  end.to_h.merge(options)
end

#output_dirObject



67
68
69
# File 'lib/make_pdf/jekyll.rb', line 67

def output_dir
  @writer.output_dir
end

#process(**options) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/make_pdf/jekyll.rb', line 107

def process(**options)
  render_option(**options)
  unless self.targets.nil?
    self.targets.split(",").each do |option|
      render_option(version: option.split(","), **options)
    end
  end
end

#render_option(**options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/make_pdf/jekyll.rb', line 87

def render_option(**options)
  logger.debug("MakePDF rendering options #{options}")

  attempted = 0
  begin
    logger.info("processing #{@file}")
    @writer.process(@file, **options.merge(@options))
  rescue => error
    attempted += 1
    if attempted <= 2
      logger.warn("Failed to generate #{@file} retrying #{attempted}")
      logger.warn("ERROR: #{error}")
      retry
    else
      logger.warn("Skipping generation of #{@file} with #{options}")
      raise error
    end
  end
end

#targetsObject



71
72
73
# File 'lib/make_pdf/jekyll.rb', line 71

def targets
  @options[:targets] || ""
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/make_pdf/jekyll.rb', line 12

def valid?
  @reason.nil?
end