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
modified?() click to toggle source
# File lib/reapack/index/metadata.rb, line 12
def modified?
  !!@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
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