Module: Convert::Converters
- Included in:
- Convert
- Defined in:
- lib/convert.rb,
lib/converters/ted.rb,
lib/converters/gist.rb,
lib/converters/vimeo.rb,
lib/converters/decode.rb,
lib/converters/encode.rb,
lib/converters/flickr.rb,
lib/converters/hashtag.rb,
lib/converters/twitter.rb,
lib/converters/youtube.rb,
lib/converters/kramdown.rb,
lib/converters/liveleak.rb,
lib/converters/markdown.rb,
lib/converters/metacafe.rb,
lib/converters/nokogiri.rb,
lib/converters/sanitize.rb,
lib/converters/auto_link.rb,
lib/converters/image_tag.rb,
lib/converters/instagram.rb,
lib/converters/redcarpet.rb,
lib/converters/simpleidn.rb,
lib/converters/worldstar.rb,
lib/converters/soundcloud.rb,
lib/converters/dailymotion.rb,
lib/converters/google_maps.rb,
lib/converters/html_escape.rb,
lib/converters/video_embed.rb,
lib/converters/vimeo_embed.rb,
lib/converters/email_escape.rb,
lib/converters/iframe_embed.rb,
lib/converters/strip_params.rb,
lib/converters/simple_format.rb,
lib/converters/unescape_html.rb,
lib/converters/youtube_embed.rb,
lib/converters/youtube_image.rb,
lib/converters/facebook_embed.rb,
lib/converters/youtube_js_api.rb
Overview
Autoload for faster loading
Constant Summary collapse
- SKIP =
['a', 'pre', 'code', 'kbd', 'script', 'iframe', 'img', 'link']
Instance Method Summary collapse
-
#auto_link(string, options = {}) ⇒ Object
Convert URL to html link.
-
#convert(string, options = {}) ⇒ Object
Loop converters and convert.
-
#convertable?(node) ⇒ Boolean
Checks if a node is convertable by scanning the parents.
- #dailymotion(string, options = {}) ⇒ Object
- #decode(string, options = {}) ⇒ Object
-
#email_escape(string, options = {}) ⇒ Object
Remove stuff from email body that is going to be stripped anyway.
- #encode(string, options = {}) ⇒ Object
-
#escape_html(string, options = {}) ⇒ Object
Escape html.
-
#facebook_embed(string, options = {}) ⇒ Object
Embed vimeo by id.
- #flickr(string, options = {}) ⇒ Object
- #gist(string, options = {}) ⇒ Object
- #google_maps(string, options = {}) ⇒ Object
- #hashtag(string, options = {}) ⇒ Object
-
#iframe_embed(url, options = {}) ⇒ Object
Iframe embed code.
-
#image_tag(src, options = {}) ⇒ Object
Convert image to img html tag.
- #instagram(string, options = {}) ⇒ Object
- #kramdown(string, options = {}) ⇒ Object
- #liveleak(string, options = {}) ⇒ Object
-
#markdown(string, options = {}) ⇒ Object
Converts the string into html.
-
#metacafe(string, options = {}) ⇒ Object
Convert metacafe movie URL to embedded html.
-
#redcarpet(string, options = {}) ⇒ Object
Markown converter.
-
#sanitize(string, options = {}) ⇒ Object
Built in configs: :custom, :full, :linebreaks, :simple, :restricted, :basic, :relaxed.
-
#scan(string, options = {}) ⇒ Object
Scan a string with Nokogiri and convert if match string.
-
#simple_format(string, options = {}) ⇒ Object
Convert newlines to br tags.
-
#soundcloud(string, options = {}) ⇒ Object
Convert soundcloud player URL to embedded iframe.
-
#strip_params(url) ⇒ Object
Remove query params.
-
#ted(string, options = {}) ⇒ Object
Convert ted movie URL to embedded iframe.
-
#to_ascii(string) ⇒ Object
Convert chinese characters to URL safe format.
-
#to_unicode(string) ⇒ Object
Reverse safe format back to unicode.
-
#twitter(string, options = {}) ⇒ Object
Convert twitter URL to embedded html.
- #unescape_html(string, options = {}) ⇒ Object
-
#video_embed(string, options = {}) ⇒ Object
Vimeo and Youtube embed markdown extension.
-
#vimeo(string, options = {}) ⇒ Object
Convert vimeo movie URL to embedded iframe.
-
#vimeo_embed(string, options = {}) ⇒ Object
Embed vimeo by id.
-
#worldstar(string, options = {}) ⇒ Object
Convert worldstar movie URL to embedded html.
-
#youtube(string, options = {}) ⇒ Object
Convert youtube movie URL to embedded iframe.
-
#youtube_embed(string, options = {}) ⇒ Object
Embed youtube by id.
- #youtube_image(string, options = {}) ⇒ Object
- #youtube_js_api(string, options = {}) ⇒ Object
Instance Method Details
#auto_link(string, options = {}) ⇒ Object
Convert URL to html link
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/converters/auto_link.rb', line 5 def auto_link(string, = {}) = { :mode => :all, :link_attr => nil, :skip_tags => nil, :strip => false }.merge() Rinku.auto_link(string, [:mode], [:link_attr], [:skip_tags]) do |url| # Remove query options (default false) url = strip_params(url) if [:strip] url end end |
#convert(string, options = {}) ⇒ Object
Loop converters and convert
24 25 26 27 |
# File 'lib/converters/nokogiri.rb', line 24 def convert(string, = {}) [:converters].each{|c| string = send(c, string)} string end |
#convertable?(node) ⇒ Boolean
Checks if a node is convertable by scanning the parents
30 31 32 33 34 35 |
# File 'lib/converters/nokogiri.rb', line 30 def convertable?(node) while(node = node.parent) do return false if SKIP.include?(node.name) end true end |
#dailymotion(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/converters/dailymotion.rb', line 4 def dailymotion(string, = {}) # http://www.dailymotion.com/video/x3gpxwp_first-person-view-of-a-downhill-ice-cross-course-red-bull-crashed-ice-2015_sport # Original 480 360 = {:width => 590, :height => 335}.merge() @regex = /http:\/\/www\.dailymotion\.com.*\/video\/(.+)_*/ string.gsub(@regex) do video_id = $1 %{<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/#{video_id}&related=0" width="#{options[:width]}" height="#{options[:height]}"><param name="movie" value="http://www.dailymotion.com/swf/#{video_id}&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><a href="http://www.dailymotion.com/video/#{video_id}?embed=1"><img src="http://www.dailymotion.com/thumbnail/video/#{video_id}" width="#{options[:width]}" height="#{options[:height]}"/></a></object>} end end |
#decode(string, options = {}) ⇒ Object
4 5 6 |
# File 'lib/converters/decode.rb', line 4 def decode(string, = {}) HTMLEntities.new.decode(string) end |
#email_escape(string, options = {}) ⇒ Object
Remove stuff from email body that is going to be stripped anyway.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/converters/email_escape.rb', line 5 def email_escape(string, = {}) # No options at the moment = {}.merge() # Youtube videos @regex = /<iframe.+src=['"].+\/embed\/(.+)[?].+['"].+iframe>/ string = string.gsub(@regex, "https://youtu.be/#{'\1'}") # Vimeo videos # Example: https://vimeo.com/59437462 @regex = /<iframe.+src=['"]\/\/player\.vimeo.com\/video\/(.+)[?]{1}.+['"].+iframe>/ string = string.gsub(@regex, "https://vimeo.com/#{'\1'}") string end |
#encode(string, options = {}) ⇒ Object
4 5 6 |
# File 'lib/converters/encode.rb', line 4 def encode(string, = {}) HTMLEntities.new.encode(string) end |
#escape_html(string, options = {}) ⇒ Object
Escape html
5 6 7 8 9 10 |
# File 'lib/converters/html_escape.rb', line 5 def escape_html(string, = {}) = {:map => {'&' => '&', '>' => '>', '<' => '<', '"' => '"' }}.merge() @regex = /[&"><]/ string.gsub(@regex){|m| [:map][m]} end |
#facebook_embed(string, options = {}) ⇒ Object
Embed vimeo by id
5 6 7 8 9 |
# File 'lib/converters/facebook_embed.rb', line 5 def (string, = {}) = {:width => 590, :height => 335}.merge() %{<iframe src="https://www.facebook.com/plugins/video.php?href=https://www.facebook.com/facebook/videos/#{string}/&width=#{options[:width]}&show_text=false&height=#{options[:height]}" width="#{options[:width]}" height="#{options[:height]}" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>} end |
#flickr(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/converters/flickr.rb', line 4 def flickr(string, = {}) # https://www.flickr.com/photos/fotokunstsusanne/23160248869 = {:maxwidth => nil, :maxheight => nil, :link_options => {}}.merge() @regex = %r{https?://(www\.)?flickr\.com/photos/[^\s<]*} string.gsub(@regex) do |match| params = { :url => match, :format => "json" } [:maxwidth, :maxheight].each{|p| params[p] = [p] unless [p].nil? or ![p] > 0} uri = URI("http://www.flickr.com/services/oembed") uri.query = URI.encode_www_form(params) response = JSON.parse(Net::HTTP.get(uri)) = Array([:link_options]).reject { |k,v| v.nil? }.map { |k, v| %{#{k}="#{REXML::Text::normalize(v)}"} }.join(' ') %{<a href="#{match}"#{ ' ' + link_options unless link_options.empty? }><img src="#{response["url"]}" alt="#{response["title"]}" title="#{response["title"]}" /></a>} end end |
#gist(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/converters/gist.rb', line 4 def gist(string, = {}) # https://gist.github.com/1710276 # No options at the moment = {}.merge() @regex = %r{https?://gist\.github\.com/(\w+/)?(\d+)} string.gsub(@regex) do gist_id = $2 %{<script src="https://gist.github.com/#{gist_id}.js"></script>} end end |
#google_maps(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/converters/google_maps.rb', line 4 def google_maps(string, = {}) = { :width => 420, :height => 315, :style => "color:#000;text-align:left", :link_text => "View Larger Map", :show_info => true, :type => :normal, :zoom => 18, :more => '' }.merge() map_type = {:normal => '&t=m', :satellite => '&t=k', :terrain => '&t=p', :hybrid => '&t=h'} @regex = /(https?):\/\/maps\.google\.([a-z\.]+)\/maps\?(.*)/ string.gsub(@regex) do domain_country = $2 map_query = $3 width = [:width] height = [:height] style = [:style] link_text = [:link_text] type = [:type].to_sym = ([:show_info] ? '' : '&iwloc=near') << map_type[type] if map_type.has_key?(type) << "&z=#{options[:zoom]}" << [:more] %{<iframe width="#{width}" height="#{height}" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="//maps.google.#{domain_country}/maps?f=q&source=s_q&#{map_query}&output=embed#{map_options}"></iframe><br><small><a href="//maps.google.#{domain_country}/maps?f=q&source=embed&#{map_query}" style="#{style}">#{link_text}</a></small>} end end |
#hashtag(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/converters/hashtag.rb', line 4 def hashtag(string, = {}) = {:source => :twitter}.merge() @regex = /#([^\s]+)/ if [:source] == :twitter string.gsub(@regex, '<a href="http://twitter.com/search?q=%23\1&f=realtime" class="hashtag" target="_blank">#\1</a>') elsif [:source] == :facebook string.gsub(@regex, '<a href="https://www.facebook.com/hashtag/\1" class="hashtag" target="_blank">#\1</a>') end end |
#iframe_embed(url, options = {}) ⇒ Object
Iframe embed code
5 6 7 8 9 |
# File 'lib/converters/iframe_embed.rb', line 5 def (url, = {}) = {:width => 231, :height => 436, :scrolling => 'no', :frameborder => 0}.merge() %{<iframe frameborder="#{options[:frameborder]}" scrolling="#{options[:scrolling]}" height="#{options[:height]}" width="#{options[:width]}" src="#{url}"></iframe>} end |
#image_tag(src, options = {}) ⇒ Object
Convert image to img html tag
5 6 7 8 |
# File 'lib/converters/image_tag.rb', line 5 def image_tag(src, = {}) = {:alt => ''}.merge() %{<img src="#{src}" alt="#{options[:alt]}">} end |
#instagram(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/converters/instagram.rb', line 4 def instagram(string, = {}) = {:height => 714, :width => 616}.merge() @regex = %r{https?:\/\/(www.)?instagr(am\.com|\.am)/p/.+} string.gsub(@regex) do string += '/' unless string.end_with?('/') %{<iframe src="#{string}embed" height="#{options[:height]}" width="#{options[:width]}" frameborder="0" scrolling="no"></iframe>} end end |
#kramdown(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/converters/kramdown.rb', line 4 def kramdown(string, = {}) = { :auto_ids => false, :entity_output => :as_char, :enable_coderay => true, :parse_block_html => true, :parse_span_html => true, :smart_quotes => ['apos', 'apos', 'quot', 'quot'] }.merge() Kramdown::Document.new(string, ).to_html end |
#liveleak(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/converters/liveleak.rb', line 4 def liveleak(string, = {}) = { :width => 420, :height => 315, :frameborder => 0, :wmode => nil, :autoplay => false, :hide_related => false }.merge() @regex = %r{http://www\.liveleak\.com/(?:ll_embed|view)\?.=(\w+)} string.gsub(@regex) do a = [] a << "wmode=#{options[:wmode]}" if [:wmode] a << "autoplay=1" if [:autoplay] a << "rel=0" if [:hide_related] src = "http://www.liveleak.com/ll_embed?f=#{$1}" src += "?#{a.join '&'}" unless a.empty? %{<iframe width="#{options[:width]}" height="#{options[:height]}" src="#{src}" frameborder="#{options[:frameborder]}" allowfullscreen></iframe>} end end |
#markdown(string, options = {}) ⇒ Object
Converts the string into html
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/converters/markdown.rb', line 5 def markdown(string, = {}) = { :autolink => true, :fenced_code_blocks => true, :disable_indented_code_blocks => true, :no_intra_emphasis => true }.merge() Redcarpet::Markdown.new( Redcarpet::Render::HTML.new(:filter_html => false, :hard_wrap => true), ).render(string) end |
#metacafe(string, options = {}) ⇒ Object
Convert metacafe movie URL to embedded html
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/converters/metacafe.rb', line 5 def (string, = {}) # Original 440 272 = { :width => 590, :height => 335, :show_stats => false, :autoplay => false }.merge() @regex = /http:\/\/www\.metacafe\.com\/watch\/([A-Za-z0-9._%-]*)\/([A-Za-z0-9._%-]*)(\/)?/ string.gsub(@regex) do = $1 = $2 width = [:width] height = [:height] show_stats = [:show_stats] ? "showStats=yes" : "showStats=no" autoplay = [:autoplay] ? "autoPlay=yes" : "autoPlay=no" flash_vars = [show_stats, autoplay].join("|") %{<div style="background:#000000;width:#{width}px;height:#{height}px"><embed flashVars="playerVars=#{flash_vars}" src="http://www.metacafe.com/fplayer/#{metacafe_id}/#{metacafe_slug}.swf" width="#{width}" height="#{height}" wmode="transparent" allowFullScreen="true" allowScriptAccess="always" name="Metacafe_#{metacafe_id}" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></div>} end end |
#redcarpet(string, options = {}) ⇒ Object
Markown converter
5 6 7 8 9 |
# File 'lib/converters/redcarpet.rb', line 5 def redcarpet(string, = {}) = {:renderer => Redcarpet::Render::HTML, :markdown_options => {}}.merge() Redcarpet::Markdown.new([:renderer], [:markdown_options]).render(string) end |
#sanitize(string, options = {}) ⇒ Object
Built in configs: :custom, :full, :linebreaks, :simple, :restricted, :basic, :relaxed
6 7 8 9 10 11 |
# File 'lib/converters/sanitize.rb', line 6 def sanitize(string, = {}) return string if [:config] == false = {:config => nil}.merge() config = Object.const_get("Sanitize::Config::#{options[:config].to_s.upcase}") Sanitize.fragment(string, config || {}) end |
#scan(string, options = {}) ⇒ Object
Scan a string with Nokogiri and convert if match string
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/converters/nokogiri.rb', line 7 def scan(string, = {}) return string if ![:converters] or [:converters].empty? doc = Nokogiri::HTML.fragment(string) doc.search('.//text()').each do |el| t = el.text if t.strip.size > 0 t = convert(t, ) if convertable?(el) end el.replace(t) end doc.to_html end |
#simple_format(string, options = {}) ⇒ Object
Convert newlines to br tags
5 6 7 8 |
# File 'lib/converters/simple_format.rb', line 5 def simple_format(string, = {}) = {:config => :simple}.merge() sanitize(string.gsub(%r{(\r\n|\n|\r)}, '<br>'), ) end |
#soundcloud(string, options = {}) ⇒ Object
Convert soundcloud player URL to embedded iframe
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/converters/soundcloud.rb', line 5 def soundcloud(string, = {}) = { :width => '100%', :height => 166, :auto_play => false, :theme_color => '00FF00', :color => '915f33', :show_comments => false, :show_artwork => false }.merge() @regex = /(https?:\/\/)?(www.)?soundcloud\.com\/\S*/ begin string.gsub(@regex) do |match| new_uri = match.to_s new_uri = (new_uri =~ /^https?\:\/\/.*/) ? URI(new_uri) : URI("http://#{new_uri}") new_uri.normalize! width = [:width] height = [:height] auto_play = [:auto_play] theme_color = [:theme_color] color = [:color] show_artwork = [:show_artwork] show_comments = [:show_comments] %{<iframe width="#{width}" height="#{height}" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=#{new_uri}&show_artwork=#{show_artwork}&show_comments=#{show_comments}&auto_play=#{auto_play}&color=#{color}&theme_color=#{theme_color}"></iframe> } end rescue URI::InvalidURIError string end end |
#strip_params(url) ⇒ Object
Remove query params
5 6 7 8 9 |
# File 'lib/converters/strip_params.rb', line 5 def strip_params(url) uri = URI.parse(URI.encode(url.strip)) uri.query = nil uri.to_s end |
#ted(string, options = {}) ⇒ Object
Convert ted movie URL to embedded iframe
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/converters/ted.rb', line 5 def ted(string, = {}) # Original: 640 360 = { :width => 590, :height => 335, :scrolling => 'no', :frameborder => 0, :allow_full_screen => false }.merge() @regex = /https?:\/\/(www.|embed.)?ted\.com\/talks\/([A-Za-z0-9._%-]*)\.html((\?|#)\S+)?/ string.gsub(@regex) do ted_page = $2 width = [:width] height = [:height] frameborder = [:frameborder] scrolling = [:scrolling] allow_full_screen = [:allow_full_screen] %{<iframe width="#{width}" height="#{height}" frameborder="#{frameborder}" scrolling="#{scrolling}" src="http://embed.ted.com/talks/#{ted_page}.html"#{allow_full_screen ? ' webkitAllowFullScreen mozallowfullscreen allowFullScreen' : ''}></iframe>} end end |
#to_ascii(string) ⇒ Object
Convert chinese characters to URL safe format
5 6 7 |
# File 'lib/converters/simpleidn.rb', line 5 def to_ascii(string) SimpleIDN.to_ascii(string) end |
#to_unicode(string) ⇒ Object
Reverse safe format back to unicode
10 11 12 |
# File 'lib/converters/simpleidn.rb', line 10 def to_unicode(string) SimpleIDN.to_unicode(string) end |
#twitter(string, options = {}) ⇒ Object
Convert twitter URL to embedded html
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/converters/twitter.rb', line 5 def twitter(string, = {}) @regex = %r{(?<!href=")https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+(/?)} string.gsub(@regex) do |match| params = {:url => match}.merge() uri = URI("https://api.twitter.com/1/statuses/oembed.json") uri.query = URI.encode_www_form(params) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true response = JSON.parse(http.get(uri.request_uri).body) response['html'] end end |
#unescape_html(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/converters/unescape_html.rb', line 4 def unescape_html(string, = {}) @regex = /(<.*?[a-z]{0,12}?>)+/i string.gsub(@regex) do |tag| tag.gsub("<", "<").gsub(">", ">") end end |
#video_embed(string, options = {}) ⇒ Object
Vimeo and Youtube embed markdown extension
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/converters/video_embed.rb', line 5 def (string, = {}) @regex = /\?\[(youtube|vimeo)\]\(https?:\/\/(www\.)?(vimeo\.com\/|youtu\.be\/|youtube\.com\/watch\?v=)(\S+)\)/ string.scan(@regex).each do |type, prefix, url, id| r = if('vimeo' == type) () elsif('youtube' == type) () else nil end # Substitute the original text with the embedded version # OLD, optional https not supported: # @text.gsub!("?[#{type}](https://#{prefix}#{url}#{id})", r) if r x = %r{\?\[#{type}\]\(https?:\/\/#{Regexp.escape(prefix)}#{Regexp.escape(url)}#{id}\)}i string.gsub!(x, r) if r end string end |
#vimeo(string, options = {}) ⇒ Object
Convert vimeo movie URL to embedded iframe
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/converters/vimeo.rb', line 5 def vimeo(string, = {}) # Original: 440 248 = { :width => 590, :height => 335, :show_title => false, :show_byline => false, :show_portrait => false, :allow_fullscreen => true }.merge() @regex = /https?:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/ string.gsub(@regex) do vimeo_id = $2 width = [:width] height = [:height] show_title = "title=0" unless [:show_title] = "byline=0" unless [:show_byline] show_portrait = "portrait=0" unless [:show_portrait] allow_fullscreen = " allowfullscreen" if [:allow_fullscreen] frameborder = [:frameborder] || 0 query_string_variables = [show_title, , show_portrait].compact.join("&") query_string = "?" + query_string_variables unless query_string_variables.empty? %{<iframe src="//player.vimeo.com/video/#{vimeo_id}#{query_string}" width="#{width}" height="#{height}" frameborder="#{frameborder}"#{allow_fullscreen}></iframe>} end end |
#vimeo_embed(string, options = {}) ⇒ Object
Embed vimeo by id
5 6 7 8 9 |
# File 'lib/converters/vimeo_embed.rb', line 5 def (string, = {}) = {:width => 590, :height => 335}.merge() %{<iframe src="https://player.vimeo.com/video/#{string}?title=0&byline=0&portrait=0" width="#{options[:width]}" height="#{options[:height]}"></iframe>} end |
#worldstar(string, options = {}) ⇒ Object
Convert worldstar movie URL to embedded html
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/converters/worldstar.rb', line 5 def worldstar(string, = {}) # Original: 448 374 = {:width => 590, :height => 335}.merge() @regex = /http:\/\/www\.worldstarhiphop\.com\/videos\/video\.php\?v\=(wshh[A-Za-z0-9]+)/ string.gsub(@regex) do video_id = $1 width = [:width] height = [:height] %{<object width="#{width}" height="#{height}"><param name="movie" value="http://www.worldstarhiphop.com/videos/e/16711680/#{video_id}"><param name="allowFullScreen" value="true"></param><embed src="http://www.worldstarhiphop.com/videos/e/16711680/#{video_id}" type="application/x-shockwave-flash" allowFullscreen="true" width="#{width}" height="#{height}"></embed></object>} end end |
#youtube(string, options = {}) ⇒ Object
Convert youtube movie URL to embedded iframe
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/converters/youtube.rb', line 5 def youtube(string, = {}) # Original: 420 315 = { :width => 590, :height => 335, :frameborder => 0, :wmode => 'transparent', :autoplay => false, :hide_related => true, :fs => true, :modestbranding => true, :allow_fullscreen => true }.merge() @regex = /(https?:\/\/)?(www.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/watch\?feature=player_embedded&v=)([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?/ string.gsub(@regex) do youtube_id = $4 src = "https://www.youtube.com/embed/#{youtube_id}" width = [:width] height = [:height] allow_fullscreen = " allowfullscreen" if [:allow_fullscreen] frameborder = [:frameborder] a = [] a << "wmode=#{options[:wmode]}" if [:wmode] a << "autoplay=1" if [:autoplay] a << "rel=0" if [:hide_related] a << "modestbranding=1" if [:modestbranding] a << "fs=1" if [:fs] src += "?#{a.join '&'}" unless a.empty? %{<iframe width="#{width}" height="#{height}" src="#{src}" frameborder="#{frameborder}"#{allow_fullscreen}></iframe>} end end |
#youtube_embed(string, options = {}) ⇒ Object
Embed youtube by id
5 6 7 8 9 |
# File 'lib/converters/youtube_embed.rb', line 5 def (string, = {}) = {:width => 590, :height => 335}.merge() %{<iframe src="https://www.youtube.com/embed/#{string}?wmode=transparent&modestbranding=1&fs=1&rel=0" allowfullscreen="1" webkitallowfullscreen="1" mozallowfullscreen="1" width="#{options[:width]}" height="#{options[:height]}" frameborder="0"></iframe>} end |
#youtube_image(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/converters/youtube_image.rb', line 4 def youtube_image(string, = {}) = { :width => 320, :height => 315, :style => 'medium', :target => 'blank', :border => '0' }.merge() styles = { 'default' => 'default', 'high' => 'hqdefault', 'medium' => 'mqdefault', 'normal' => 'sddefault', 'max' => 'maxresdefault' } @regex = /(https?:\/\/)?(www.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/watch\?feature=player_embedded&v=)([A-Za-z0-9_-]*)(\&\S+)?(\?\S+)?/ string.gsub(@regex) do youtube_id = $4 video_url = "https://www.youtube.com/watch?v=#{youtube_id}" target = [:target] width = [:width] height = [:height] border = [:border] style = styles[[:style]] rescue styles['default'] src = "//img.youtube.com/vi/#{youtube_id}/#{style}.jpg" %{<div class="thumbnail youtube"><a href="#{video_url}" target="_#{target}"><img src="#{src}" width="#{width}" height="#{height}" border="#{border}"></a></div>} end end |
#youtube_js_api(string, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/converters/youtube_js_api.rb', line 4 def youtube_js_api(string, = {}) # Original: 390 250 = {:width => 590, :height => 335}.merge() @regex = /https?:\/\/(www.)?youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)(\&\S+)?/ string.gsub(@regex) do youtube_id = $2 width = [:width] height = [:height] %{<object width="#{width}" height="#{height}"><param name="movie" value="//www.youtube.com/v/#{youtube_id}?enablejsapi=1&playerapiid=ytplayer"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/#{youtube_id}?enablejsapi=1&playerapiid=ytplayer" type="application/x-shockwave-flash" wmode="transparent" width="#{width}" height="#{height}" allowscriptaccess="always"></embed></object>} end end |