Class: CS50::VideoTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-theme-cs50.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ VideoTag


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jekyll-theme-cs50.rb', line 82

def initialize(tag_name, text, tokens)
  super
  if text =~ /^https?:\/\/(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
    @v = $1
    components = {
      rel: "0",
      showinfo: "0"
    }
    params = CGI::parse(URI::parse(text.strip).query || "")
    ["autoplay", "end", "index", "list", "start", "t"].each do |param|
        if params.key?(param)
          components[param] = params[param].first
        end
    end
    @src = URI::HTTPS.build(:host => "www.youtube.com", :path => "/embed/#{@v}", :query => URI.encode_www_form(components))
  end
end

Instance Method Details

#render(context) ⇒ Object


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll-theme-cs50.rb', line 100

def render(context)
  if @v and @src
    <<~EOT
      <div class="embed-responsive embed-responsive-16by9">
          <iframe allowfullscreen class="border embed-responsive-item" src="#{@src}" style="background-image: url('https://img.youtube.com/vi/#{@v}/sddefault.jpg'); background-repeat: no-repeat; background-size: cover;"></iframe>
      </div>
    EOT
  else
    <<~EOT
      <p><img alt="static" class="border" data-video src="https://i.imgur.com/xnZ5A2u.gif"></p>
    EOT
  end
end