Module: Jekyll::ComponentBase

Includes:
Liquid::StandardFilters
Included in:
ComponentBlock, ComponentTag
Defined in:
lib/jekyll/components/base.rb

Constant Summary collapse

@@compressor =
HtmlCompressor::Compressor.new({
  :remove_comments => true
}).freeze

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

blank? Description: Override’s Liquid’s default blank checker. This allows for templates to be used without passing inner content.

Returns:

  • (Boolean)


38
39
40
# File 'lib/jekyll/components/base.rb', line 38

def blank?
  false
end

#initialize(tag_name, markup, tokens) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll/components/base.rb', line 13

def initialize(tag_name, markup, tokens)
  super

  @attributes = {}
  @context = false
  @context_name = self.name.to_s.gsub("::", "_").downcase
  @content = ''
  @default_selector_attr = []
  @props = Hash.new
  @site = false

  if markup =~ /(#{Liquid::QuotedFragment}+)?/
    # Parse parameters
    # Source: https://gist.github.com/jgatjens/8925165
    markup.scan(Liquid::TagAttributes) do |key, value|
      @attributes[key] = Liquid::Expression.parse(value)
    end
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
  end
end

#render(context) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll/components/base.rb', line 101

def render(context)
  @context = context
  @site = @context.registers[:site]
  @content = super
  serialize_data
  output = template(context)

  if (output.instance_of?(String))
    output = Liquid::Template.parse(output).render()
    output = @@compressor.compress(unindent(output))
  else
    output = ""
  end

  return output
end

#selector_data_props(attr = @attributes) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll/components/base.rb', line 53

def selector_data_props(attr = @attributes)
  template = ""
  attr.keys.each { |key|
    if key.include? "data"
      template += "#{key.gsub("_", "-")}='#{@props[key]}' "
    end
  }

  return template
end

#selector_default_props(attr = @default_selector_attr) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll/components/base.rb', line 42

def selector_default_props(attr = @default_selector_attr)
  template = ""
  attr.each { |prop|
    if @props.key?(prop)
      template += "#{prop}='#{@props[prop]}' "
    end
  }

  return template
end

#selector_props(attr = @default_selector_attr) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/jekyll/components/base.rb', line 64

def selector_props(attr = @default_selector_attr)
  template = ""
  template += selector_data_props
  template += selector_default_props(attr)

  return template
end

#serialize_dataObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jekyll/components/base.rb', line 76

def serialize_data
  data = Hash.new
  @attributes["content"] = @content
  if @attributes.length
    @attributes.each do |key, value|
      val = @context.evaluate(value)
      data[key] = val
    end
  end

  return set_props(data)
end

#set_props(props = Hash.new) ⇒ Object



72
73
74
# File 'lib/jekyll/components/base.rb', line 72

def set_props(props = Hash.new)
  @context[@context_name] = @props = @props.merge(props)
end

#template(context = @context) ⇒ Object



118
119
120
# File 'lib/jekyll/components/base.rb', line 118

def template(context = @context)
  return context
end

#unindent(content) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jekyll/components/base.rb', line 89

def unindent(content)
  # Remove initial whitespace
  content.gsub!(/\A^\s*\n/, "")
  # Remove indentations
  if content =~ %r!^\s*!m
    indentation = Regexp.last_match(0).length
    content.gsub!(/^\ {#{indentation}}/, "")
  end

  return content
end