module Glim::LiquidFilters

Public Instance Methods

absolute_url(path) click to toggle source
# File lib/liquid_ext.rb, line 33
def absolute_url(path)
  return if 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
cgi_escape(input) click to toggle source
# File lib/liquid_ext.rb, line 29
def cgi_escape(input)
  CGI.escape(input) unless input.nil?
end
date_to_long_string(input) click to toggle source
# File lib/liquid_ext.rb, line 90
def date_to_long_string(input)
  Liquid::Utils.to_date(input).localtime.strftime("%d %B %Y") unless input.nil?
end
date_to_rfc822(input) click to toggle source
# File lib/liquid_ext.rb, line 82
def date_to_rfc822(input)
  Liquid::Utils.to_date(input).localtime.rfc822 unless input.nil?
end
date_to_string(input) click to toggle source
# File lib/liquid_ext.rb, line 86
def date_to_string(input)
  Liquid::Utils.to_date(input).localtime.strftime("%d %b %Y") unless input.nil?
end
date_to_xmlschema(input) click to toggle source
# File lib/liquid_ext.rb, line 78
def date_to_xmlschema(input)
  Liquid::Utils.to_date(input).localtime.xmlschema unless input.nil?
end
group_by(input, property) click to toggle source
# File lib/liquid_ext.rb, line 103
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) click to toggle source
# File lib/liquid_ext.rb, line 112
def group_by_exp(input, variable, expression)
  return input unless 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
end
markdownify(input) click to toggle source
# File lib/liquid_ext.rb, line 6
def markdownify(input)
  return if input.nil?
  
  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
  end
end
path_to_url(input) click to toggle source
# File lib/liquid_ext.rb, line 68
def path_to_url(input)
  return if input.nil?
  
  if file = Jekyll.sites.last.links[input]
    file.url
  else
    raise Glim::Error.new("path_to_url: No file found for: #{input}")
  end
end
relative_url(other) click to toggle source
# File lib/liquid_ext.rb, line 46
def relative_url(other)
  return if other.nil?
  
  site, page = URI(@context['site']['url']), URI(@context['page']['url'])
  host, port = @context['site']['host'], @context['site']['port']
  
  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
  
  if page.relative? || (site.host == host && site.port == port)
    helper.call(@context['site']['url'], other)
  else
    helper.call(@context['page']['url'], other)
  end
end
slugify(input) click to toggle source
# File lib/liquid_ext.rb, line 21
def slugify(input)
  Util.slugify(input) unless input.nil?
end
where(input, property, value) click to toggle source
# File lib/liquid_ext.rb, line 94
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) click to toggle source
# File lib/liquid_ext.rb, line 25
def xml_escape(input)
  input.encode(:xml => :attr).gsub(/\A"|"\z/, '') unless input.nil?
end

Private Instance Methods

get_property(obj, property) click to toggle source
# File lib/liquid_ext.rb, line 127
def get_property(obj, property)
  if obj.respond_to?(:to_liquid)
    property.to_s.split('.').reduce(obj.to_liquid) do |mem, key|
      mem[key]
    end
  elsif obj.respond_to?(:data)
    obj.data[property.to_s]
  else
    obj[property.to_s]
  end
end