class Jekyll::PDF::Partial

Attributes

content[RW]
doc[RW]
ext[RW]
output[W]
partial[RW]
write[RW]

Public Class Methods

new(doc, partial) click to toggle source

Initialize this Partial instance.

doc - The Document.

Returns the new Partial.

# File lib/jekyll/pdf/partial.rb, line 25
def initialize(doc, partial)
  self.doc = doc
  self.partial = partial
  self.content = build_partial(partial)
end

Public Instance Methods

data() click to toggle source

Fetch YAML front-matter data from related doc, without layout key

Returns Hash of doc data

# File lib/jekyll/pdf/partial.rb, line 34
def data
  @data ||= doc.data.dup
  @data.delete("layout")
  @data
end
dir() click to toggle source

Returns the cache directory

# File lib/jekyll/pdf/partial.rb, line 52
def dir
  @dir ||= cache_dir
end
id() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 47
def id
  File.basename(path, File.extname(path)) + "-" + Digest::MD5.hexdigest(to_s) + File.extname(path)
end
inspect() click to toggle source

Returns the shorthand String identifier of this doc.

# File lib/jekyll/pdf/partial.rb, line 68
def inspect
  "<Partial: #{self.id}>"
end
output() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 72
def output
  @output ||= Renderer.new(doc.site, self, site.site_payload).run
end
path() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 43
def path
  File.join(doc.path, partial)
end
place_in_layout?() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 91
def place_in_layout?
  false
end
to_liquid() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 60
def to_liquid
  doc.data[partial] = nil
  @to_liquid ||= doc.to_liquid
  doc.data[partial] = self
  @to_liquid
end
to_s() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 56
def to_s
  output || content
end
trigger_hooks(*) click to toggle source
# File lib/jekyll/pdf/partial.rb, line 40
def trigger_hooks(*)
end

Protected Instance Methods

build_partial(path) click to toggle source

Internal: Generate partial html

Partials are rendered same time as content is rendered.

Returns partial html String

# File lib/jekyll/pdf/partial.rb, line 111
      def build_partial(path)

        # vars to insert into partial
        vars = ['frompage','topage','page','webpage','section','subsection','subsubsection']
        doc.data["pdf"] = {}
        vars.each { |var| doc.data["pdf"][var] = "<span class='__#{var}'></span>" }

        # JavaScript to replace var placeholders with content
        script = "<script>!function(){var t={},n=document.location.search.substring(1).split('&');for(var e in n){var o=n[e].split('=',2);t[o[0]]=decodeURIComponent(o[1])}var n=#{vars};for(var e in n)for(var r=document.getElementsByClassName('__'+n[e]),a=0;a<r.length;++a)r[a].textContent=t[n[e]]}();</script>\n"

        # Parse & render
        content = File.read(File.join("_includes", path))

        # Add replacer script to body
        if content =~ /<\/body>/i
          content[/(<\/body>)/i] = script + content[/(<\/body>)/i]
        else
          Jekyll.logger.warn  <<-eos
  Couldn't find <body> in #{path}. Make sure your partial is a properly formatted HTML document (including DOCTYPE) e.g.

  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    Page {{ pdf.page }} of {{ pdf.topage }}
  </body>
  </html>
  eos
          # No body found - insert html into default template
          content = %{<!DOCTYPE html>
  <html>
    <body>
    #{self.output}
    #{script}
    </body>
  </html>
  }
        end

        content

      end
cache_dir() click to toggle source
# File lib/jekyll/pdf/partial.rb, line 97
def cache_dir
  return site.config["pdf"]["cache"] if site.config["pdf"] != nil && site.config["pdf"].has_key?('cache')

  # Use jekyll-assets cache directory if it exists
  cache_dir = site.config["assets"]["cache"] || '.asset-cache' if site.config["assets"] != nil

  File.join(cache_dir || Dir.tmpdir(), 'pdf')
end