Module: Jekyll::Autoembed

Defined in:
lib/jekyll/autoembed.rb,
lib/jekyll/autoembed/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.autoembed(content) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll/autoembed.rb', line 30

def self.autoembed(content)
  # TODO: replace this with more flexible stacked
  # autoembed processors (this first one being a Gist processor)
  # once another autoembed requirement comes up
  return content unless content.include?('gist.github.com')
  content.gsub(
    %r{(https?://gist\.github\.com/[a-zA-Z0-9/]+)},
    '<script src="\1.js"></script>'
  )
end

.execute(doc) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jekyll/autoembed.rb', line 6

def self.execute(doc)

  if doc.output.include? "<body"
    parsed_doc = Nokogiri::HTML::Document.parse(doc.output)
    body = parsed_doc.at_css('body')
    replace_nodes(body)
  else 
    parsed_doc = Nokogiri::HTML::DocumentFragment.parse(doc.output)
    replace_nodes(parsed_doc)
  end

  doc.output = parsed_doc.to_html
  doc
end

.replace_nodes(parsed_doc) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/jekyll/autoembed.rb', line 21

def self.replace_nodes(parsed_doc)
  parsed_doc.search('.//text()').each do |node|
    content = node.to_html
    html = autoembed(content)
    next if html == content
    node.replace(html)
  end
end