fn_document 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/Gemfile +13 -0
  3. data/Gemfile.lock +20 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +19 -0
  6. data/Rakefile +53 -0
  7. data/VERSION +1 -0
  8. data/fn_document.gemspec +95 -0
  9. data/lib/fn/block.rb +27 -0
  10. data/lib/fn/document.rb +204 -0
  11. data/lib/fn/migrate.xslt +224 -0
  12. data/lib/fn/node/base.rb +69 -0
  13. data/lib/fn/node/context.rb +70 -0
  14. data/lib/fn/node/root.rb +14 -0
  15. data/lib/fn/pdf/node/begin_document.rb +22 -0
  16. data/lib/fn/pdf/node/begin_page_ext.rb +24 -0
  17. data/lib/fn/pdf/node/create_textflow.rb +137 -0
  18. data/lib/fn/pdf/node/end_page_ext.rb +22 -0
  19. data/lib/fn/pdf/node/fit_image.rb +28 -0
  20. data/lib/fn/pdf/node/fit_pdi_page.rb +26 -0
  21. data/lib/fn/pdf/node/fit_textflow.rb +27 -0
  22. data/lib/fn/pdf/node/invert.rb +36 -0
  23. data/lib/fn/pdf/node/load_image.rb +21 -0
  24. data/lib/fn/pdf/node/open_pdi.rb +22 -0
  25. data/lib/fn/pdf/node/open_pdi_page.rb +31 -0
  26. data/lib/fn/pdf/node/resume_page.rb +21 -0
  27. data/lib/fn/pdf/node/set_parameter.rb +22 -0
  28. data/lib/fn/pdf/node/watermark.rb +24 -0
  29. data/lib/fn/pdf/struct.rb +40 -0
  30. data/lib/fn/pdf/writer.rb +205 -0
  31. data/lib/fn/resource.rb +53 -0
  32. data/lib/fn/swf/node/break.rb +19 -0
  33. data/lib/fn/swf/node/flash.rb +24 -0
  34. data/lib/fn/swf/node/font.rb +24 -0
  35. data/lib/fn/swf/node/frame.rb +19 -0
  36. data/lib/fn/swf/node/hot_spot.rb +43 -0
  37. data/lib/fn/swf/node/image.rb +19 -0
  38. data/lib/fn/swf/node/page.rb +30 -0
  39. data/lib/fn/swf/node/photo_block.rb +53 -0
  40. data/lib/fn/swf/node/text.rb +51 -0
  41. data/lib/fn/swf/struct.rb +43 -0
  42. data/lib/fn/swf/writer.rb +89 -0
  43. data/lib/fn/util.rb +27 -0
  44. data/lib/fn/validation.rng +172 -0
  45. data/lib/fn_document.rb +3 -0
  46. data/test/helper.rb +18 -0
  47. data/test/test_fn_document.rb +7 -0
  48. metadata +150 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Nathan Colgate Clark
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = fn_document
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to fn_document
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Nathan Colgate Clark. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "fn_document"
18
+ gem.homepage = "http://github.com/nathancolgate/fn_document"
19
+ gem.license = "MIT"
20
+ gem.summary = "Gem for handling documents within FASI AMS"
21
+ gem.description = "Gem for handling documents within FASI AMS"
22
+ gem.email = "nathancolgate@gmail.com"
23
+ gem.authors = ["Nathan Colgate Clark","Kyle Maxwell"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "fn_document #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.2
@@ -0,0 +1,95 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fn_document}
8
+ s.version = "0.9.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nathan Colgate Clark", "Kyle Maxwell"]
12
+ s.date = %q{2011-07-14}
13
+ s.description = %q{Gem for handling documents within FASI AMS}
14
+ s.email = %q{nathancolgate@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "fn_document.gemspec",
28
+ "lib/fn/block.rb",
29
+ "lib/fn/document.rb",
30
+ "lib/fn/migrate.xslt",
31
+ "lib/fn/node/base.rb",
32
+ "lib/fn/node/context.rb",
33
+ "lib/fn/node/root.rb",
34
+ "lib/fn/pdf/node/begin_document.rb",
35
+ "lib/fn/pdf/node/begin_page_ext.rb",
36
+ "lib/fn/pdf/node/create_textflow.rb",
37
+ "lib/fn/pdf/node/end_page_ext.rb",
38
+ "lib/fn/pdf/node/fit_image.rb",
39
+ "lib/fn/pdf/node/fit_pdi_page.rb",
40
+ "lib/fn/pdf/node/fit_textflow.rb",
41
+ "lib/fn/pdf/node/invert.rb",
42
+ "lib/fn/pdf/node/load_image.rb",
43
+ "lib/fn/pdf/node/open_pdi.rb",
44
+ "lib/fn/pdf/node/open_pdi_page.rb",
45
+ "lib/fn/pdf/node/resume_page.rb",
46
+ "lib/fn/pdf/node/set_parameter.rb",
47
+ "lib/fn/pdf/node/watermark.rb",
48
+ "lib/fn/pdf/struct.rb",
49
+ "lib/fn/pdf/writer.rb",
50
+ "lib/fn/resource.rb",
51
+ "lib/fn/swf/node/break.rb",
52
+ "lib/fn/swf/node/flash.rb",
53
+ "lib/fn/swf/node/font.rb",
54
+ "lib/fn/swf/node/frame.rb",
55
+ "lib/fn/swf/node/hot_spot.rb",
56
+ "lib/fn/swf/node/image.rb",
57
+ "lib/fn/swf/node/page.rb",
58
+ "lib/fn/swf/node/photo_block.rb",
59
+ "lib/fn/swf/node/text.rb",
60
+ "lib/fn/swf/struct.rb",
61
+ "lib/fn/swf/writer.rb",
62
+ "lib/fn/util.rb",
63
+ "lib/fn/validation.rng",
64
+ "lib/fn_document.rb",
65
+ "test/helper.rb",
66
+ "test/test_fn_document.rb"
67
+ ]
68
+ s.homepage = %q{http://github.com/nathancolgate/fn_document}
69
+ s.licenses = ["MIT"]
70
+ s.require_paths = ["lib"]
71
+ s.rubygems_version = %q{1.6.2}
72
+ s.summary = %q{Gem for handling documents within FASI AMS}
73
+
74
+ if s.respond_to? :specification_version then
75
+ s.specification_version = 3
76
+
77
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
78
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
79
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
80
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
81
+ s.add_development_dependency(%q<rcov>, [">= 0"])
82
+ else
83
+ s.add_dependency(%q<shoulda>, [">= 0"])
84
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
86
+ s.add_dependency(%q<rcov>, [">= 0"])
87
+ end
88
+ else
89
+ s.add_dependency(%q<shoulda>, [">= 0"])
90
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
91
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
92
+ s.add_dependency(%q<rcov>, [">= 0"])
93
+ end
94
+ end
95
+
data/lib/fn/block.rb ADDED
@@ -0,0 +1,27 @@
1
+ module FN
2
+ module Block
3
+ def page_number
4
+ find_first("ancestor::page")["number"].to_i
5
+ end
6
+
7
+ def content?
8
+ content && !content.empty?
9
+ end
10
+
11
+ def text
12
+ self["text"]
13
+ end
14
+
15
+ def src
16
+ self["src"]
17
+ end
18
+
19
+ def index
20
+ self["index"] && self["index"].to_i
21
+ end
22
+
23
+ def sort_key
24
+ "#{text}-#{index}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,204 @@
1
+ require_dependency File.dirname(__FILE__) + '/util'
2
+ require_dependency File.dirname(__FILE__) + '/resource'
3
+ require_dependency File.dirname(__FILE__) + '/block'
4
+ require_dependency File.dirname(__FILE__) + '/pdf/writer'
5
+ require_dependency File.dirname(__FILE__) + '/swf/writer'
6
+
7
+ XML.default_keep_blanks = false
8
+
9
+ module FN
10
+ class ItemNotFoundError < RuntimeError; end
11
+ class Document
12
+ module ClassMethods
13
+ include Util
14
+
15
+ # Creates a new FN::Document from an XML object that is in the old-
16
+ # school (original) style
17
+ def migrate_from(xml)
18
+ raise_unless_xml_doc xml
19
+ @xslt ||= XSLT::Stylesheet.new(
20
+ XML::Document.file(
21
+ File.dirname(__FILE__) + "/migrate.xslt"
22
+ ))
23
+ return FN::Document.new(@xslt.apply(xml))
24
+ end
25
+
26
+ # Reads XML from a file, and returns the FN::Document
27
+ def file(file, options = {})
28
+ new(XML::Document.file(file), options)
29
+ end
30
+
31
+ # Reads XML from a string, and returns the FN::Document
32
+ def string(string, options = {})
33
+ new(XML::Document.string(string), options)
34
+ end
35
+ end
36
+ extend ClassMethods
37
+
38
+ include Util
39
+
40
+ SPACE = /\s+/
41
+
42
+ attr_accessor :resource_root
43
+ attr_reader :xml
44
+
45
+ def initialize(xml, options = {})
46
+ raise_unless_xml_doc xml
47
+ @xml = xml
48
+ update_resources options
49
+ # validate!
50
+ end
51
+
52
+ def update_resources(options = {})
53
+ @resource_root = options[:resource_root] if options[:resource_root]
54
+ mapping = string_keys(options[:resources]) or return
55
+ resources.each do |resource|
56
+ path = mapping[resource.key.to_s]
57
+ resource.path = path if path
58
+ end
59
+ end
60
+
61
+ def menu_item(path)
62
+ xpath = "//" + path.
63
+ split("/").
64
+ map{|label| "item[@label='#{label}']"}.
65
+ join("/")
66
+ @xml.find_first(xpath) or raise ItemNotFoundError.new(xpath + "\n\n" + @xml.to_s)
67
+ end
68
+
69
+ def edit_menu_items
70
+ @xml.find("//item[@label='Edit']/item")
71
+ end
72
+
73
+ def string_keys(hash)
74
+ return nil unless hash
75
+ hash.inject({}) do |m, (k, v)|
76
+ m[k.to_s] = v
77
+ m
78
+ end
79
+ end
80
+
81
+ def remove_menu_items(*args)
82
+ args.flatten.each do |arg|
83
+ remove_menu_item arg
84
+ end
85
+ end
86
+
87
+ def remove_menu_item(name)
88
+ node = @xml.find_first("//item[@label='#{name}']")
89
+ node.remove! unless node.nil?
90
+ #@xml.find("//item[@label='#{name}']").each do |node|
91
+ # node.remove! # mongrels keep dying -> bug caused by finding two items named "Download"
92
+ #end
93
+ end
94
+
95
+ def to_pdf(options = {})
96
+ update_resources(options)
97
+ @pdf ||= PDF::Writer.new
98
+ @pdf.write(self, options)
99
+ end
100
+
101
+ def to_swf(options = {})
102
+ update_resources(options)
103
+ @swf ||= SWF::Writer.new
104
+ @swf.write(self, options)
105
+ end
106
+
107
+ def blocks
108
+ extend_block @xml.find("//block")
109
+ end
110
+
111
+ def text_blocks
112
+ extend_block(@xml.find("//block[@type='text']")).sort_by{|b| b.sort_key }
113
+ end
114
+
115
+ def text_blocks_by(page)
116
+ typed_blocks_by(page, "text")
117
+ end
118
+
119
+ def photo_blocks_by(page)
120
+ typed_blocks_by(page, "photo")
121
+ end
122
+
123
+ def typed_blocks_by(page, type)
124
+ query = "//page[@number='#{page}']//block[@type='#{type}']
125
+ |//block[@type='#{type}' and @multipage='yes']".gsub(SPACE, '')
126
+ extend_block(@xml.find(query)).sort_by{|b| b.sort_key }
127
+ end
128
+
129
+ def photo_blocks
130
+ extend_block @xml.find("//block[@type='photo']")
131
+ end
132
+
133
+ def extend_block(blox)
134
+ blox.map do |blk|
135
+ blk.extend(FN::Block)
136
+ blk
137
+ end
138
+ end
139
+
140
+ def textflows
141
+ @xml.find("//text")
142
+ end
143
+
144
+ def pages
145
+ @xml.find("//page")
146
+ end
147
+
148
+ def dependencies?
149
+ resources.all?{|r| r.complete? }
150
+ end
151
+
152
+ def printable?
153
+ dependencies? && @resource_root
154
+ end
155
+
156
+ def resource_hash
157
+ @resources ||= @xml.find("//resource").inject({}) {|memo, r|
158
+ res = Resource.new(r)
159
+ memo[res.key].delete if memo[res.key]
160
+ memo[res.key] = res
161
+ memo
162
+ }
163
+ end
164
+
165
+ def images
166
+ photos + backgrounds
167
+ end
168
+
169
+ def photos
170
+ resources.select{|r| r.type == "photo"}
171
+ end
172
+
173
+ def backgrounds
174
+ resources.select{|r| r.type == "bkg"}
175
+ end
176
+ alias_method :bkgs, :backgrounds
177
+
178
+ def fonts
179
+ resources.select{|r| r.type == "font"}
180
+ end
181
+
182
+ def resources
183
+ resource_hash.values.sort_by {|x| x.key }
184
+ end
185
+
186
+ def resource(key)
187
+ resource_hash[key] || Resource.new(LibXML::XML::Node.new("anon", key))
188
+ end
189
+
190
+ # Raise unless this document matches an internal schema
191
+ def validate!
192
+ @schema ||= XML::RelaxNG.document(
193
+ XML::Document.file(
194
+ File.dirname(__FILE__) + "/validation.rng"
195
+ ))
196
+ @xml.validate_relaxng(@schema)
197
+ end
198
+
199
+ # String representation of the internal XML document
200
+ def xml_to_s
201
+ @xml.to_s
202
+ end
203
+ end
204
+ end