Class: Ebookie::Rendering::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ebookie/rendering/base.rb

Direct Known Subclasses

Epub, Mobi, Pdf

Constant Summary collapse

IMAGE_SRC_REGEX =
/src=['|"]((\/?.+\/)*)?(.+?[\.]+.+?)['|"]/xi

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
# File 'lib/ebookie/rendering/base.rb', line 12

def initialize(document)
  @document = document

  after_initialize if respond_to?(:after_initialize)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/ebookie/rendering/base.rb', line 10

def document
  @document
end

Class Method Details

.formatObject



38
39
40
# File 'lib/ebookie/rendering/base.rb', line 38

def format
  self.name.split("::").last.downcase
end

.inherited(subclass) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# 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

.set(key, val) ⇒ Object



31
32
33
34
35
36
# 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

Instance Method Details

#clean_images(html, new_path) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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

#formatObject



85
86
87
# File 'lib/ebookie/rendering/base.rb', line 85

def format
  self.class.format
end

#output_pathObject



89
90
91
# File 'lib/ebookie/rendering/base.rb', line 89

def output_path
  Pathname.new(document.destination).join("#{document.config.slug}.#{format}").to_s
end

#renderObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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 = {}) ⇒ Object



68
69
70
71
72
73
74
75
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ebookie/rendering/base.rb', line 93

def sanitize_html(html)
  {
    /’|‘/ => "'",
    /”|“|“|”/ => "\"",
    "’"               => "'",
    ":"           => ":",
    "⌘"              => "⌘"
  }.each do |k,v|
    html.gsub! k, v
  end

  html
end

#template_dirObject



81
82
83
# File 'lib/ebookie/rendering/base.rb', line 81

def template_dir
  Pathname.new(File.expand_path("../../templates/#{format}", __FILE__))
end

#template_file(path) ⇒ Object



58
59
60
61
62
63
64
65
66
# 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_dirObject



77
78
79
# File 'lib/ebookie/rendering/base.rb', line 77

def tmp_dir
  Pathname.new(File.expand_path("../../../../tmp/#{document.config.slug}/#{format}", __FILE__))
end