Class: Publishr::EpubRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/publishr/epub_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, inpath, outpath, metadata, rails_resources_url = '') ⇒ EpubRenderer

Returns a new instance of EpubRenderer.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/publishr/epub_renderer.rb', line 20

def initialize(name,inpath,outpath,,rails_resources_url='')
  @name = name
  @inpath = inpath
  @outpath = outpath
  @metadata = 
  @gempath = Publishr::Project.gempath
  @rails_resources_url = rails_resources_url

  @html_files = []
  @image_files = []
end

Instance Method Details

#compile_xmlsObject



63
64
65
66
67
68
69
70
# File 'lib/publishr/epub_renderer.rb', line 63

def compile_xmls
  File.open(File.join(@outpath,'content.opf'),'w'){ |f| f.write render_content_opf }
  File.open(File.join(@outpath,'toc.ncx'),'w'){ |f| f.write render_toc_ncx     }
  Dir.chdir @outpath
  filename = File.join(@inpath,"#{ @name }.epub")
  `zip -X0 #{ filename } mimetype`
  `zip -Xur9D #{ filename } *`
end

#make_epub_directory_structureObject



38
39
40
41
42
43
44
45
46
# File 'lib/publishr/epub_renderer.rb', line 38

def make_epub_directory_structure
  FileUtils.rm_rf @outpath
  FileUtils.cp_r File.join(@gempath,'lib','epub_skeleton'), @outpath
  FileUtils.cp Dir[File.join(@inpath,'images','*.jpg')], @outpath
  FileUtils.cp File.join(@inpath,'cover.jpg'), @outpath
  @image_files = Dir[File.join(@outpath,'*.jpg')]
  # users can provide an overriding css file
  FileUtils.cp_f File.join(@inpath,'epub.css'), @outpath if File.exists?(File.join(@inpath,'epub.css'))
end

#renderObject



32
33
34
35
36
# File 'lib/publishr/epub_renderer.rb', line 32

def render
  make_epub_directory_structure
  render_htmls
  compile_xmls
end

#render_content_opfObject



72
73
74
75
76
77
# File 'lib/publishr/epub_renderer.rb', line 72

def render_content_opf
  erb = File.open(File.join(@gempath,'lib','epub_templates','content.opf.erb'),'r').read
  spine_items = render_spine_items
  manifest_items = render_manifest_items
  ERB.new(erb).result binding
end

#render_htmlsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/publishr/epub_renderer.rb', line 48

def render_htmls
  kramdown_options = {
    :smart_quotes => @metadata['kramdown_options'][:smart_quotes],
    :template => File.join(@gempath,'lib','epub_templates','kramdown.html')
  }
  Dir[File.join(@inpath,'*.txt')].each do |infilepath|
    kramdown = File.open(infilepath, 'r').read
    html = Kramdown::Document.new(kramdown, kramdown_options).to_html
    degraded_html = HtmlProcessor.new(html,@inpath,@metadata,@rails_resources_url).degrade
    outfilepath = File.join(@outpath, File.basename(infilepath).gsub(/(.*).txt/, '\1.html'))
    File.open(outfilepath, 'w'){ |f| f.write degraded_html }
  end
  @html_files = ([File.join(@outpath,'covertext.html')] + [File.join(@outpath,'frontmatter.html')] + [File.join(@outpath,'toc.html')] + Dir[File.join(@outpath,'*.html')].sort).uniq
end

#render_manifest_itemsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/publishr/epub_renderer.rb', line 98

def render_manifest_items
  image_files = Dir[File.join(@outpath,'*.jpg')]
  erb = File.open(File.join(@gempath,'lib','epub_templates','manifest_items.erb'),'r').read
  results = []
  (@html_files + @image_files).each do |h|
    filename = File.basename(h)
    id = filename.gsub '.',''
    mediatype = case File.extname(h) 
      when '.html' then 'application/xhtml+xml'
      when '.jpg' then 'image/jpeg'
      when '.png' then 'image/png'
    end
    results << (ERB.new(erb).result binding)
  end
  results.join("\n")
end

#render_nav_pointsObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/publishr/epub_renderer.rb', line 85

def render_nav_points
  erb = File.open(File.join(@gempath,'lib','epub_templates','nav_points.erb'),'r').read
  results = []
  i = 0
  @html_files.each do |h|
    filename = File.basename(h)
    id = filename.gsub '.',''
    i += 1
    results << (ERB.new(erb).result binding)
  end
  results.join("\n")
end

#render_spine_itemsObject



115
116
117
118
119
120
121
122
123
# File 'lib/publishr/epub_renderer.rb', line 115

def render_spine_items
  erb = File.open(File.join(@gempath,'lib','epub_templates','spine_items.erb'),'r').read
  results = []
  @html_files.each do |h|
    id = File.basename(h).gsub '.',''
    results << (ERB.new(erb).result binding)
  end
  results.join("\n")
end

#render_toc_ncxObject



79
80
81
82
83
# File 'lib/publishr/epub_renderer.rb', line 79

def render_toc_ncx
  erb = File.open(File.join(@gempath,'lib','epub_templates','toc.ncx.erb'),'r').read
  nav_points = render_nav_points
  ERB.new(erb).result binding
end