refactor videos regex to be less insane

This commit is contained in:
Brandon Mathis 2015-01-11 11:05:24 -06:00
parent c72c3cd2a4
commit 1708b99a06

View File

@ -22,33 +22,39 @@ module Jekyll
@width = '' @width = ''
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
if markup =~ /((https?:\/\/|\/)\S+)(\s+((https?:\/\/|\/)\S+))?(\s+((https?:\/\/|\/)\S+))?(\s+(\d+)\s(\d+))?(\s+((https?:\/\/|\/)\S+))?/i @videos = markup.scan(/((https?:\/\/|\/)\S+\.(webm|ogv|mp4)\S*)/i).map(&:first).compact
@video = [$1, $4, $7].compact @poster = markup.scan(/((https?:\/\/|\/)\S+\.(png|gif|jpe?g)\S*)/i).map(&:first).compact.first
@width = $10 @sizes = markup.scan(/\s(\d\S+)/i).map(&:first).compact
@height = $11
@poster = $13
end
super super
end end
def render(context) def render(context)
output = super output = super
type = { types = {
'mp4' => "type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'", '.mp4' => "type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'",
'ogv' => "type='video/ogg; codecs=theora, vorbis'", '.ogv' => "type='video/ogg; codecs=theora, vorbis'",
'webm' => "type='video/webm; codecs=vp8, vorbis'" '.webm' => "type='video/webm; codecs=vp8, vorbis'"
} }
if @video.size > 0 if @videos.size > 0
video = "<video width='#{@width}' height='#{@height}' preload='none' controls poster='#{@poster}'>" video = "<video #{sizes} preload='metadata' controls #{poster}>"
@video.each do |v| @videos.each do |v|
t = v.match(/([^\.]+)$/)[1] video << "<source src='#{v}' #{types[File.extname(v)]}>"
video += "<source src='#{v}' #{type[t]}>"
end end
video += "</video>" video += "</video>"
else else
"Error processing input, expected syntax: {% video url/to/video [url/to/video] [url/to/video] [width height] [url/to/poster] %}" "Error processing input, expected syntax: {% video url/to/video [url/to/video] [url/to/video] [width height] [url/to/poster] %}"
end end
end end
def poster
"poster='#{@poster}'" if @poster
end
def sizes
attrs = "width='#{@sizes[0]}'" if @sizes[0]
attrs += " height='#{@sizes[1]}'" if @sizes[1]
attrs
end
end end
end end