Class: Jekyll::PDF::Document

Inherits:
Jekyll::Page
  • Object
show all
Includes:
Helper
Defined in:
lib/jekyll/pdf/document.rb

Instance Method Summary collapse

Methods included from Helper

#fix_relative_paths

Constructor Details

#initialize(site, base, page) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll/pdf/document.rb', line 9

def initialize(site, base, page)
  @site = site
  @base = base
  @dir = File.dirname(page.url)
  @name = File.basename(page.url, File.extname(page.url)) + '.pdf'
  @settings = site.config.key?('pdf') ? site.config['pdf'].clone : {}
  @partials = ['cover','header_html','footer_html']

  self.process(@name)
  self.data = page.data.clone
  self.content = page.content.clone

  # Set layout to the PDF layout
  self.data['layout'] = layout

  # Get PDF settings from the layouts
  Jekyll::Utils.deep_merge_hashes!(@settings, self.getConfig(self.data))

  PDFKit.configure do |config|
    config.verbose = site.config['verbose']
  end

  # Set pdf_url variable in the source page (for linking to the PDF version)
  page.data['pdf_url'] = self.url

  # Set html_url variable in the source page (for linking to the HTML version)
  self.data['html_url'] = page.url

  # create the partial objects
  @partials.each do |partial|
    @settings[partial] = Jekyll::PDF::Partial.new(self, @settings[partial]) if @settings[partial] != nil
  end
end

Instance Method Details

#getConfig(data) ⇒ Object

Recursively merge settings from the page, layout, site config & jekyll-pdf defaults



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll/pdf/document.rb', line 44

def getConfig(data)
  settings = data['pdf'].is_a?(Hash) ? data['pdf'] : {}
  layout = @site.layouts[data['layout']].data.clone if data['layout'] != nil

  # No parent layout found - return settings hash
  return settings if layout == nil

  # Merge settings with parent layout settings
  layout['pdf'] ||= {}
  Jekyll::Utils.deep_merge_hashes!(layout['pdf'], settings)

  return self.getConfig(layout)
end

#layoutObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll/pdf/document.rb', line 84

def layout()
  # Set page layout to the PDF layout
  layout = self.data['pdf_layout'] || @settings['layout']

  # Check if a PDF version exists for the current layout (e.g. layout_pdf)
  if layout == nil && self.data['layout'] != nil && File.exist?("_layouts/" + self.data['layout'] + "_pdf.html")
    layout = self.data['layout'] + "_pdf"
  end

  layout || 'pdf'
end

#write(dest_prefix, dest_suffix = nil) ⇒ Object

Write the PDF file todo: remove pdfkit dependency



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll/pdf/document.rb', line 60

def write(dest_prefix, dest_suffix = nil)
  self.render(@site.layouts, @site.site_payload) if self.output == nil

  path = File.join(dest_prefix, CGI.unescape(self.url))
  dest = File.dirname(path)

  # Create directory
  FileUtils.mkdir_p(dest) unless File.exist?(dest)

  # write partials
  @partials.each do |partial|
    @settings[partial].write if @settings[partial] != nil
  end

  # Debugging - create html version of PDF
  File.open("#{path}.html", 'w') {|f| f.write(self.output) } if @settings["debug"]
  @settings.delete("debug")

  # Build PDF file
  fix_relative_paths
  kit = PDFKit.new(self.output, @settings)
  file = kit.to_file(path)
end