Class: EPUB::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/directory.rb,
lib/epub/directory/version.rb

Defined Under Namespace

Classes: Rendition

Constant Summary collapse

SRC_DIR =
Pathname.new("src")
DEST_DIR =
Pathname.new("build")
VERSION =

epub-directory version

"0.1.6"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(path) ⇒ Object



20
21
22
23
24
# File 'lib/epub/directory.rb', line 20

def add(path)
  EPUB::Parser.parse(path).renditions.each do |rendition|
    add_rendition rendition
  end
end

.add_rendition(rendition) ⇒ Object



30
31
32
# File 'lib/epub/directory.rb', line 30

def add_rendition(rendition)
  Rendition.add rendition
end

.buildObject



26
27
28
# File 'lib/epub/directory.rb', line 26

def build
  new.build
end

.run(paths) ⇒ Object



13
14
15
16
17
18
# File 'lib/epub/directory.rb', line 13

def run(paths)
  paths.each do |path|
    add path
  end
  build
end

Instance Method Details

#buildObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/epub/directory.rb', line 35

def build
  DEST_DIR.mkpath
  opfs = []
  Pathname.glob("#{SRC_DIR}/**/*.opf").each do |opf_path|
    make_destination_directory opf_path
    build_opf opf_path
    build_html opf_path
    build_opds opf_path

    opfs << opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  rescue => error
    warn "Error occurred while processing #{opf_path}"
    raise if $DEBUG
  end
  build_indices opfs
end

#build_html(opf_path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/epub/directory.rb', line 64

def build_html(opf_path)
  dest_path = opf_path.sub_ext(".html").to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  rendition = EPUB::Parser::Publication.new(opf_path.read).parse
   = rendition.
  page_title = "Information on #{.title}(Release Identifier: #{.release_identifier})"
  template_path = File.join(__dir__, "../../templates/release-identifier.haml")
  engine = Haml::Engine.new(File.read(template_path))
  engine.options[:format] = :html5
  dest_path.write engine.render(binding)
end

#build_indices(opfs) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/epub/directory.rb', line 108

def build_indices(opfs)
  dest_path = DEST_DIR/"index.html"
  page_title = "EPUB Directory"
  items = opfs.lazy.collect {|path|
    {
      "uri" => Pathname.new(path).sub_ext(".html").to_path.sub(DEST_DIR.to_path, ""),
      "metadata" => EPUB::Parser::Publication.new(File.read(path)).parse.
    }
  }
  template_path = File.join(__dir__, "../../templates/index.haml")
  engine = Haml::Engine.new(File.read(template_path))
  engine.options[:format] = :html5
  dest_path.write engine.render(binding)
end

#build_json(opf_path) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/epub/directory.rb', line 104

def build_json(opf_path)
  raise NotImplementedError
end

#build_opds(opf_path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/epub/directory.rb', line 76

def build_opds(opf_path)
  dest_path = opf_path.sub_ext(".opds").to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  rendition = EPUB::Parser::Publication.new(opf_path.read).parse
  opds = RSS::Maker.make("atom:entry") {|maker|
    maker.items.new_item do |entry|
      entry.id = rendition..release_identifier
      entry.updated = rendition..modified.to_s
      entry.title = rendition..title
      # TODO: Filter authors by refines
      creators = rendition..creators + rendition..metas.select {|meta| ["dc:publisher", "dcterms:creator"].include? meta.property}
      creators.each do |creator|
        entry.authors.new_author do |author|
          author.name = creator.to_s
        end
      end
      entry.author = "Unknown" unless entry.author
      entry.summary = rendition..description
      entry.links.new_link do |link|
        link.rel = RSS::OPDS::RELATIONS["acquisition"] # TODO: Arrange by info.toml
        link.href = dest_path.basename.sub_ext(".epub").to_path # TODO: Arrange by info.toml
        link.type = "application/epub+zip"
      end
    end
  }
  dest_path.write opds
end

#build_opf(opf_path) ⇒ Object



58
59
60
61
62
# File 'lib/epub/directory.rb', line 58

def build_opf(opf_path)
  dest_path = opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  dest_path.write opf_path.read
end

#make_destination_directory(opf_path) ⇒ Object



52
53
54
55
56
# File 'lib/epub/directory.rb', line 52

def make_destination_directory(opf_path)
  dest_path = opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  dest_path.dirname.mkpath
end