markdown_videos 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: d1f9e1dcb78017d103441d5c63b0da0ede87775d
4
+ data.tar.gz: d6b03551a6f6aea6de4f739b1f9d5fb2becffdb3
5
+ SHA512:
6
+ metadata.gz: 171c683b7328db61a5448b482eeaa2643c9cc25d9b22cbd37b471c8f5e0556a9d9a57ef008b115dd8e8761d4de5eb1906f46f8b1a07b23c4b32172accc591e89
7
+ data.tar.gz: facc284b37332c4ae24bc000bb4a2ab519e5270b9f81689794c7f5193359af89350fdb85fdb11f0b3173eb76de2ad3305c562160902f1a905b15a9882a809b15
@@ -0,0 +1,21 @@
1
+ require "markdown_videos/renderer"
2
+ require "markdown_videos/services"
3
+ require "markdown_videos/configuration"
4
+
5
+ module MarkdownVideos
6
+
7
+ def self.render(string, options = {})
8
+ MarkdownVideos::Renderer.new(string, options).render
9
+ end
10
+
11
+ def self.configure
12
+ @configuration = Configuration.new
13
+ yield(@configuration) if block_given?
14
+ @configuration
15
+ end
16
+
17
+ def self.defaults
18
+ @configuration || configure
19
+ end
20
+
21
+ end
@@ -0,0 +1,26 @@
1
+ module MarkdownVideos
2
+
3
+ class Configuration
4
+
5
+ attr_accessor :wrapper,
6
+ :classname
7
+
8
+ # if defined, needs to contain `%s` where HTML markup should be included
9
+ # e.g.: WRAPPER = '<p class="embed-responsive embed-responsive-16by9">%s</p>'.freeze
10
+ def wrapper
11
+ @wrapper || nil
12
+ end
13
+
14
+ def wrapper=(wrapper)
15
+ raise "Wrapper needs to include `%s`" unless wrapper.nil? || wrapper['%s']
16
+ @wrapper = wrapper
17
+ end
18
+
19
+ # extra classname(s) added to embed HTML markup
20
+ def classname
21
+ @classname || nil
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,56 @@
1
+ require "addressable/uri"
2
+
3
+ module MarkdownVideos
4
+
5
+ class Renderer
6
+
7
+ def initialize(string, options = {})
8
+ @string = string
9
+ @options = {
10
+ wrapper: MarkdownVideos.defaults.wrapper,
11
+ classname: MarkdownVideos.defaults.classname
12
+ }.merge(options || {})
13
+ end
14
+
15
+ def render
16
+ MarkdownVideos::SERVICES.keys.each do |service|
17
+ @string = render_service(service)
18
+ end
19
+ @string
20
+ end
21
+
22
+ private
23
+
24
+ def render_service(service)
25
+ values = MarkdownVideos::SERVICES[service]
26
+ @string.gsub(values[:regexp]) do |match|
27
+ match_data = Regexp.last_match
28
+ id = match_data[3]
29
+ title = match_data[1]
30
+ url = Addressable::URI.parse(match_data[2])
31
+ url_parameters = url.query_values || {}
32
+
33
+ html_markup = sprintf(values[:markup],
34
+ CGI::escapeHTML(title),
35
+ service_url(service, id, url_parameters),
36
+ @options[:classname].nil? ? "" : " class=\"#{@options[:classname]}\"")
37
+
38
+ if @options[:wrapper].nil?
39
+ html_markup
40
+ else
41
+ sprintf(@options[:wrapper], html_markup)
42
+ end
43
+ end
44
+ end
45
+
46
+ def service_url(service, id, url_parameters = {})
47
+ url = sprintf(MarkdownVideos::SERVICES[service][:url], id)
48
+ uri = Addressable::URI.parse(url)
49
+ url_parameters.select! { |k, v| MarkdownVideos::SERVICES[service][:url_parameters].include?(k.to_sym) && (!v.nil? || !v.empty?) } if url_parameters
50
+ uri.query_values = url_parameters unless url_parameters.nil? || url_parameters.empty?
51
+ uri.to_s
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,33 @@
1
+ module MarkdownVideos
2
+
3
+ #
4
+ # Supported services list
5
+ #
6
+ # regexp: Regexp must capture in 1) title, 2) full url and 3) video id
7
+ # markup: HTML markup rendered with first %s being altertive text and second %s being the video URL (iFrame src)
8
+ # and third %s being class attribute declaration location (make sure there is only one space after, not before)
9
+ # url: URL of service included in the markup, with first %s corresponding to video's ID
10
+ # url_parameters: List of authorized URL parameters
11
+ #
12
+
13
+ SERVICES = {
14
+
15
+ # http://rubular.com/r/8Xb4s9ukXy
16
+ youtube: {
17
+ regexp: /^!\[([^\]]+)\]\((https?:\/\/(?:youtu.be\/|www.youtube.com\/(?:watch\?v=|embed\/))([\w\-]{4,16})\??([\w\-\=]+)?)\)$/,
18
+ markup: '<iframe width="560" height="315" title="%s" src="%s"%s frameborder="0" allowfullscreen></iframe>',
19
+ url: "https://www.youtube.com/embed/%s",
20
+ url_parameters: [:start]
21
+ },
22
+
23
+ # http://rubular.com/r/Upo2dmGbIJ
24
+ vimeo: {
25
+ regexp: /^!\[([^\]]+)\]\((https?:\/\/(?:(?:player\.)?vimeo.com\/(?:video\/)?)([0-9]{4,16})\??([\w\-\=]+)?)\)$/,
26
+ markup: '<iframe title="%s" src="%s" width="500" height="281"%s frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>',
27
+ url: "https://player.vimeo.com/video/%s",
28
+ url_parameters: []
29
+ }
30
+
31
+ }.freeze
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module MarkdownVideos
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1 @@
1
+ require "markdown_videos/base"
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_videos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Capripot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.37'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.37'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ description: Markdown syntax video parser
56
+ email: capripot@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/markdown_videos.rb
62
+ - lib/markdown_videos/base.rb
63
+ - lib/markdown_videos/configuration.rb
64
+ - lib/markdown_videos/renderer.rb
65
+ - lib/markdown_videos/services.rb
66
+ - lib/markdown_videos/version.rb
67
+ homepage: https://github.com/capripot/markdown_videos
68
+ licenses:
69
+ - LGPL-3.0
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A gem that provides ability to parse any string containing image markdown
91
+ syntax into appropriate video HTML embed based on URL.
92
+ test_files: []
93
+ has_rdoc: false