class Ebookie::Rendering::Base

Constants

IMAGE_SRC_REGEX

Attributes

document[R]

Public Class Methods

format() click to toggle source
# File lib/ebookie/rendering/base.rb, line 38
def format
  self.name.split("::").last.downcase
end
inherited(subclass) click to toggle source
# File lib/ebookie/rendering/base.rb, line 19
def inherited(subclass)
  subclass.class_eval do
    attr_reader :settings
  end

  subclass.instance_eval do
    define_method :settings do
      @@settings.send(format) || {}
    end
  end
end
new(document) click to toggle source
# File lib/ebookie/rendering/base.rb, line 12
def initialize(document)
  @document = document

  after_initialize if respond_to?(:after_initialize)
end
set(key, val) click to toggle source
# File lib/ebookie/rendering/base.rb, line 31
def set(key, val)
  @@settings ||= OpenStruct.new

  format_options = @@settings.send(format) || {}
  @@settings.send "#{format}=", format_options.merge({key => val})
end

Public Instance Methods

clean_images(html, new_path) click to toggle source
# File lib/ebookie/rendering/base.rb, line 107
def clean_images(html, new_path)
  html.each_line do |line|
    old_line = line.dup
    matches = line.match(IMAGE_SRC_REGEX).to_a
    next unless matches.any?

    # remove folder
    line.gsub! matches[2], "" if matches[2]

    # set our folder
    line.gsub! matches[3], new_path.join(matches[3]).to_s

    html.gsub! old_line, line
  end
end
format() click to toggle source
# File lib/ebookie/rendering/base.rb, line 85
def format
  self.class.format
end
output_path() click to toggle source
# File lib/ebookie/rendering/base.rb, line 89
def output_path
  Pathname.new(document.destination).join("#{document.config.slug}.#{format}").to_s
end
render() click to toggle source
# File lib/ebookie/rendering/base.rb, line 43
def render
  throw "Output path required" unless document.destination

  FileUtils.mkdir_p(tmp_dir) unless File.exists?(tmp_dir)

  create_paths if settings.keys.include?(:paths) && settings[:paths]
  copy_files if settings.keys.include?(:files) && settings[:files]
  copy_images if document.images.any? && settings[:images_dir]

  FileUtils.mkdir_p(document.destination) unless File.exists?(document.destination)

  process!
  return output_path
end
render_erb_to_file(template, filepath, locals={}) click to toggle source
# File lib/ebookie/rendering/base.rb, line 68
def render_erb_to_file(template, filepath, locals={})
  locals.merge! document: document, renderer: self

  locals_struct = OpenStruct.new(locals).instance_eval { binding }
  contents = ERB.new(File.read(template)).result(locals_struct)

  write_contents_to_file(contents, filepath)
end
sanitize_html(html) click to toggle source
# File lib/ebookie/rendering/base.rb, line 93
def sanitize_html(html)
  {
    /’|‘/ => "'",
    /”|“|“|”/ => "\"",
    "’"               => "'",
    ":"           => ":",
    "⌘"              => "⌘"
  }.each do |k,v|
    html.gsub! k, v
  end

  html
end
template_dir() click to toggle source
# File lib/ebookie/rendering/base.rb, line 81
def template_dir
  Pathname.new(File.expand_path("../../templates/#{format}", __FILE__))
end
template_file(path) click to toggle source
# File lib/ebookie/rendering/base.rb, line 58
def template_file(path)
  custom_template_dir = Pathname.new(document.template) if document.template

  if document.template && File.exists?(custom_template_dir.join(format, path))
    custom_template_dir.join(format, path)
  else
    template_dir.join(path)
  end
end
tmp_dir() click to toggle source
# File lib/ebookie/rendering/base.rb, line 77
def tmp_dir
  Pathname.new(File.expand_path("../../../../tmp/#{document.config.slug}/#{format}", __FILE__))
end

Private Instance Methods

copy_files() click to toggle source
# File lib/ebookie/rendering/base.rb, line 137
def copy_files
  settings[:files].each do |file|
    if File.extname(file) == '.erb' && ext = File.extname(file)
      render_erb_to_file template_file(file), tmp_dir.join(file.gsub(ext, ''))
    else
      FileUtils.cp template_file(file), tmp_dir.join(file)
    end
  end
end
copy_images() click to toggle source
# File lib/ebookie/rendering/base.rb, line 131
def copy_images
  document.images.each do |image|
    borrow image.file.to_s, to: tmp_dir.join(settings[:images_dir], image.basename)
  end
end
create_paths() click to toggle source
# File lib/ebookie/rendering/base.rb, line 125
def create_paths
  settings[:paths].each do |path|
    FileUtils.mkdir_p tmp_dir.join(path)
  end
end
write_contents_to_file(contents, filepath, mode="w+") click to toggle source
# File lib/ebookie/rendering/base.rb, line 147
def write_contents_to_file(contents, filepath, mode="w+")
  File.open(filepath, mode) do |handle|
    handle.write(contents)
    handle.close
  end
end