Class: FB2rb::Book
- Inherits:
-
Object
- Object
- FB2rb::Book
- Defined in:
- lib/fb2rb.rb
Overview
Holds data of a single FB2 file
Instance Attribute Summary collapse
- #binaries ⇒ Array<FB2fb::Binary>
- #bodies ⇒ Array<FB2rb::Body>
- #description ⇒ FB2rb::Description
- #stylesheets ⇒ Array<FB2rb::Stylesheet>
Class Method Summary collapse
- .ns_prefix(namespace, namespaces) ⇒ Object
-
.read_compressed(filename_or_io) ⇒ FB2rb::Book?
Reads existing compressed FB2 file from an IO object, and creates new Book object.
-
.read_uncompressed(filename_or_io) ⇒ FB2rb::Book
Reads existing uncompressed FB2 file from an IO object, and creates new Book object.
Instance Method Summary collapse
- #add_binary(id, filename_or_io, content_type = nil) ⇒ Object
- #add_stylesheet(content_type, filename_or_io) ⇒ Object
-
#initialize(description: Description.new, bodies: [], binaries: [], stylesheets: []) ⇒ Book
constructor
A new instance of Book.
-
#to_xml(xml) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#write_compressed(filename_or_io = StringIO.new) ⇒ Object
Writes compressed FB2 to file or IO object.
-
#write_uncompressed(filename_or_io = StringIO.new) ⇒ Object
Writes FB2 (uncompressed) to file or IO object specified by the argument.
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
#binaries ⇒ Array<FB2fb::Binary>
23 24 25 |
# File 'lib/fb2rb.rb', line 23 def binaries @binaries end |
#description ⇒ FB2rb::Description
19 20 21 |
# File 'lib/fb2rb.rb', line 19 def description @description end |
#stylesheets ⇒ Array<FB2rb::Stylesheet>
17 18 19 |
# File 'lib/fb2rb.rb', line 17 def stylesheets @stylesheets end |
Class Method Details
.ns_prefix(namespace, namespaces) ⇒ Object
58 59 60 61 |
# File 'lib/fb2rb.rb', line 58 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 nil end |
.read_uncompressed(filename_or_io) ⇒ FB2rb::Book
Reads existing uncompressed FB2 file from an IO object, and creates new Book object.
51 52 53 54 55 56 |
# File 'lib/fb2rb.rb', line 51 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(id, filename_or_io, content_type = nil) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/fb2rb.rb', line 110 def add_binary(id, filename_or_io, content_type = nil) if filename_or_io.respond_to?(:read) add_binary_io id, filename_or_io, content_type else File.open(filename_or_io, 'r') do |io| add_binary_io id, io, content_type end end end |
#add_stylesheet(content_type, filename_or_io) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/fb2rb.rb', line 120 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fb2rb.rb', line 94 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.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fb2rb.rb', line 131 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/fb2rb.rb', line 144 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 |