Class: Jekyll::CodePen

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-codepen.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, content, tokens) ⇒ CodePen

Returns a new instance of CodePen.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll-codepen.rb', line 7

def initialize(tag_name, content, tokens)
  super

  @defaults = {
    'class' => 'codepen',
    'embed_version' => 2,
    'height' =>  300,
    'preview' =>  false,
    'theme_id' =>  11_473,
    'default_tab' =>  'result'
  }

  @args = content.split

  @url = @args.first
  @slug = @url.match(%r{codepen.io\/\w*\/pen\/(?<slug>\w*)})[:slug]
  @user = @url.match(%r{codepen.io\/(?<user>\w*)\/})[:user]
end

Instance Method Details

#attr_map(attrs) ⇒ Object



33
34
35
36
37
38
# File 'lib/jekyll-codepen.rb', line 33

def attr_map(attrs)
  attrs.map do |key, value|
    key = key.to_s.tr('_', '-')
    "data-#{key}=\"#{value}\" "
  end.join('')
end

#parse_args(args) ⇒ Object



26
27
28
29
30
31
# File 'lib/jekyll-codepen.rb', line 26

def parse_args(args)
  args[1..-1].map do |arg|
    k, v = arg.split('=')
    { k => v }
  end.flatten.first
end

#render(context) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jekyll-codepen.rb', line 40

def render(context)
  config = context.registers[:site].config['codepen'] || {}
  attrs = @defaults.merge(config)

  args = parse_args(@args)

  attrs = attrs.merge(args) if args

  class_name = attrs['class']
  attrs.delete('class')

  tag = "<p #{attr_map(attrs)} data-user=\"#{@user}\" data-slug-hash=\"#{@slug}\" class=\"#{class_name}\">" +
        "See the <a href=\"#{@url}\">pen</a> on <a href=\"//codepen.io\">CodePen</a>." +
        "</p>"

  script = "<script async src=\"https://production-assets.codepen.io/assets/embed/ei.js\"></script>"

  tag + script
end