class FN::Document

Constants

SPACE

Attributes

resource_root[RW]
xml[R]

Public Class Methods

new(xml, options = {}) click to toggle source
# File lib/fn/document.rb, line 45
def initialize(xml, options = {})
  raise_unless_xml_doc xml
  @xml = xml
  update_resources options
  # validate!
end

Public Instance Methods

backgrounds() click to toggle source
# File lib/fn/document.rb, line 173
def backgrounds
  resources.select{|r| r.type == "bkg"}
end
Also aliased as: bkgs
bkgs()
Alias for: backgrounds
blocks() click to toggle source
# File lib/fn/document.rb, line 107
def blocks
  extend_block @xml.find("//block")
end
dependencies?() click to toggle source
# File lib/fn/document.rb, line 148
def dependencies?
  resources.all?{|r| r.complete? }
end
edit_menu_items() click to toggle source
# File lib/fn/document.rb, line 69
def edit_menu_items
  @xml.find("//item[@label='Edit']/item")
end
extend_block(blox) click to toggle source
# File lib/fn/document.rb, line 133
def extend_block(blox)
  blox.map do |blk|
    blk.extend(FN::Block)
    blk
  end
end
fonts() click to toggle source
# File lib/fn/document.rb, line 178
def fonts
 resources.select{|r| r.type == "font"}
end
images() click to toggle source
# File lib/fn/document.rb, line 165
def images
  photos + backgrounds
end
menu_item(path) click to toggle source
pages() click to toggle source
# File lib/fn/document.rb, line 144
def pages
  @xml.find("//page")
end
photo_blocks() click to toggle source
# File lib/fn/document.rb, line 129
def photo_blocks
  extend_block @xml.find("//block[@type='photo']")
end
photo_blocks_by(page) click to toggle source
# File lib/fn/document.rb, line 119
def photo_blocks_by(page)
  typed_blocks_by(page, "photo")
end
photos() click to toggle source
# File lib/fn/document.rb, line 169
def photos
  resources.select{|r| r.type == "photo"}
end
printable?() click to toggle source
# File lib/fn/document.rb, line 152
def printable?
  dependencies? && @resource_root
end
remove_menu_item(name) click to toggle source
# File lib/fn/document.rb, line 87
def remove_menu_item(name)
  node = @xml.find_first("//item[@label='#{name}']")
  node.remove! unless node.nil?
  #@xml.find("//item[@label='#{name}']").each do |node|
  #  node.remove! # mongrels keep dying -> bug caused by finding two items named "Download"
  #end
end
remove_menu_items(*args) click to toggle source
# File lib/fn/document.rb, line 81
def remove_menu_items(*args)
  args.flatten.each do |arg|
    remove_menu_item arg
  end
end
resource(key) click to toggle source
# File lib/fn/document.rb, line 186
def resource(key)
  resource_hash[key] || Resource.new(LibXML::XML::Node.new("anon", key))
end
resource_hash() click to toggle source
# File lib/fn/document.rb, line 156
def resource_hash
  @resources ||= @xml.find("//resource").inject({}) {|memo, r|
    res = Resource.new(r)
    memo[res.key].delete      if memo[res.key]
    memo[res.key] = res
    memo
  }
end
resources() click to toggle source
# File lib/fn/document.rb, line 182
def resources
  resource_hash.values.sort_by {|x| x.key }
end
string_keys(hash) click to toggle source
# File lib/fn/document.rb, line 73
def string_keys(hash)
  return nil unless hash
  hash.inject({}) do |m, (k, v)|
    m[k.to_s] = v
    m
  end
end
text_blocks() click to toggle source
# File lib/fn/document.rb, line 111
def text_blocks
  extend_block(@xml.find("//block[@type='text']")).sort_by{|b| b.sort_key }
end
text_blocks_by(page) click to toggle source
# File lib/fn/document.rb, line 115
def text_blocks_by(page)
  typed_blocks_by(page, "text")
end
textflows() click to toggle source
# File lib/fn/document.rb, line 140
def textflows
  @xml.find("//text")
end
to_pdf(options = {}, debug = false) click to toggle source
# File lib/fn/document.rb, line 95
def to_pdf(options = {}, debug = false)
  update_resources(options)
  @pdf ||= PDF::Writer.new
  @pdf.write(self, options, debug)      
end
to_swf(options = {}) click to toggle source
# File lib/fn/document.rb, line 101
def to_swf(options = {})
  update_resources(options)
  @swf ||= SWF::Writer.new
  @swf.write(self, options)      
end
typed_blocks_by(page, type) click to toggle source
# File lib/fn/document.rb, line 123
def typed_blocks_by(page, type)
  query = "//page[@number='#{page}']//block[@type='#{type}']
          |//block[@type='#{type}' and @multipage='yes']".gsub(SPACE, '')
  extend_block(@xml.find(query)).sort_by{|b| b.sort_key }
end
update_resources(options = {}) click to toggle source
# File lib/fn/document.rb, line 52
def update_resources(options = {})
  @resource_root = options[:resource_root]      if options[:resource_root]
  mapping = string_keys(options[:resources])    or return
  resources.each do |resource|
    path = mapping[resource.key.to_s]
    resource.path = path                        if path
  end
end
validate!() click to toggle source

Raise unless this document matches an internal schema

# File lib/fn/document.rb, line 191
def validate!
  @schema ||= XML::RelaxNG.document(
               XML::Document.file(
               File.dirname(__FILE__) + "/validation.rng"
             ))
  @xml.validate_relaxng(@schema)
end
xml_to_s() click to toggle source

String representation of the internal XML document

# File lib/fn/document.rb, line 200
def xml_to_s
  @xml.to_s
end