Skip to content

Instantly share code, notes, and snippets.

@mmb
Created January 6, 2009 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmb/43933 to your computer and use it in GitHub Desktop.
Save mmb/43933 to your computer and use it in GitHub Desktop.
#!/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