Class: MarkdownVideos::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_videos/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown_text, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



9
10
11
12
13
14
15
# File 'lib/markdown_videos/renderer.rb', line 9

def initialize(markdown_text, options = {})
  @markdown_text = markdown_text
  @options = {
    wrapper: MarkdownVideos.defaults.wrapper,
    class_name: MarkdownVideos.defaults.class_name
  }.merge(options || {})
end

Instance Attribute Details

#markdown_textObject (readonly)

Returns the value of attribute markdown_text.



7
8
9
# File 'lib/markdown_videos/renderer.rb', line 7

def markdown_text
  @markdown_text
end

Instance Method Details

#find_service_for(url) ⇒ Object



36
37
38
# File 'lib/markdown_videos/renderer.rb', line 36

def find_service_for(url)
  MarkdownVideos::SERVICES.find { |service_class| service_class.support(url) }
end

#renderObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/markdown_videos/renderer.rb', line 17

def render
  @markdown_text.gsub(/!\[([^\]]*)\]\(([^)]+)\)/) do
    rendered = nil
    match_data = Regexp.last_match
    markdown = {
      alt_text: match_data[1],
      url: match_data[2].strip
    }

    service_class = find_service_for(markdown[:url])

    if service_class
      service_class.new(markdown[:alt_text], markdown[:url], @options).render
    else
      match_data.string
    end
  end
end