lib/fb2rb.rb in fb2rb-0.1.0 vs lib/fb2rb.rb in fb2rb-0.1.1
- old
+ new
@@ -11,23 +11,28 @@
FB2_NAMESPACE = 'http://www.gribuser.ru/xml/fictionbook/2.0'
XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink'
# Holds data of a single FB2 file
class Book
+ # @return [Array<FB2rb::Stylesheet>]
attr_accessor(:stylesheets)
+ # @return [FB2rb::Description]
attr_accessor(:description)
+ # @return [Array<FB2rb::Body>]
attr_accessor(:bodies)
+ # @return [Array<FB2fb::Binary>]
attr_accessor(:binaries)
def initialize(description = Description.new, bodies = [], binaries = [], stylesheets = [])
@binaries = binaries
@bodies = bodies
@description = description
@stylesheets = stylesheets
end
# Reads existing FB2 file from an IO object, and creates new Book object.
+ # @return [FB2rb::Book]
def self.read(filename_or_io)
Zip::InputStream.open(filename_or_io) do |zis|
while (entry = zis.get_next_entry)
next if entry.directory?
@@ -42,10 +47,11 @@
def self.ns_prefix(namespace, namespaces)
prefix = namespaces.key(namespace)
prefix.nil? ? nil : prefix.sub(/^xmlns:/, '')
end
+ # @return [FB2rb::Book]
def self.parse(xml, fb2_prefix, xlink_prefix) # rubocop:disable Metrics/MethodLength
Book.new(
Description.parse(xml.xpath("/#{fb2_prefix}:FictionBook/#{fb2_prefix}:description"), fb2_prefix, xlink_prefix),
xml.xpath("/#{fb2_prefix}:FictionBook/#{fb2_prefix}:body").map do |node|
Body.parse(node)
@@ -84,10 +90,11 @@
end
end
end
# Serializes and returns FB2 as StringIO.
+ # @return [StringIO]
def to_ios
Zip::OutputStream.write_buffer do |io|
write_to_stream(io)
end
end
@@ -130,14 +137,19 @@
end
end
# Holds <description> data
class Description
+ # @return [FB2rb::TitleInfo]
attr_accessor(:title_info)
+ # @return [FB2rb::TitleInfo, nil]
attr_accessor(:src_title_info)
+ # @return [FB2rb::DocumentInfo]
attr_accessor(:document_info)
+ # @return [FB2rb::PublishInfo, nil]
attr_accessor(:publish_info)
+ # @return [Array<FB2rb::CustomInfo>]
attr_accessor(:custom_infos)
# TODO: <output>
def initialize(title_info = TitleInfo.new,
document_info = DocumentInfo.new,
@@ -149,10 +161,11 @@
@publish_info = publish_info
@src_title_info = src_title_info
@custom_infos = custom_infos
end
+ # @return [FB2rb::Description]
def self.parse(xml, fb2_prefix, xlink_prefix) # rubocop:disable Metrics/MethodLength
publish_info_xml = xml.at("./#{fb2_prefix}:publish-info")
src_title_info_xml = xml.at("./#{fb2_prefix}:src-title-info")
Description.new(
TitleInfo.parse(xml.at("./#{fb2_prefix}:title-info"), fb2_prefix, xlink_prefix),
@@ -178,11 +191,13 @@
end
end
# Holds <stylesheet> data
class Stylesheet
+ # @return [String]
attr_accessor(:type)
+ # @return [String, nil]
attr_accessor(:content)
def initialize(type = '', content = nil)
@type = type
@content = content
@@ -199,18 +214,21 @@
end
end
# Holds <custom-info> data
class CustomInfo
+ # @return [String]
attr_accessor(:info_type)
+ # @return [String, nil]
attr_accessor(:content)
def initialize(info_type = '', content = nil)
@info_type = info_type
@content = content
end
+ # @return [FB2rb::CustomInfo]
def self.parse(xml)
CustomInfo.new(xml['info-type'], xml.text)
end
def to_xml(xml)
@@ -220,15 +238,21 @@
end
end
# Holds <publish-info> data
class PublishInfo
+ # @return [String, nil]
attr_accessor(:book_name)
+ # @return [String, nil]
attr_accessor(:publisher)
+ # @return [String, nil]
attr_accessor(:city)
+ # @return [String, nil]
attr_accessor(:year)
+ # @return [String, nil]
attr_accessor(:isbn)
+ # @return [Array<FB2RB::Sequence>]
attr_accessor(:sequences)
def initialize(book_name = nil, # rubocop:disable Metrics/ParameterLists
publisher = nil,
city = nil,
@@ -241,10 +265,11 @@
@year = year
@isbn = isbn
@sequences = sequences
end
+ # @return [FB2RB::PublishInfo]
def self.parse(xml, fb2_prefix)
PublishInfo.new(
xml.at("./#{fb2_prefix}:book-name/text()")&.text,
xml.at("./#{fb2_prefix}:publisher/text()")&.text,
xml.at("./#{fb2_prefix}:city/text()")&.text,
@@ -270,18 +295,27 @@
end
end
# Holds <document-info> data
class DocumentInfo
+ # @return [Array<FB2rb::Author>]
attr_accessor(:authors)
+ # @return [String, nil]
attr_accessor(:program_used)
+ # @return [FB2rb::FB2Date]
attr_accessor(:date)
+ # @return [Array<String>]
attr_accessor(:src_urls)
+ # @return [String, nil]
attr_accessor(:src_ocr)
+ # @return [String]
attr_accessor(:id)
+ # @return [String]
attr_accessor(:version)
+ # @return [String, nil]
attr_accessor(:history)
+ # @return [Array<String>]
attr_accessor(:publishers)
def initialize(authors = [], # rubocop:disable Metrics/ParameterLists
program_used = nil,
date = FB2Date.new,
@@ -300,10 +334,11 @@
@version = version
@history = history
@publishers = publishers
end
+ # @return [FB2rb::DocumentInfo]
def self.parse(xml, fb2_prefix) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
date = xml.at("./#{fb2_prefix}:date")
DocumentInfo.new(
xml.xpath("./#{fb2_prefix}:author").map do |node|
Author.parse(node, fb2_prefix)
@@ -312,15 +347,16 @@
date.nil? ? FB2Date.new : FB2Date.parse(date),
xml.xpath("./#{fb2_prefix}:src-url").map(&:text),
xml.at("./#{fb2_prefix}:src-ocr")&.text,
xml.at("./#{fb2_prefix}:id").text,
xml.at("./#{fb2_prefix}:version")&.text,
- xml.at("./#{fb2_prefix}:history")&.children&.to_s&.strip
+ xml.at("./#{fb2_prefix}:history")&.children&.to_s&.strip,
+ xml.xpath("./#{fb2_prefix}:publisher").map(&:text)
)
end
- def to_xml(xml) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
+ def to_xml(xml) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
xml.send(:'document-info') do
@authors.each do |author|
author.to_xml(xml, 'author')
end
xml.send('program-used', @program_used) unless @program_used.nil?
@@ -334,26 +370,40 @@
unless @history.nil?
xml.history do
xml << @history
end
end
+ @publishers.each do |publisher|
+ xml.publisher(publisher)
+ end
end
end
end
# Holds <title-info>/<src-title-info> data
class TitleInfo
+ # @return [Array<String>]
attr_accessor(:genres)
+ # @return [Array<FB2rb::Author>]
attr_accessor(:authors)
+ # @return [String]
attr_accessor(:book_title)
+ # @return [String, nil]
attr_accessor(:annotation)
+ # @return [Array<String>]
attr_accessor(:keywords)
+ # @return [String, nil]
attr_accessor(:date)
+ # @return [FB2rb::Coverpage, nil]
attr_accessor(:coverpage)
+ # @return [String]
attr_accessor(:lang)
+ # @return [String, nil]
attr_accessor(:src_lang)
+ # @return [Array<FB2rb::Author>]
attr_accessor(:translators)
+ # @return [Array<FB2rb::Sequence>]
attr_accessor(:sequences)
def initialize(genres = [], # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
authors = [],
book_title = '',
@@ -376,10 +426,11 @@
@src_lang = src_lang
@translators = translators
@sequences = sequences
end
+ # @return [FB2rb::TitleInfo]
def self.parse(xml, fb2_prefix, xlink_prefix) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
date = xml.at("./#{fb2_prefix}:date")
coverpage = xml.at("./#{fb2_prefix}:coverpage")
TitleInfo.new(
xml.xpath("./#{fb2_prefix}:genre/text()").map(&:text),
@@ -431,10 +482,11 @@
end
end
# Holds <coverpage> data
class Coverpage
+ # @return [Array<String>]
attr_accessor(:images)
def initialize(images = [])
@images = images
end
@@ -454,11 +506,13 @@
end
end
# Holds <date> data
class FB2Date
+ # @return [String]
attr_accessor(:display_value)
+ # @return [Date, nil]
attr_accessor(:value)
def initialize(display_value = '', value = nil)
@display_value = display_value
@value = value
@@ -479,18 +533,21 @@
end
end
# Holds <sequence> data
class Sequence
+ # @return [String]
attr_accessor(:name)
+ # @return [Integer, nil]
attr_accessor(:number)
def initialize(name = '', number = nil)
@name = name
@number = number
end
+ # @return [FB2rb::Sequence]
def self.parse(xml)
Sequence.new(xml['name'], xml['number']&.to_i)
end
def to_xml(xml)
@@ -500,16 +557,23 @@
end
end
# Holds <author> data
class Author
+ # @return [String, nil]
attr_accessor(:first_name)
+ # @return [String, nil]
attr_accessor(:middle_name)
+ # @return [String, nil]
attr_accessor(:last_name)
+ # @return [String, nil]
attr_accessor(:nickname)
+ # @return [Array<String>]
attr_accessor(:home_pages)
+ # @return [Array<String>]
attr_accessor(:emails)
+ # @return [String, nil]
attr_accessor(:id)
def initialize(first_name = nil, # rubocop:disable Metrics/ParameterLists
middle_name = nil,
last_name = nil,
@@ -524,10 +588,11 @@
@home_pages = home_pages
@emails = emails
@id = id
end
+ # @return [FB2rb::Author]
def self.parse(xml, fb2_prefix) # rubocop:disable Metrics/CyclomaticComplexity
Author.new(
xml.at("./#{fb2_prefix}:first-name/text()")&.text,
xml.at("./#{fb2_prefix}:middle-name/text()")&.text,
xml.at("./#{fb2_prefix}:last-name/text()")&.text,
@@ -555,18 +620,21 @@
end
end
# Holds <body> data
class Body
+ # @return [String, nil]
attr_accessor(:name)
+ # @return [String]
attr_accessor(:content)
def initialize(name = nil, content = '')
@name = name
@content = content
end
+ # @return [FB2rb::Body]
def self.parse(xml)
Body.new(
xml['name'],
xml.children.to_s.strip
)
@@ -582,15 +650,18 @@
end
end
# Holds data of a single binary within FB2 file
class Binary
+ # @return [String]
attr_accessor(:id)
+ # @return [String]
attr_accessor(:content)
+ # @return [String, nil]
attr_accessor(:content_type)
- def initialize(name, content, content_type = nil)
- @id = name
+ def initialize(id, content, content_type = nil)
+ @id = id
@content = content
@content_type = content_type
end
def self.parse(xml)