Skip to content

Instantly share code, notes, and snippets.

@mmb
Created Jan 6, 2009
Embed
What would you like to do?
#!/usr/bin/ruby
# convert embed code generated by some Flash video players into valid markup
# (use object tag instead of deprecated embed tag)
require 'rexml/document'
require 'rubygems'
require 'builder'
class Embed < REXML::Document
def flashvars
xpath_get(['//embed/@flashvars', ])
end
def height
xpath_get(['//embed/@height', ])
end
def src
xpath_get(['//embed/@src', ])
end
def type
xpath_get(['//embed/@type', ])
end
def width
xpath_get(['//embed/@width', ])
end
def to_object
xm = Builder::XmlMarkup.new
xm.object(
:type => type,
:data => src,
:height => height,
:width => width) {
xm.param(:name => 'flashvars', :value => flashvars)
}
end
def xpath_get(xpaths)
result = nil
xpaths.each do |xpath|
n = root.elements[xpath]
unless n.nil?
result = n.value
break
end
end
result
end
end
input = '<embed src="http://www.theonion.com/content/themes/common/assets/videop
layer2/flvplayer.swf" type="application/x-shockwave-flash" allowScriptAccess="al
ways" wmode="transparent" width="400" height="355" flashvars="file=http://www.th
eonion.com/content/xml/92328/video&autostart=false&image=http://www.theonion.com
/content/files/images/NO_KEYBOARD_article.jpg&bufferlength=3&embedded=true&title
=Apple%20Introduces%20Revolutionary%20New%20Laptop%20With%20No%20Keyboard"></emb
ed>'
e = Embed.new(input)
puts "input: #{input}\n\noutput: #{e.to_object}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment