class Verku::Exporter::Epub

Public Instance Methods

assets() click to toggle source
# File lib/verku/exporter/epub.rb, line 136
def assets
  @assets ||= begin
    assets = Dir[root_dir.join("_templates/epub/*.css")]
    assets += Dir[root_dir.join("images/**/*.{jpg,png,gif}")]
    assets
  end
end
back_page() click to toggle source
# File lib/verku/exporter/epub.rb, line 46
def back_page
  return Dir[back_path] if File.exist?(back_path)
  []
end
back_path() click to toggle source
# File lib/verku/exporter/epub.rb, line 166
def back_path
  tmp_dir.join("back.html")
end
cover_image() click to toggle source
# File lib/verku/exporter/epub.rb, line 143
def cover_image
  path = Dir[root_dir.join("images/cover-#{name}.{jpg,png,gif}").to_s.downcase].first
  return File.basename(path) if path && File.exist?(path)
end
cover_page() click to toggle source
# File lib/verku/exporter/epub.rb, line 50
def cover_page
  Dir[cover_path]
end
cover_path() click to toggle source
# File lib/verku/exporter/epub.rb, line 160
def cover_path
  tmp_dir.join("cover.html")
end
epub() click to toggle source
# File lib/verku/exporter/epub.rb, line 15
def epub; @epub ||= EeePub.make ;end
export!() click to toggle source
# File lib/verku/exporter/epub.rb, line 17
def export!
  puts "-- Exporting EPUB"
  epub.title        config["title"]
  epub.language     config["language"]
  epub.creator      config["authors"].to_sentence
  epub.publisher    config["publisher"]
  epub.date         config["published_at"]
  epub.uid          config["uid"]
  epub.identifier   config["identifier"]["id"], :scheme => config["identifier"]["type"]
  if cover_image.nil?
    puts "   - Consider adding a cover images in /images."
  else
    epub.cover      cover_image
  end
  write_coverpage!
  write_thankspage!
  write_copyright!
  write_sections!
  write_backpage!
  write_toc!
  epub.files    cover_page + thanks_page + sections.map(&:filepath) + back_page + copyright_page + assets
  epub.nav      navigation

  epub.save(epub_file)
  true
rescue Exception
  p $!, $@
  false
end
html() click to toggle source
# File lib/verku/exporter/epub.rb, line 16
def html; @html ||= Nokogiri::HTML(html_file.read); end
navigation() click to toggle source
render_chapter(content) click to toggle source
# File lib/verku/exporter/epub.rb, line 132
def render_chapter(content)
  locals = config.merge(:content => content)
  render_template(template_path, locals)
end
sections() click to toggle source
# File lib/verku/exporter/epub.rb, line 5
def sections
  @sections ||= html.css("div.section").each_with_index.map do |section, index|
    OpenStruct.new({
      :index    => index,
      :filename => "section_#{index}.html",
      :filepath => tmp_dir.join("section_#{index}.html").to_s,
      :html     => Nokogiri::HTML(section.inner_html)
    })
  end
end
template_path() click to toggle source
# File lib/verku/exporter/epub.rb, line 154
def template_path
  root_dir.join("_templates/epub/page.erb")
end
thanks_page() click to toggle source
# File lib/verku/exporter/epub.rb, line 56
def thanks_page
  Dir[thanks_path]
end
thanks_path() click to toggle source
# File lib/verku/exporter/epub.rb, line 163
def thanks_path
  tmp_dir.join("thanks.html")
end
tmp_dir() click to toggle source
# File lib/verku/exporter/epub.rb, line 157
def tmp_dir
  root_dir.join("builds/tmp")
end
toc_path() click to toggle source
# File lib/verku/exporter/epub.rb, line 172
def toc_path
  tmp_dir.join("toc.html")
end
write_backpage!() click to toggle source
# File lib/verku/exporter/epub.rb, line 59
def write_backpage!
  contents = render_template(root_dir.join("_templates/epub/back.html"), config)
  File.open(back_path,"w") do |file|
    file << contents
  end
end
write_coverpage!() click to toggle source
# File lib/verku/exporter/epub.rb, line 65
def write_coverpage!
  contents = render_template(root_dir.join("_templates/epub/cover.html"), config)
  puts "Writing cover page. #{cover_path}"
  FileUtils.mkdir_p(File.dirname(cover_path))
  File.open(cover_path,"w") do |file|
    file << contents
  end
end
write_sections!() click to toggle source
# File lib/verku/exporter/epub.rb, line 95
def write_sections!
  # First we need to get all ids, which are used as
  # the anchor target.
  links = sections.inject({}) do |buffer, section|
    section.html.css("[id]").each do |element|
      anchor = "##{element["id"]}"
      buffer[anchor] = "#{section.filename}#{anchor}"
    end

    buffer
  end

  # Then we can normalize all links and
  # manipulate other paths.
  #
  sections.each do |section|
    section.html.css("a[href^='#']").each do |link|
      href = link["href"]
      link.set_attribute("href", links.fetch(href, href))
    end

    # Replace all srcs.
    #
    section.html.css("[src]").each do |element|
      src = File.basename(element["src"]).gsub(/\.svg$/, ".png")
      element.set_attribute("src", src)
      element.set_attribute("alt", "")
      element.node_name = "img"
    end

    FileUtils.mkdir_p(tmp_dir)
    File.open(section.filepath, "w") do |file|
      body = section.html.css("body").to_xhtml.gsub(%r[<body>(.*?)</body>]m, "\\1")
      file << render_chapter(body)
    end
  end
end
write_thankspage!() click to toggle source
# File lib/verku/exporter/epub.rb, line 80
def write_thankspage!
  contents = render_template(root_dir.join("_templates/html/thanks.erb"), config)
  FileUtils.mkdir_p(File.dirname(thanks_path))
  File.open(thanks_path,"w") do |file|
    file << contents
  end
end
write_toc!() click to toggle source
# File lib/verku/exporter/epub.rb, line 87
def write_toc!
  toc = TOC::Epub.new(navigation)
  FileUtils.mkdir_p(File.dirname(toc_path))
  File.open(toc_path, "w") do |file|
    file << toc.to_html
  end
end