done21-validifier 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. data/LICENSE +22 -0
  2. data/README.rdoc +44 -0
  3. data/Rakefile +16 -0
  4. data/init.rb +1 -0
  5. data/lib/validifier.rb +122 -0
  6. data/validifier.gemspec +37 -0
  7. metadata +83 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Jim Hoskins (Done21)
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = Validifier
2
+
3
+ Validifier allows you to generate valid XHTML flash embed code in ruby.
4
+
5
+ = Usage
6
+
7
+ You can create a flash movie by supplying it with the data manually, or by supplying it with
8
+ existing embed code.
9
+
10
+ == Creating a flash movie manually
11
+
12
+ To create a new flash movie manually, just supply the path or url to your swf file and your options
13
+
14
+ movie = Validifier::FlashMovie.new("http://example.com/index.swf", {:width => 640, :height => 480})
15
+
16
+ movie.to_s # => The embed code for your movie
17
+
18
+ == Creating a flash movie from existing embed source code
19
+
20
+ Validifier can also construct a flash movie from existing (x)html source code. You can access and update
21
+ any of the properties of the movie
22
+
23
+ movie = Validifier::FlashMovie.from_source(existing_source_code_string)
24
+
25
+ # Update any properties of the movie
26
+ movie.width = 640
27
+ movie.height = 480
28
+ movie.params["allowScriptAccess"] = "false"
29
+
30
+ movie.to_s # => The embed code for your movie
31
+
32
+ If you only need to validify existing source code without modifying any propeties, you can use the validify method
33
+
34
+ Validifier::FlashMovie.validifiy(existing_source_code_string) # => valid embed code string
35
+
36
+ = Installation
37
+
38
+ Add github as a gem source (once per machine)
39
+
40
+ gem sources -a http://gems.github.com
41
+
42
+ Install the gem
43
+
44
+ gem install done21-validifier
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('validifier', '0.1.1') do |p|
6
+ p.description = "Generate flash embed code that is valid XHTML"
7
+ p.url = "http://github.com/done21/validifier"
8
+ p.author = "Jim Hoskins"
9
+ p.email = "jim@done21.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ p.runtime_dependencies = ["hpricot", "htmlentities"]
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
16
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'lib/validifier'
data/lib/validifier.rb ADDED
@@ -0,0 +1,122 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'htmlentities'
4
+
5
+ module Validifier
6
+ WIDTH = 425
7
+ HEIGHT = 344
8
+ CODEBASE = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
9
+ CLASSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
10
+
11
+ class FlashMovie
12
+ attr_accessor :source, :width, :height, :params, :codebase, :classid, :comment
13
+
14
+ def initialize(source, options={})
15
+ @source = source
16
+ @width = options[:width] || WIDTH
17
+ @height = options[:height] || HEIGHT
18
+ @codebase = options[:codebase] ||
19
+ @classid = options[:classid] ||
20
+ @params = options[:params] || {}
21
+ @comment = options[:comment] || ""
22
+
23
+ @coder = HTMLEntities.new
24
+ end
25
+
26
+ # nodoc
27
+ def clean(str)
28
+ @coder.encode(@coder.decode(str.to_s))
29
+ end
30
+
31
+ # The valid XHTML markup for this flash movie
32
+ def to_s
33
+ p = params.map {|k|}
34
+ output = %Q{<!--[if !IE]> -->
35
+ <object type="application/x-shockwave-flash" data="#{clean(source)}" width="#{clean(width)}" height="#{clean(height)}">
36
+ <!-- <![endif]-->
37
+ <!--[if IE]>
38
+ <object classid="#{clean(classid)}" codebase="#{clean(codebase)}" width="#{clean(width)}" height="#{clean(height)}">
39
+ <param name="movie" value="#{clean(source)}" />
40
+ <!--><!-- #{comment} -->
41
+ }
42
+ params.each do |name, value|
43
+ output << %Q{ <param name="#{clean(name)}" value="#{clean(value)}" />\n}
44
+ end
45
+ output + " </object>\n<!-- <![endif]-->"
46
+ end
47
+
48
+ class <<self
49
+
50
+ # Build a FlashMovie based on chunk of HTML (with object and/or embed code)
51
+ def from_source(html_str)
52
+ html = Hpricot(html_str)
53
+ if object = (html/:object).first
54
+ movie = scrape_object(object)
55
+ elsif embed = (html/:embed).first
56
+ movie = scrape_embed(embed)
57
+ else
58
+ return nil
59
+ end
60
+ movie
61
+ end
62
+
63
+ # Convert html to valid XHTML
64
+ def validify(html_str)
65
+ FlashMovie.from_source(html_str).to_s
66
+ end
67
+
68
+ private
69
+ def scrape_object(object)
70
+ movie_param = (object/"param[@name='movie']").first
71
+ source = movie_param ? movie_param[:value] : object[:data]
72
+
73
+ options = {}
74
+ options[:width] = object[:width]
75
+ options[:height] = object[:height]
76
+ options[:classid] = object[:classid]
77
+ options[:codebase] = object[:codebase]
78
+ options[:params] = {}
79
+
80
+ skip_params = %W(movie wmode)
81
+ (object/:param).each do |param|
82
+ next if skip_params.include?(param[:name].to_s)
83
+ options[:params][param[:name]] = param[:value]
84
+ end
85
+
86
+ set_dimensions_from_style object[:style], options
87
+
88
+ FlashMovie.new(source, options)
89
+ end
90
+
91
+ def scrape_embed(embed)
92
+ source = embed[:src]
93
+
94
+ options = {}
95
+ options[:width] = embed[:width]
96
+ options[:height] = embed[:height]
97
+ options[:params] = {}
98
+
99
+ skip_attrs = %W(wmode id src style movie)
100
+ embed.attributes.each do |name, value|
101
+ next if skip_attrs.include? name
102
+ options[:params][name] = value
103
+ end
104
+
105
+ set_dimensions_from_style embed[:style], options
106
+
107
+ FlashMovie.new(source, options)
108
+ end
109
+
110
+ def set_dimensions_from_style style, options
111
+ return unless style
112
+ width = (/width\s*:\s*(\d+(em|ex|px|%|in|cm|pt|pc)|0)\s*;?/i).match(style)
113
+ options[:width] = width[1] if width[1]
114
+
115
+ height = (/height\s*:\s*(\d+(em|ex|px|%|in|cm|pt|pc)|0)\s*;?/i).match(style)
116
+ options[:width] = height[1] if height[1]
117
+ options
118
+ end
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{validifier}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jim Hoskins"]
9
+ s.date = %q{2009-01-18}
10
+ s.description = %q{Generate flash embed code that is valid XHTML}
11
+ s.email = %q{jim@done21.com}
12
+ s.extra_rdoc_files = ["lib/validifier.rb", "LICENSE", "README.rdoc"]
13
+ s.files = ["init.rb", "lib/validifier.rb", "LICENSE", "Manifest", "Rakefile", "README.rdoc", "validifier.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/done21/validifier}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Validifier", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{validifier}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Generate flash embed code that is valid XHTML}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<hpricot>, [">= 0"])
28
+ s.add_runtime_dependency(%q<htmlentities>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<hpricot>, [">= 0"])
31
+ s.add_dependency(%q<htmlentities>, [">= 0"])
32
+ end
33
+ else
34
+ s.add_dependency(%q<hpricot>, [">= 0"])
35
+ s.add_dependency(%q<htmlentities>, [">= 0"])
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: done21-validifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jim Hoskins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-18 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: htmlentities
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: Generate flash embed code that is valid XHTML
34
+ email: jim@done21.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - lib/validifier.rb
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - init.rb
45
+ - lib/validifier.rb
46
+ - LICENSE
47
+ - Manifest
48
+ - Rakefile
49
+ - README.rdoc
50
+ - validifier.gemspec
51
+ has_rdoc: true
52
+ homepage: http://github.com/done21/validifier
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --line-numbers
56
+ - --inline-source
57
+ - --title
58
+ - Validifier
59
+ - --main
60
+ - README.rdoc
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "1.2"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project: validifier
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: Generate flash embed code that is valid XHTML
82
+ test_files: []
83
+