Class: Verku::Exporter::Epub

Inherits:
Base
  • Object
show all
Defined in:
lib/verku/exporter/epub.rb

Constant Summary

Constants inherited from Base

Base::EXTENSIONS

Instance Attribute Summary

Attributes inherited from Base

#root_dir, #source

Instance Method Summary collapse

Methods inherited from Base

#base_name, #build_data, #config, #epub_file, export!, #git_branch, #handle_error, #html_file, #initialize, #name, #output_name, #read_content, #render_template, #source_list, #spawn_command, #tex_file, #ui

Constructor Details

This class inherits a constructor from Verku::Exporter::Base

Instance Method Details

#assetsObject


136
137
138
139
140
141
142
# 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_pageObject


46
47
48
49
# File 'lib/verku/exporter/epub.rb', line 46

def back_page
  return Dir[back_path] if File.exist?(back_path)
  []
end

#back_pathObject


166
167
168
# File 'lib/verku/exporter/epub.rb', line 166

def back_path
  tmp_dir.join("back.html")
end

53
54
55
# File 'lib/verku/exporter/epub.rb', line 53

def copyright_page
  Dir[copyright_path]
end

169
170
171
# File 'lib/verku/exporter/epub.rb', line 169

def copyright_path
  tmp_dir.join("copyright.html")
end

#cover_imageObject


143
144
145
146
# 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_pageObject


50
51
52
# File 'lib/verku/exporter/epub.rb', line 50

def cover_page
  Dir[cover_path]
end

#cover_pathObject


160
161
162
# File 'lib/verku/exporter/epub.rb', line 160

def cover_path
  tmp_dir.join("cover.html")
end

#epubObject


15
# File 'lib/verku/exporter/epub.rb', line 15

def epub; @epub ||= EeePub.make ;end

#export!Object


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
42
43
44
45
# 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 $!, [email protected]
  false
end

#htmlObject


16
# File 'lib/verku/exporter/epub.rb', line 16

def html; @html ||= Nokogiri::HTML(html_file.read); end

147
148
149
150
151
152
153
# File 'lib/verku/exporter/epub.rb', line 147

def navigation
  sections.map do |section| {
      :label => section.html.css("h2:first-of-type").text,
      :content => section.filename
    }
  end
end

#render_chapter(content) ⇒ Object


132
133
134
135
# File 'lib/verku/exporter/epub.rb', line 132

def render_chapter(content)
  locals = config.merge(:content => content)
  render_template(template_path, locals)
end

#sectionsObject


5
6
7
8
9
10
11
12
13
14
# 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_pathObject


154
155
156
# File 'lib/verku/exporter/epub.rb', line 154

def template_path
  root_dir.join("_templates/epub/page.erb")
end

#thanks_pageObject


56
57
58
# File 'lib/verku/exporter/epub.rb', line 56

def thanks_page
  Dir[thanks_path]
end

#thanks_pathObject


163
164
165
# File 'lib/verku/exporter/epub.rb', line 163

def thanks_path
  tmp_dir.join("thanks.html")
end

#tmp_dirObject


157
158
159
# File 'lib/verku/exporter/epub.rb', line 157

def tmp_dir
  root_dir.join("builds/tmp")
end

#toc_pathObject


172
173
174
# File 'lib/verku/exporter/epub.rb', line 172

def toc_path
  tmp_dir.join("toc.html")
end

#write_backpage!Object


59
60
61
62
63
64
# 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_copyright!Object


73
74
75
76
77
78
79
# File 'lib/verku/exporter/epub.rb', line 73

def write_copyright!
  contents = render_template(root_dir.join("_templates/html/copyright.erb"), config)
  FileUtils.mkdir_p(File.dirname(copyright_path))
  File.open(copyright_path,"w") do |file|
    file << contents
  end
end

#write_coverpage!Object


65
66
67
68
69
70
71
72
# 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!Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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!Object


80
81
82
83
84
85
86
# 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!Object


87
88
89
90
91
92
93
# 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