Class: FB2rb::Book

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

Overview

Holds data of a single FB2 file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description = Description.new, bodies = [], binaries = [], stylesheets = []) ⇒ Book

Returns a new instance of Book.



25
26
27
28
29
30
# File 'lib/fb2rb.rb', line 25

def initialize(description = Description.new, bodies = [], binaries = [], stylesheets = [])
  @binaries = binaries
  @bodies = bodies
  @description = description
  @stylesheets = stylesheets
end

Instance Attribute Details

#binariesArray<FB2fb::Binary>

Returns:

  • (Array<FB2fb::Binary>)


23
24
25
# File 'lib/fb2rb.rb', line 23

def binaries
  @binaries
end

#bodiesArray<FB2rb::Body>

Returns:



21
22
23
# File 'lib/fb2rb.rb', line 21

def bodies
  @bodies
end

#descriptionFB2rb::Description

Returns:



19
20
21
# File 'lib/fb2rb.rb', line 19

def description
  @description
end

#stylesheetsArray<FB2rb::Stylesheet>

Returns:



17
18
19
# File 'lib/fb2rb.rb', line 17

def stylesheets
  @stylesheets
end

Class Method Details

.ns_prefix(namespace, namespaces) ⇒ Object



57
58
59
60
# File 'lib/fb2rb.rb', line 57

def ns_prefix(namespace, namespaces)
  prefix = namespaces.key(namespace)
  prefix.nil? ? nil : prefix.sub(/^xmlns:/, '')
end

.read_compressed(filename_or_io) ⇒ FB2rb::Book?

Reads existing compressed FB2 file from an IO object, and creates new Book object.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fb2rb.rb', line 35

def read_compressed(filename_or_io)
  Zip::InputStream.open(filename_or_io) do |zis|
    while (entry = zis.get_next_entry)
      next if entry.directory?

      xml = Nokogiri::XML::Document.parse(zis)
      fb2_prefix = ns_prefix(FB2rb::FB2_NAMESPACE, xml.namespaces)
      xlink_prefix = ns_prefix(FB2rb::XLINK_NAMESPACE, xml.namespaces)
      return parse(xml, fb2_prefix, xlink_prefix)
    end
  end
end

.read_uncompressed(filename_or_io) ⇒ FB2rb::Book

Reads existing uncompressed FB2 file from an IO object, and creates new Book object.

Returns:



50
51
52
53
54
55
# File 'lib/fb2rb.rb', line 50

def read_uncompressed(filename_or_io)
  xml = read_xml(filename_or_io)
  fb2_prefix = ns_prefix(FB2rb::FB2_NAMESPACE, xml.namespaces)
  xlink_prefix = ns_prefix(FB2rb::XLINK_NAMESPACE, xml.namespaces)
  parse(xml, fb2_prefix, xlink_prefix)
end

Instance Method Details

#add_binary(name, filename_or_io, content_type = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/fb2rb.rb', line 109

def add_binary(name, filename_or_io, content_type = nil)
  if filename_or_io.respond_to?(:read)
    add_binary_io name, filename_or_io, content_type
  else
    File.open(filename_or_io, 'r') do |io|
      add_binary_io name, io, content_type
    end
  end
end

#add_stylesheet(content_type, filename_or_io) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/fb2rb.rb', line 119

def add_stylesheet(content_type, filename_or_io)
  if filename_or_io.respond_to?(:read)
    add_stylesheet_io(content_type, filename_or_io)
  else
    File.open(filename_or_io, 'r') do |io|
      add_stylesheet_io(content_type, io)
    end
  end
end

#to_xml(xml) ⇒ Object

rubocop:disable Metrics/MethodLength



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fb2rb.rb', line 93

def to_xml(xml) # rubocop:disable Metrics/MethodLength
  xml.FictionBook('xmlns' => FB2rb::FB2_NAMESPACE,
                  'xmlns:l' => 'http://www.w3.org/1999/xlink') do
    @stylesheets.each do |stylesheet|
      stylesheet.to_xml(xml)
    end
    @description.to_xml(xml)
    @bodies.each do |body|
      body.to_xml(xml)
    end
    @binaries.each do |binary|
      binary.to_xml(xml)
    end
  end
end

#write_compressed(filename_or_io = StringIO.new) ⇒ Object

Writes compressed FB2 to file or IO object. If file exists, it will be overwritten.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fb2rb.rb', line 130

def write_compressed(filename_or_io = StringIO.new)
  if filename_or_io.respond_to?(:write)
    Zip::OutputStream.write_buffer(filename_or_io) do |zos|
      write_to_zip(zos)
    end
  else
    Zip::OutputStream.open(filename_or_io) do |zos|
      write_to_zip(zos)
    end
  end
end

#write_uncompressed(filename_or_io = StringIO.new) ⇒ Object

Writes FB2 (uncompressed) to file or IO object specified by the argument.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fb2rb.rb', line 143

def write_uncompressed(filename_or_io = StringIO.new) # rubocop:disable Metrics/MethodLength
  data = (Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    to_xml(xml)
  end).to_xml

  if filename_or_io.respond_to?(:write)
    filename_or_io.write(data)
  else
    File.open(filename_or_io, 'wb') do |io|
      io.write(data)
    end
  end
  filename_or_io
end