class GEPUB::Meta

Holds one metadata with refine meta elements.

Constants

REFINERS

Attributes

content[RW]
name[R]

Public Class Methods

new(name, content, parent, attributes= {}, refiners = {}) click to toggle source
# File lib/gepub/meta.rb, line 10
def initialize(name, content, parent, attributes= {}, refiners = {})
  @parent = parent
  @name = name
  @content = content
  @attributes = attributes
  @refiners = refiners
  @parent.register_meta(self) unless @parent.nil?
end

Public Instance Methods

[](x) click to toggle source
# File lib/gepub/meta.rb, line 19
def [](x)
  @attributes[x]
end
[]=(x,y) click to toggle source
# File lib/gepub/meta.rb, line 23
def []=(x,y)
  @attributes[x] = y
end
add_alternates(alternates = {}) click to toggle source

add alternate script refiner.

# File lib/gepub/meta.rb, line 95
def add_alternates(alternates = {})
  alternates.each {
    |locale, content|
    add_refiner('alternate-script', content, { 'xml:lang' => locale })
  }
  self
end
add_refiner(property, content, attributes = {}) click to toggle source

add a refiner.

# File lib/gepub/meta.rb, line 51
def add_refiner(property, content, attributes = {})
  (@refiners[property] ||= []) <<  Meta.new('meta', content, @parent, { 'property' => property }.merge(attributes)) unless content.nil?
  self
end
lang(lang = UNASSIGNED) click to toggle source
# File lib/gepub/meta.rb, line 86
def lang(lang = UNASSIGNED)
  if unassigned?(lang)
    @attributes['xml:lang']
  else
    self.lang=(lang)
  end
end
lang=(lang) click to toggle source
# File lib/gepub/meta.rb, line 82
def lang=(lang)
  @attributes['xml:lang'] = lang
end
list_alternates() click to toggle source
# File lib/gepub/meta.rb, line 103
def list_alternates
  list = refiner_list('alternate-script').map {
    |refiner|
    [ refiner['xml:lang'], refiner.content ]
  }
  Hash[*list.flatten]
end
refine(property, content, attributes = {}) click to toggle source

set a 'unique' refiner. all other refiners with same property will be removed.

# File lib/gepub/meta.rb, line 57
def refine(property, content, attributes = {})
  if !content.nil?
    refiner_clear(property)
    add_refiner(property, content, attributes)
  end
  self
end
refiner(name) click to toggle source
# File lib/gepub/meta.rb, line 41
def refiner(name)
  refiner = @refiners[name]
  if refiner.nil? || refiner.size == 0
    nil
  else
    refiner[0]
  end
end
refiner_clear(name) click to toggle source
# File lib/gepub/meta.rb, line 31
def refiner_clear(name)
  if !@refiners[name].nil?
    @refiners[name].each {
      |refiner|
      @parent.unregister_meta(refiner)
    }
  end
  @refiners[name]= []
end
refiner_list(name) click to toggle source
# File lib/gepub/meta.rb, line 27
def refiner_list(name)
  return @refiners[name].dup
end
to_s(locale=nil) click to toggle source
Calls superclass method
# File lib/gepub/meta.rb, line 138
def to_s(locale=nil)
  localized = nil
  if !locale.nil?
    prefix = locale.sub(/^(.+?)-.*/, '\1')
    regex = Regexp.new("^((" + locale.split('-').join(')?-?(') + ")?)")
    candidates = @refiners['alternate-script'].select {
      |refiner|
      refiner['xml:lang'] =~ /^#{prefix}-?.*/
    }.sort_by {
      |x|
      x['xml:lang'] =~ regex; $1.size
    }.reverse
    localized = candidates[0].content if candidates.size > 0
  end
  (localized || self.content || super()).to_s
end
to_xml(builder, id_pool, ns = nil, additional_attr = {}, opf_version = '3.0') click to toggle source
# File lib/gepub/meta.rb, line 111
def to_xml(builder, id_pool, ns = nil, additional_attr = {}, opf_version = '3.0')
  additional_attr ||= {}
  if @refiners.size > 0 && opf_version.to_f >= 3.0
    @attributes['id'] = id_pool.generate_key(:prefix => name) if @attributes['id'].nil?
  end

  # using __send__ to parametarize Namespace and content.
  target = ns.nil? || @name == 'meta' ? builder : builder[ns]
  attr = @attributes.reject{|_k,v| v.nil?}.merge(additional_attr)
  if @content.nil?
    target.__send__(@name, attr)
  else
    target.__send__(@name, attr, self.to_s)
  end

  if @refiners.size > 0 && opf_version.to_f >= 3.0
    additional_attr['refines'] = "##{@attributes['id']}"
    @refiners.each {
      |_k, ref_list|
      ref_list.each {
        |ref|
        ref.to_xml(builder, id_pool, nil, additional_attr)
      }
    }
  end
end