Module: Glim::LiquidFilters

Defined in:
lib/liquid_ext.rb

Instance Method Summary collapse

Instance Method Details

#absolute_url(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/liquid_ext.rb', line 31

def absolute_url(path)
  unless path.nil?
    site, page = URI(@context['site']['url']), URI(@context['page']['url'])
    host, port = @context['site']['host'], @context['site']['port']
    if page.relative? || (site.host == host && site.port == port)
      site.merge(URI(path)).to_s
    else
      page.merge(URI(path)).to_s
    end
  end
end

#cgi_escape(input) ⇒ Object



27
28
29
# File 'lib/liquid_ext.rb', line 27

def cgi_escape(input)
  CGI.escape(input) unless input.nil?
end

#date_to_long_string(input) ⇒ Object



84
85
86
# File 'lib/liquid_ext.rb', line 84

def date_to_long_string(input)
  Liquid::Utils.to_date(input).localtime.strftime("%d %B %Y") unless input.nil?
end

#date_to_rfc822(input) ⇒ Object



76
77
78
# File 'lib/liquid_ext.rb', line 76

def date_to_rfc822(input)
  Liquid::Utils.to_date(input).localtime.rfc822 unless input.nil?
end

#date_to_string(input) ⇒ Object



80
81
82
# File 'lib/liquid_ext.rb', line 80

def date_to_string(input)
  Liquid::Utils.to_date(input).localtime.strftime("%d %b %Y") unless input.nil?
end

#date_to_xmlschema(input) ⇒ Object



72
73
74
# File 'lib/liquid_ext.rb', line 72

def date_to_xmlschema(input)
  Liquid::Utils.to_date(input).localtime.xmlschema unless input.nil?
end

#group_by(input, property) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/liquid_ext.rb', line 97

def group_by(input, property)
  if input.respond_to?(:group_by) && property
    groups = input.group_by { |item| get_property(item, property) }
    groups.map { |key, value| { "name" => key, "items" => value, "size" => value.size } }
  else
    input
  end
end

#group_by_exp(input, variable, expression) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/liquid_ext.rb', line 106

def group_by_exp(input, variable, expression)
  if input.respond_to?(:group_by)
    parsed_expr = Liquid::Variable.new(expression, Liquid::ParseContext.new)
    @context.stack do
      groups = input.group_by do |item|
        @context[variable] = item
        parsed_expr.render(@context)
      end
      groups.map { |key, value| { "name" => key, "items" => value, "size" => value.size } }
    end
  else
    input
  end
end

#markdownify(input) ⇒ Object



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

def markdownify(input)
  Profiler.group('markdownify') do
    if defined?(MultiMarkdown)
      MultiMarkdown.new("\n" + input, 'snippet', 'no_metadata').to_html
    else
      options  = @context['site']['kramdown'].map { |key, value| [ key.to_sym, value ] }.to_h
      document = Kramdown::Document.new(input, options)
      @context['warnings'].concat(document.warnings) if options[:show_warnings] && @context['warnings']
      document.to_html
    end unless input.nil?
  end
end

#path_to_url(input) ⇒ Object



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

def path_to_url(input)
  if file = Jekyll.sites.last.links[input]
    file.url
  else
    raise Glim::Error.new("path_to_url: No file found for: #{input}")
  end unless input.nil?
end

#relative_url(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/liquid_ext.rb', line 43

def relative_url(other)
  helper = lambda do |base, other|
    base_url, other_url = URI(base), URI(other)
    if other_url.absolute? && base_url.host == other_url.host
      other_url.path
    else
      other
    end
  end

  unless other.nil?
    site, page = URI(@context['site']['url']), URI(@context['page']['url'])
    host, port = @context['site']['host'], @context['site']['port']
    if page.relative? || (site.host == host && site.port == port)
      helper.call(@context['site']['url'], other)
    else
      helper.call(@context['page']['url'], other)
    end
  end
end

#slugify(input) ⇒ Object



19
20
21
# File 'lib/liquid_ext.rb', line 19

def slugify(input)
  Util.slugify(input) unless input.nil?
end

#where(input, property, value) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/liquid_ext.rb', line 88

def where(input, property, value)
  if input.respond_to?(:select) && property && value
    input = input.values if input.is_a?(Hash)
    input.select { |item| get_property(item, property) == value }
  else
    input
  end
end

#xml_escape(input) ⇒ Object



23
24
25
# File 'lib/liquid_ext.rb', line 23

def xml_escape(input)
  input.encode(:xml => :attr).gsub(/\A"|"\z/, '') unless input.nil?
end