convert 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/convert.gemspec +2 -2
  4. data/lib/convert.rb +15 -21
  5. data/lib/converters/auto_link.rb +16 -14
  6. data/lib/converters/dailymotion.rb +12 -10
  7. data/lib/converters/decode.rb +6 -4
  8. data/lib/converters/email_escape.rb +16 -14
  9. data/lib/converters/encode.rb +6 -4
  10. data/lib/converters/flickr.rb +16 -14
  11. data/lib/converters/gist.rb +12 -10
  12. data/lib/converters/google_maps.rb +30 -28
  13. data/lib/converters/hashtag.rb +11 -9
  14. data/lib/converters/html_escape.rb +9 -7
  15. data/lib/converters/iframe_embed.rb +8 -6
  16. data/lib/converters/image_tag.rb +8 -6
  17. data/lib/converters/instagram.rb +10 -8
  18. data/lib/converters/kramdown.rb +14 -12
  19. data/lib/converters/liveleak.rb +22 -20
  20. data/lib/converters/markdown.rb +16 -14
  21. data/lib/converters/metacafe.rb +23 -21
  22. data/lib/converters/nokogiri.rb +27 -25
  23. data/lib/converters/redcarpet.rb +8 -6
  24. data/lib/converters/sanitize.rb +10 -8
  25. data/lib/converters/simple_format.rb +8 -6
  26. data/lib/converters/soundcloud.rb +31 -29
  27. data/lib/converters/strip_params.rb +9 -7
  28. data/lib/converters/ted.rb +23 -21
  29. data/lib/converters/twitter.rb +15 -13
  30. data/lib/converters/unescape_html.rb +8 -6
  31. data/lib/converters/video_embed.rb +20 -18
  32. data/lib/converters/vimeo.rb +28 -26
  33. data/lib/converters/vimeo_embed.rb +8 -6
  34. data/lib/converters/worldstar.rb +14 -12
  35. data/lib/converters/youtube.rb +40 -38
  36. data/lib/converters/youtube_embed.rb +8 -6
  37. data/lib/converters/youtube_image.rb +30 -28
  38. data/lib/converters/youtube_js_api.rb +13 -11
  39. data/mod.rb +13 -0
  40. metadata +3 -3
  41. data/test.rb +0 -16
@@ -1,16 +1,18 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- def kramdown(string, options = {})
4
- options = {
5
- :auto_ids => false,
6
- :entity_output => :as_char,
7
- :enable_coderay => true,
8
- :parse_block_html => true,
9
- :parse_span_html => true,
10
- :smart_quotes => ['apos', 'apos', 'quot', 'quot']
11
- }.merge(options)
4
+ def kramdown(string, options = {})
5
+ options = {
6
+ :auto_ids => false,
7
+ :entity_output => :as_char,
8
+ :enable_coderay => true,
9
+ :parse_block_html => true,
10
+ :parse_span_html => true,
11
+ :smart_quotes => ['apos', 'apos', 'quot', 'quot']
12
+ }.merge(options)
12
13
 
13
- Kramdown::Document.new(string, options).to_html
14
- end
14
+ Kramdown::Document.new(string, options).to_html
15
+ end
15
16
 
17
+ end
16
18
  end
@@ -1,28 +1,30 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- def liveleak(string, options = {})
4
- options = {
5
- :width => 420,
6
- :height => 315,
7
- :frameborder => 0,
8
- :wmode => nil,
9
- :autoplay => false,
10
- :hide_related => false
11
- }.merge(options)
4
+ def liveleak(string, options = {})
5
+ options = {
6
+ :width => 420,
7
+ :height => 315,
8
+ :frameborder => 0,
9
+ :wmode => nil,
10
+ :autoplay => false,
11
+ :hide_related => false
12
+ }.merge(options)
12
13
 
13
- @regex = %r{http://www\.liveleak\.com/(?:ll_embed|view)\?.=(\w+)}
14
+ @regex = %r{http://www\.liveleak\.com/(?:ll_embed|view)\?.=(\w+)}
14
15
 
15
- string.gsub(@regex) do
16
- a = []
17
- a << "wmode=#{options[:wmode]}" if options[:wmode]
18
- a << "autoplay=1" if options[:autoplay]
19
- a << "rel=0" if options[:hide_related]
16
+ string.gsub(@regex) do
17
+ a = []
18
+ a << "wmode=#{options[:wmode]}" if options[:wmode]
19
+ a << "autoplay=1" if options[:autoplay]
20
+ a << "rel=0" if options[:hide_related]
20
21
 
21
- src = "http://www.liveleak.com/ll_embed?f=#{$1}"
22
- src += "?#{a.join '&'}" unless a.empty?
22
+ src = "http://www.liveleak.com/ll_embed?f=#{$1}"
23
+ src += "?#{a.join '&'}" unless a.empty?
23
24
 
24
- %{<iframe width="#{options[:width]}" height="#{options[:height]}" src="#{src}" frameborder="#{options[:frameborder]}" allowfullscreen></iframe>}
25
+ %{<iframe width="#{options[:width]}" height="#{options[:height]}" src="#{src}" frameborder="#{options[:frameborder]}" allowfullscreen></iframe>}
26
+ end
25
27
  end
26
- end
27
28
 
29
+ end
28
30
  end
@@ -1,18 +1,20 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Converts the string into html
4
- def markdown(string, options = {})
5
- options = {
6
- :autolink => true,
7
- :fenced_code_blocks => true,
8
- :disable_indented_code_blocks => true,
9
- :no_intra_emphasis => true
10
- }.merge(options)
4
+ # Converts the string into html
5
+ def markdown(string, options = {})
6
+ options = {
7
+ :autolink => true,
8
+ :fenced_code_blocks => true,
9
+ :disable_indented_code_blocks => true,
10
+ :no_intra_emphasis => true
11
+ }.merge(options)
11
12
 
12
- Redcarpet::Markdown.new(
13
- Redcarpet::Render::HTML.new(:filter_html => false, :hard_wrap => true),
14
- options
15
- ).render(string)
16
- end
13
+ Redcarpet::Markdown.new(
14
+ Redcarpet::Render::HTML.new(:filter_html => false, :hard_wrap => true),
15
+ options
16
+ ).render(string)
17
+ end
17
18
 
19
+ end
18
20
  end
@@ -1,28 +1,30 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Convert metacafe movie URL to embedded html
4
- def metacafe(string, options = {})
5
- # Original 440 272
6
- options = {
7
- :width => 590,
8
- :height => 335,
9
- :show_stats => false,
10
- :autoplay => false
11
- }.merge(options)
4
+ # Convert metacafe movie URL to embedded html
5
+ def metacafe(string, options = {})
6
+ # Original 440 272
7
+ options = {
8
+ :width => 590,
9
+ :height => 335,
10
+ :show_stats => false,
11
+ :autoplay => false
12
+ }.merge(options)
12
13
 
13
- @regex = /http:\/\/www\.metacafe\.com\/watch\/([A-Za-z0-9._%-]*)\/([A-Za-z0-9._%-]*)(\/)?/
14
+ @regex = /http:\/\/www\.metacafe\.com\/watch\/([A-Za-z0-9._%-]*)\/([A-Za-z0-9._%-]*)(\/)?/
14
15
 
15
- string.gsub(@regex) do
16
- metacafe_id = $1
17
- metacafe_slug = $2
18
- width = options[:width]
19
- height = options[:height]
20
- show_stats = options[:show_stats] ? "showStats=yes" : "showStats=no"
21
- autoplay = options[:autoplay] ? "autoPlay=yes" : "autoPlay=no"
22
- flash_vars = [show_stats, autoplay].join("|")
16
+ string.gsub(@regex) do
17
+ metacafe_id = $1
18
+ metacafe_slug = $2
19
+ width = options[:width]
20
+ height = options[:height]
21
+ show_stats = options[:show_stats] ? "showStats=yes" : "showStats=no"
22
+ autoplay = options[:autoplay] ? "autoPlay=yes" : "autoPlay=no"
23
+ flash_vars = [show_stats, autoplay].join("|")
23
24
 
24
- %{<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>}
25
+ %{<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>}
26
+ end
25
27
  end
26
- end
27
28
 
29
+ end
28
30
  end
@@ -1,36 +1,38 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- SKIP = ['a', 'pre', 'code', 'kbd', 'script', 'iframe', 'img', 'link']
4
+ SKIP = ['a', 'pre', 'code', 'kbd', 'script', 'iframe', 'img', 'link']
4
5
 
5
- # Scan a string with Nokogiri and convert if match string
6
- def scan(string, options = {})
7
- return string if options[:converters].empty?
6
+ # Scan a string with Nokogiri and convert if match string
7
+ def scan(string, options = {})
8
+ return string if options[:converters].empty?
8
9
 
9
- doc = Nokogiri::HTML.fragment(string)
10
+ doc = Nokogiri::HTML.fragment(string)
10
11
 
11
- doc.search('.//text()').each do |el|
12
- t = el.text
13
- if t.strip.size > 0
14
- t = convert(t, options) if convertable?(el)
12
+ doc.search('.//text()').each do |el|
13
+ t = el.text
14
+ if t.strip.size > 0
15
+ t = convert(t, options) if convertable?(el)
16
+ end
17
+ el.replace(t)
15
18
  end
16
- el.replace(t)
17
- end
18
19
 
19
- doc.to_html
20
- end
20
+ doc.to_html
21
+ end
21
22
 
22
- # Loop converters and convert
23
- def convert(string, options = {})
24
- options[:converters].each{|c| string = send(c, string)}
25
- string
26
- end
23
+ # Loop converters and convert
24
+ def convert(string, options = {})
25
+ options[:converters].each{|c| string = send(c, string)}
26
+ string
27
+ end
27
28
 
28
- # Checks if a node is convertable by scanning the parents
29
- def convertable?(node)
30
- while(node = node.parent) do
31
- return false if SKIP.include?(node.name)
29
+ # Checks if a node is convertable by scanning the parents
30
+ def convertable?(node)
31
+ while(node = node.parent) do
32
+ return false if SKIP.include?(node.name)
33
+ end
34
+ true
32
35
  end
33
- true
34
- end
35
36
 
37
+ end
36
38
  end
@@ -1,10 +1,12 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Markown converter
4
- def redcarpet(string, options = {})
5
- options = {:renderer => Redcarpet::Render::HTML, :markdown_options => {}}.merge(options)
4
+ # Markown converter
5
+ def redcarpet(string, options = {})
6
+ options = {:renderer => Redcarpet::Render::HTML, :markdown_options => {}}.merge(options)
6
7
 
7
- Redcarpet::Markdown.new(options[:renderer], options[:markdown_options]).render(string)
8
- end
8
+ Redcarpet::Markdown.new(options[:renderer], options[:markdown_options]).render(string)
9
+ end
9
10
 
11
+ end
10
12
  end
@@ -1,13 +1,15 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- CONFIGS = [:custom, :full, :linebreaks, :simple, :restricted, :basic, :relaxed]
4
+ CONFIGS = [:custom, :full, :linebreaks, :simple, :restricted, :basic, :relaxed]
4
5
 
5
- def sanitize(string, options = {})
6
- return string if options[:config] == false
7
- options = {:config => nil}.merge(options)
6
+ def sanitize(string, options = {})
7
+ return string if options[:config] == false
8
+ options = {:config => nil}.merge(options)
8
9
 
9
- config = Object.const_get("Sanitize::Config::#{options[:config].to_s.upcase}") if CONFIGS.include?(options[:config])
10
- Sanitize.fragment(string, config || {})
11
- end
10
+ config = Object.const_get("Sanitize::Config::#{options[:config].to_s.upcase}") if CONFIGS.include?(options[:config])
11
+ Sanitize.fragment(string, config || {})
12
+ end
12
13
 
14
+ end
13
15
  end
@@ -1,9 +1,11 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Convert newlines to br tags
4
- def simple_format(string, options = {})
5
- options = {:config => :simple}.merge(options)
6
- sanitize(string.gsub(%r{(\r\n|\n|\r)}, '<br>'), options)
7
- end
4
+ # Convert newlines to br tags
5
+ def simple_format(string, options = {})
6
+ options = {:config => :simple}.merge(options)
7
+ sanitize(string.gsub(%r{(\r\n|\n|\r)}, '<br>'), options)
8
+ end
8
9
 
10
+ end
9
11
  end
@@ -1,36 +1,38 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Convert soundcloud player URL to embedded iframe
4
- def soundcloud(string, options = {})
5
- options = {
6
- :width => '100%',
7
- :height => 166,
8
- :auto_play => false,
9
- :theme_color => '00FF00',
10
- :color => '915f33',
11
- :show_comments => false,
12
- :show_artwork => false
13
- }.merge(options)
4
+ # Convert soundcloud player URL to embedded iframe
5
+ def soundcloud(string, options = {})
6
+ options = {
7
+ :width => '100%',
8
+ :height => 166,
9
+ :auto_play => false,
10
+ :theme_color => '00FF00',
11
+ :color => '915f33',
12
+ :show_comments => false,
13
+ :show_artwork => false
14
+ }.merge(options)
14
15
 
15
- @regex = /(https?:\/\/)?(www.)?soundcloud\.com\/\S*/
16
+ @regex = /(https?:\/\/)?(www.)?soundcloud\.com\/\S*/
16
17
 
17
- begin
18
- string.gsub(@regex) do |match|
19
- new_uri = match.to_s
20
- new_uri = (new_uri =~ /^https?\:\/\/.*/) ? URI(new_uri) : URI("http://#{new_uri}")
21
- new_uri.normalize!
22
- width = options[:width]
23
- height = options[:height]
24
- auto_play = options[:auto_play]
25
- theme_color = options[:theme_color]
26
- color = options[:color]
27
- show_artwork = options[:show_artwork]
28
- show_comments = options[:show_comments]
29
- %{<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> }
18
+ begin
19
+ string.gsub(@regex) do |match|
20
+ new_uri = match.to_s
21
+ new_uri = (new_uri =~ /^https?\:\/\/.*/) ? URI(new_uri) : URI("http://#{new_uri}")
22
+ new_uri.normalize!
23
+ width = options[:width]
24
+ height = options[:height]
25
+ auto_play = options[:auto_play]
26
+ theme_color = options[:theme_color]
27
+ color = options[:color]
28
+ show_artwork = options[:show_artwork]
29
+ show_comments = options[:show_comments]
30
+ %{<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> }
31
+ end
32
+ rescue URI::InvalidURIError
33
+ string
30
34
  end
31
- rescue URI::InvalidURIError
32
- string
33
35
  end
34
- end
35
36
 
37
+ end
36
38
  end
@@ -1,10 +1,12 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Remove query params
4
- def strip_params(url)
5
- uri = URI.parse(URI.encode(url.strip))
6
- uri.query = nil
7
- uri.to_s
8
- end
4
+ # Remove query params
5
+ def strip_params(url)
6
+ uri = URI.parse(URI.encode(url.strip))
7
+ uri.query = nil
8
+ uri.to_s
9
+ end
9
10
 
11
+ end
10
12
  end
@@ -1,28 +1,30 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Convert ted movie URL to embedded iframe
4
- def ted(string, options = {})
5
- # Original: 640 360
6
- options = {
7
- :width => 590,
8
- :height => 335,
9
- :scrolling => 'no',
10
- :frameborder => 0,
11
- :allow_full_screen => false
12
- }.merge(options)
4
+ # Convert ted movie URL to embedded iframe
5
+ def ted(string, options = {})
6
+ # Original: 640 360
7
+ options = {
8
+ :width => 590,
9
+ :height => 335,
10
+ :scrolling => 'no',
11
+ :frameborder => 0,
12
+ :allow_full_screen => false
13
+ }.merge(options)
13
14
 
14
- @regex = /https?:\/\/(www.|embed.)?ted\.com\/talks\/([A-Za-z0-9._%-]*)\.html((\?|#)\S+)?/
15
+ @regex = /https?:\/\/(www.|embed.)?ted\.com\/talks\/([A-Za-z0-9._%-]*)\.html((\?|#)\S+)?/
15
16
 
16
- string.gsub(@regex) do
17
- ted_page = $2
18
- width = options[:width]
19
- height = options[:height]
20
- frameborder = options[:frameborder]
21
- scrolling = options[:scrolling]
22
- allow_full_screen = options[:allow_full_screen]
17
+ string.gsub(@regex) do
18
+ ted_page = $2
19
+ width = options[:width]
20
+ height = options[:height]
21
+ frameborder = options[:frameborder]
22
+ scrolling = options[:scrolling]
23
+ allow_full_screen = options[:allow_full_screen]
23
24
 
24
- %{<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>}
25
+ %{<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>}
26
+ end
25
27
  end
26
- end
27
28
 
29
+ end
28
30
  end
@@ -1,21 +1,23 @@
1
- module Converters
1
+ module Convert
2
+ module Converters
2
3
 
3
- # Convert twitter URL to embedded html
4
- def twitter(string, options = {})
5
- @regex = %r{(?<!href=")https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+(/?)}
4
+ # Convert twitter URL to embedded html
5
+ def twitter(string, options = {})
6
+ @regex = %r{(?<!href=")https://twitter\.com(/#!)?/[A-Za-z0-9_]{1,15}/status(es)?/\d+(/?)}
6
7
 
7
- string.gsub(@regex) do |match|
8
- params = {:url => match}.merge(options)
8
+ string.gsub(@regex) do |match|
9
+ params = {:url => match}.merge(options)
9
10
 
10
- uri = URI("https://api.twitter.com/1/statuses/oembed.json")
11
- uri.query = URI.encode_www_form(params)
11
+ uri = URI("https://api.twitter.com/1/statuses/oembed.json")
12
+ uri.query = URI.encode_www_form(params)
12
13
 
13
- http = Net::HTTP.new(uri.host, uri.port)
14
- http.use_ssl = true
14
+ http = Net::HTTP.new(uri.host, uri.port)
15
+ http.use_ssl = true
15
16
 
16
- response = JSON.parse(http.get(uri.request_uri).body)
17
- response["html"]
17
+ response = JSON.parse(http.get(uri.request_uri).body)
18
+ response["html"]
19
+ end
18
20
  end
19
- end
20
21
 
22
+ end
21
23
  end