Class: Jekyll::Site
- Inherits:
-
Object
- Object
- Jekyll::Site
- Defined in:
- lib/jekyll/epub.rb
Overview
:nodoc:
Instance Method Summary collapse
- #all ⇒ Object
-
#apply_epub_filters(files) ⇒ Object
This is a Jekyll extension.
-
#create_order(pages, posts, order, method) ⇒ Object
:nodoc:.
-
#epub ⇒ Object
This is a Jekyll extension.
-
#get_item(item, method, posts, pages) ⇒ Object
:nodoc:.
-
#package_epub ⇒ Object
This is a Jekyll extension.
-
#write_epub_file(tmpl, files) ⇒ Object
This is a Jekyll extension.
-
#zip ⇒ Object
This is a Jekyll extension.
Instance Method Details
#all ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/jekyll/epub.rb', line 18 def all if self.config["epub"]["pages-order"] self.create_order( pages.clone, posts.clone, self.config["epub"]["pages-order"], :url ) else posts + pages end end |
#apply_epub_filters(files) ⇒ Object
This is a Jekyll extension
Very simple post-filters
This method need to be rewritten and add the possibility to use user’s defined filters
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/jekyll/epub.rb', line 165 def apply_epub_filters( files ) #:nodoc: files.each do |file| file.output = file.output.gsub( /(src\s*=\s*['|"])\//, '\1' ) file.output = file.output.gsub( /(href\s*=\s*['|"])\//, '\1' ) # Remove all <script> tags file.output = file.output.gsub( /<script[^>]*>[^<\/script>]*<\/script>/, "" ) file.output = file.output.gsub( /<script[^>]*\/>/, "" ) file.output = file.output.gsub( /<noscript>.*<\/noscript>/, "" ) end end |
#create_order(pages, posts, order, method) ⇒ Object
:nodoc:
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jekyll/epub.rb', line 44 def create_order( pages, posts, order, method ) #:nodoc: _result = [] _order = {} order.each do |item| if item.class == Symbol _order[item] = eval(item.to_s) else _order[item], posts, pages = get_item( item, method, posts, pages ) end end order.each do |item| _result = _result + _order[item] end return _result end |
#epub ⇒ Object
This is a Jekyll extension
Same as Site::process but generate the epub.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll/epub.rb', line 65 def epub #:nodoc: self.reset self.read # Change layout to use epub self.layouts.each do |name, layout| self.layouts[name].data['layout'] = layout.data['epub'] if layout.data['epub'] end # Generate specifics epub files FileUtils.rm_rf( self.dest ) self.package_epub self.render # Apply filters self.apply_epub_filters( pages ) self.apply_epub_filters( posts ) self.write # Create epub file self.zip end |
#get_item(item, method, posts, pages) ⇒ Object
:nodoc:
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jekyll/epub.rb', line 26 def get_item( item, method, posts, pages ) #:nodoc: _out = [] posts.each do |p| if p.send(method).gsub( /^\//, "" ) == item _out << posts.delete(p) end end pages.each do |p| if p.send(method).gsub( /^\//, "" ) == item _out << pages.delete(p) end end return _out, posts, pages end |
#package_epub ⇒ Object
This is a Jekyll extension
Generate the specifics epub files : content.opf, toc.ncx, mimetype, page-template.xpgt and META-INF/container.xml
94 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/jekyll/epub.rb', line 94 def package_epub #:nodoc: files = [] order = 0 # Check for cover image if self.config["epub"]["cover-image"] self.config["epub"]["cover"] = { "image" => self.config["epub"]["cover-image"], "mime" => MIME::Types.type_for( self.config["epub"]["cover-image"] ).to_s } end (all + static_files).each do |p| url = p.url.gsub( /^\//, "" ) next if url == self.config["epub"]["cover-image"] mime = MIME::Types.type_for( url ).to_s mime = "application/xhtml+xml" if mime == "text/html" next if self.config['exclude'].include?(File.basename( url )) if mime == "" then $stderr.puts "** Ignore file #{url}, unknown mime type!" next end file_data = { 'id' => url.gsub( /\/|\./, "_" ), 'url' => url, 'mime' => mime } begin p.data['layout'] = p.data['epub'] if p.data['epub'] file_data['title'] = p.data["title"] file_data['order'] = order order += 1 rescue => e if file_data['mime'] == "text/html" $stderr.puts "** Ignore file #{url} : #{e.}" next end end files << file_data end FileUtils.mkdir_p(self.dest) FileUtils.mkdir_p(File.join( self.dest, "META-INF" )) write_epub_file( "content.opf", files ) write_epub_file( "toc.ncx", files ) write_epub_file( "mimetype", files ) write_epub_file( "page-template.xpgt", files ) write_epub_file( File.join( "META-INF", "container.xml" ) , files ) if self.config["epub"]["cover-image"] write_epub_file( "cover.xhtml", files ) end end |
#write_epub_file(tmpl, files) ⇒ Object
This is a Jekyll extension
Write a specific epub file using Liquid. See Jekyll::Site::package_epub
153 154 155 156 157 158 |
# File 'lib/jekyll/epub.rb', line 153 def write_epub_file( tmpl, files ) #:nodoc: $stderr.puts "** Create #{tmpl}" template_file = File.join( File.( File.dirname( __FILE__ ) ), "epub", "templates", tmpl ) template_content = Liquid::Template.parse(File.open(template_file).read).render( 'epub' => self.config['epub'], 'files' => files ) File.open( File.join( self.dest, tmpl ), "w+" ).puts template_content end |
#zip ⇒ Object
This is a Jekyll extension
Create the epub file…
180 181 182 183 184 185 186 187 188 |
# File 'lib/jekyll/epub.rb', line 180 def zip #:nodoc: Dir.chdir( self.dest ) do filename = self.config['epub']['name'] filename += ".epub" unless File.extname(filename) == ".epub" $stderr.puts "** Create epub file #{filename} in #{Dir.pwd}..." %x(zip -X9 \"#{filename}\" mimetype) %x(zip -Xr9D \"#{filename}\" * -xi mimetype) end end |