Class: FN::Document

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Util
Defined in:
lib/fn/document.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SPACE =
/\s+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

file, migrate_from, string

Methods included from Util

#raise_unless_xml_doc, #raise_unless_xml_node

Constructor Details

#initialize(xml, options = {}) ⇒ Document

Returns a new instance of Document.



45
46
47
48
49
50
# File 'lib/fn/document.rb', line 45

def initialize(xml, options = {})
  raise_unless_xml_doc xml
  @xml = xml
  update_resources options
  # validate!
end

Instance Attribute Details

#resource_rootObject

Returns the value of attribute resource_root.



42
43
44
# File 'lib/fn/document.rb', line 42

def resource_root
  @resource_root
end

#xmlObject (readonly)

Returns the value of attribute xml.



43
44
45
# File 'lib/fn/document.rb', line 43

def xml
  @xml
end

Instance Method Details

#backgroundsObject Also known as: bkgs



173
174
175
# File 'lib/fn/document.rb', line 173

def backgrounds
  resources.select{|r| r.type == "bkg"}
end

#blocksObject



107
108
109
# File 'lib/fn/document.rb', line 107

def blocks
  extend_block @xml.find("//block")
end

#dependencies?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/fn/document.rb', line 148

def dependencies?
  resources.all?{|r| r.complete? }
end

#edit_menu_itemsObject



69
70
71
# File 'lib/fn/document.rb', line 69

def edit_menu_items
  @xml.find("//item[@label='Edit']/item")
end

#extend_block(blox) ⇒ Object



133
134
135
136
137
138
# File 'lib/fn/document.rb', line 133

def extend_block(blox)
  blox.map do |blk|
    blk.extend(FN::Block)
    blk
  end
end

#fontsObject



178
179
180
# File 'lib/fn/document.rb', line 178

def fonts
 resources.select{|r| r.type == "font"}
end

#imagesObject



165
166
167
# File 'lib/fn/document.rb', line 165

def images
  photos + backgrounds
end


61
62
63
64
65
66
67
# File 'lib/fn/document.rb', line 61

def menu_item(path)
  xpath = "//" + path.
                  split("/").
                  map{|label| "item[@label='#{label}']"}.
                  join("/")
  @xml.find_first(xpath) or raise ItemNotFoundError.new(xpath + "\n\n" + @xml.to_s)
end

#pagesObject



144
145
146
# File 'lib/fn/document.rb', line 144

def pages
  @xml.find("//page")
end

#photo_blocksObject



129
130
131
# File 'lib/fn/document.rb', line 129

def photo_blocks
  extend_block @xml.find("//block[@type='photo']")
end

#photo_blocks_by(page) ⇒ Object



119
120
121
# File 'lib/fn/document.rb', line 119

def photo_blocks_by(page)
  typed_blocks_by(page, "photo")
end

#photosObject



169
170
171
# File 'lib/fn/document.rb', line 169

def photos
  resources.select{|r| r.type == "photo"}
end

#printable?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/fn/document.rb', line 152

def printable?
  dependencies? && @resource_root
end

#remove_menu_item(name) ⇒ Object



87
88
89
90
91
92
93
# 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) ⇒ Object



81
82
83
84
85
# 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) ⇒ Object



186
187
188
# File 'lib/fn/document.rb', line 186

def resource(key)
  resource_hash[key] || Resource.new(LibXML::XML::Node.new("anon", key))
end

#resource_hashObject



156
157
158
159
160
161
162
163
# 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

#resourcesObject



182
183
184
# File 'lib/fn/document.rb', line 182

def resources
  resource_hash.values.sort_by {|x| x.key }
end

#string_keys(hash) ⇒ Object



73
74
75
76
77
78
79
# 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_blocksObject



111
112
113
# 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) ⇒ Object



115
116
117
# File 'lib/fn/document.rb', line 115

def text_blocks_by(page)
  typed_blocks_by(page, "text")
end

#textflowsObject



140
141
142
# File 'lib/fn/document.rb', line 140

def textflows
  @xml.find("//text")
end

#to_pdf(options = {}, debug = false) ⇒ Object



95
96
97
98
99
# 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 = {}) ⇒ Object



101
102
103
104
105
# 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) ⇒ Object



123
124
125
126
127
# 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 = {}) ⇒ Object



52
53
54
55
56
57
58
59
# 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!Object

Raise unless this document matches an internal schema



191
192
193
194
195
196
197
# 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_sObject

String representation of the internal XML document



200
201
202
# File 'lib/fn/document.rb', line 200

def xml_to_s
  @xml.to_s
end