Sha256: e47526b805bc44bab719b35b72e9f328380ac8ff66a6be5e483e2c58472ccdf9

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

require "tmpdir"
require "gepub"

module NcodeSyosetu
  module Builder
    class Epub3
      def self.write(novel, path)
        Dir.mktmpdir do |tmpdir|
          builder = GEPUB::Builder.new do
            language "ja"
            unique_identifier novel.url
            title novel.title
            creator novel.author

            date Time.now.to_s

            resources(workdir: tmpdir) do
              toc_html = novel.toc.html.gsub(%r[<a href="/[^/]+/(\d+)/?">], '<a href="\1.html">')
              File.write("toc.html", toc_html)
              nav "toc.html"

              ordered do
                next_heading = nil
                novel.episodes.each do |episode|
                  if episode.is_a?(NcodeSyosetu::Model::Heading)
                    next_heading = episode.title
                  else
                    html_path = "#{episode.number}.html"
                    File.write(html_path, episode.html)
                    file html_path
                    if next_heading
                      heading next_heading
                      next_heading = nil
                    end
                  end
                end
              end
            end
          end

          builder.generate_epub(path)
        end
      end
    end
  end

  NcodeSyosetu::Model::Novel.class_eval do
    def write_epub3(file)
      NcodeSyosetu::Builder::Epub3.write(self, file)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ncode-syosetu-0.0.5 lib/ncode_syosetu/builder/epub3.rb
ncode-syosetu-0.0.4 lib/ncode_syosetu/builder/epub3.rb
ncode-syosetu-0.0.3 lib/ncode_syosetu/builder/epub3.rb
ncode-syosetu-0.0.2 lib/ncode_syosetu/builder/epub3.rb
ncode-syosetu-0.0.1 lib/ncode_syosetu/builder/epub3.rb