class ReaPack::Index::Metadata
Constants
- DESC
- TAG
Public Class Methods
new(parent)
click to toggle source
# File lib/reapack/index/metadata.rb, line 6 def initialize(parent) @parent = parent @root = parent.element_children.find {|node| node.name == TAG } end
Public Instance Methods
about()
click to toggle source
# File lib/reapack/index/metadata.rb, line 89 def about cdata = nil if @root desc = @root.element_children.find {|node| node.name == DESC } cdata = desc.children.first if desc end cdata ? cdata.content : String.new end
about=(content)
click to toggle source
# File lib/reapack/index/metadata.rb, line 100 def about=(content) content = content.to_s if !content.empty? && !content.start_with?("{\\rtf") content = make_rtf content end return if content == self.about make_root desc = @root.element_children.find {|node| node.name == DESC } @dirty = true if content.empty? desc.remove auto_remove return end if desc desc.children.each {|n| n.remove } else desc = Nokogiri::XML::Node.new DESC, @root.document desc.parent = @root end cdata = Nokogiri::XML::CDATA.new desc, content cdata.parent = desc end
links(type)
click to toggle source
# File lib/reapack/index/metadata.rb, line 16 def links(type) Link.find_all(type, @root).map {|node| Link.from_node node } .select {|link| link.url.index('http') == 0 } end
modified?()
click to toggle source
# File lib/reapack/index/metadata.rb, line 12 def modified? !!@dirty end
push_link(type, name = nil, url)
click to toggle source
# File lib/reapack/index/metadata.rb, line 21 def push_link(type, name = nil, url) Link.check_type type begin uri = Addressable::URI.parse url unless ['http', 'https'].include? uri.scheme raise Addressable::URI::InvalidURIError end rescue Addressable::URI::InvalidURIError raise Error, "invalid link '#{url}'" end make_root link = Link.new name || url, url node = Link.find type, link.name, @root node ||= Link.find type, link.url, @root if node link.instance_variable_set :@is_new, false link.instance_variable_set :@modified, link != Link.from_node(node) node.remove_attribute Link::URL else link.instance_variable_set :@is_new, true link.instance_variable_set :@modified, true node = Nokogiri::XML::Node.new Link::TAG, @root.document node.parent = @root node[Link::REL] = type end if name node[Link::URL] = url node.content = name else node.content = url end @dirty = true if link.modified? link end
remove_link(type, search)
click to toggle source
# File lib/reapack/index/metadata.rb, line 66 def remove_link(type, search) node = Link.find type, search, @root raise Error, "no such #{type} link '#{search}'" unless node node.remove auto_remove @dirty = true end
replace_links(type) { || ... }
click to toggle source
# File lib/reapack/index/metadata.rb, line 77 def replace_links(type) was_dirty = @dirty old_links = hash_links Link.find_all(type, @root) .each {|node| node.remove } yield new_links = hash_links Link.find_all(type, @root) @dirty = old_links != new_links unless was_dirty end
Private Instance Methods
auto_remove()
click to toggle source
# File lib/reapack/index/metadata.rb, line 141 def auto_remove @root.remove if @root.children.empty? end
hash_links(nodes)
click to toggle source
# File lib/reapack/index/metadata.rb, line 155 def hash_links(nodes) nodes.map {|node| [node[Link::REL], node[Link::URL]] } end
make_root()
click to toggle source
# File lib/reapack/index/metadata.rb, line 132 def make_root unless @root @root = Nokogiri::XML::Node.new TAG, @parent.document @root.parent = @parent end @root end
make_rtf(content)
click to toggle source
# File lib/reapack/index/metadata.rb, line 145 def make_rtf(content) PandocRuby.new(content).to_rtf :standalone, f: :commonmark rescue Errno::ENOENT raise Error, [ "RTF conversion failed because the pandoc executable " \ "cannot be found in your PATH.", "Try again after installing pandoc <http://pandoc.org/>." ].join("\n") end