Module: Sourcerer::Jekyll::Liquid::Filters

Defined in:
lib/sourcerer/jekyll/liquid/filters.rb

Overview

This module provides a set of custom filters for use in Liquid templates.

Instance Method Summary collapse

Instance Method Details

#base64(input) ⇒ String

Base64 encodes a string.

Parameters:

  • input (String)
    The string to encode.

Returns:

  • (String)
    The Base64-encoded string.


153
154
155
156
157
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 153

def base64 input
  return input unless input.is_a? String

  Base64.strict_encode64(input)
end

#base64_decode(input) ⇒ String

Decodes a Base64-encoded string.

Parameters:

  • input (String)
    The string to decode.

Returns:

  • (String)
    The decoded string.


162
163
164
165
166
167
168
169
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 162

def base64_decode input
  return input unless input.is_a? String

  Base64.strict_decode64(input)
rescue ArgumentError
  # Return original input if decoding fails
  input
end

#demarkupify(input) ⇒ String

Removes markup from a string.

Parameters:

  • input (String)
    The string to demarkupify.

Returns:

  • (String)
    The demarkupified string.


131
132
133
134
135
136
137
138
139
140
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 131

def demarkupify input
  return input unless input.is_a? String

  input = input.gsub(/`"|"`/, '"')
  input = input.gsub(/'`|`'/, "'")
  input = input.gsub(/[*_`]/, '')
  # change curly quotes to striaght quotes
  input = input.gsub(/[“”]/, '"')
  input.gsub(/[‘’]/, "'")
end

#html_escape(input) ⇒ String

HTML-escapes a string.

Parameters:

  • input (String)
    The string to escape.

Returns:

  • (String)
    The HTML-escaped string.


195
196
197
198
199
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 195

def html_escape input
  return input unless input.is_a? String

  CGI.escapeHTML(input)
end

#html_unescape(input) ⇒ String

Unescapes an HTML-escaped string.

Parameters:

  • input (String)
    The string to unescape.

Returns:

  • (String)
    The unescaped string.


204
205
206
207
208
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 204

def html_unescape input
  return input unless input.is_a? String

  CGI.unescapeHTML(input)
end

#indent(input, spaces = 2, line1: false) ⇒ String

Indents a string by a given number of spaces.

Parameters:

  • input (String)
    The string to indent.
  • spaces (Integer) (defaults to: 2)
    The number of spaces for indentation.
  • line1 (Boolean) (defaults to: false)
    Whether to indent the first line.

Returns:

  • (String)
    The indented string.


71
72
73
74
75
76
77
78
79
80
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 71

def indent input, spaces = 2, line1: false
  indent = ' ' * spaces
  lines = input.split("\n")
  indented = if line1
               lines.map { |line| indent + line }
             else
               lines.map.with_index { |line, i| i.zero? ? line : indent + line }
             end
  indented.join("\n")
end

#inspect_yaml(input) ⇒ String

Dumps a value to YAML format.

Parameters:

  • input (Object)
    The value to dump.

Returns:

  • (String)
    The YAML representation.


145
146
147
148
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 145

def inspect_yaml input
  require 'yaml'
  YAML.dump(input)
end

#md_to_adoc(input, wrap = 'ventilate') ⇒ String

Converts a Markdown string to AsciiDoc.

Parameters:

  • input (String)
    The Markdown string.
  • wrap (String) (defaults to: 'ventilate')
    The wrapping option for the converter.

Returns:

  • (String)
    The converted AsciiDoc string.


60
61
62
63
64
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 60

def md_to_adoc input, wrap = 'ventilate'
  options = {}
  options[:wrap] = wrap.to_sym if wrap
  Kramdoc.convert(input, options)
end

#plusify(input) ⇒ String

Replaces double newlines with a newline and a plus sign.

Parameters:

  • input (String)
    The input string.

Returns:

  • (String)
    The processed string.


52
53
54
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 52

def plusify input
  input.gsub(/\n\n+/, "\n+\n")
end

#render(input, vars = nil) ⇒ String

Renders a Liquid template string with a given scope.

Parameters:

  • input (String, Object)
    The Liquid template string or a pre-parsed template object.
  • vars (Hash) (defaults to: nil)
    A hash of variables to use as the scope.

Returns:

  • (String)
    The rendered output.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 16

def render input, vars = nil
  scope = if vars.is_a?(Hash)
            vars.transform_keys(&:to_s)
          else
            {}
          end

  template =
    if input.respond_to?(:render) && input.respond_to?(:templated?) && input.templated?
      input
    else
      ::Liquid::Template.parse(input.to_s)
    end

  template.render(scope)
end

#ruby_class(input) ⇒ String

Returns the Ruby class name of a value.

Parameters:

  • input (Object)
    The value.

Returns:

  • (String)
    The class name.


124
125
126
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 124

def ruby_class input
  input.class.name
end

#sgyml_type_check(input) ⇒ String

Checks the type of a value in the context of SG-YML.

Parameters:

  • input (Object)
    The value to check.

Returns:

  • (String)
    A string representing the type.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 85

def sgyml_type_check input
  if input.nil?
    'Null:nil'
  elsif input.is_a? Array
    # if all items in Array are (integer, float, string, boolean)
    if input.all? do |item|
      item.is_a?(Integer) || item.is_a?(Float) || item.is_a?(String) ||
      item.is_a?(TrueClass) || item.is_a?(FalseClass)
    end
      'Compound:ArrayList'
    elsif input.all? { |item| item.is_a?(Hash) && (item.keys.length >= 2) }
      'Compound:ArrayTable'
    else
      'Compound:Array'
    end
  elsif input.is_a? Hash
    if input.values.all? { |value| value.is_a?(Hash) && (value.keys.length >= 2) }
      'Compound:MapTable'
    else
      'Compound:Map'
    end
  elsif input.is_a? String
    'Scalar:String'
  elsif input.is_a? Integer
    'Scalar:Number'
  elsif input.is_a? Time
    'Scalar:DateTime'
  elsif input.is_a? Float
    'Scalar:Float'
  elsif input.is_a?(TrueClass) || input.is_a?(FalseClass)
    'Scalar:Boolean'
  else
    'unknown:unknown'
  end
end

#sluggerize(input, format = 'kebab') ⇒ String

Converts a string into a slug.

Parameters:

  • input (String)
    The string to convert.
  • format (String) (defaults to: 'kebab')
    The desired format (`kebab`, `snake`, `camel`, `pascal`).

Returns:

  • (String)
    The sluggerized string.


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 37

def sluggerize input, format = 'kebab'
  return input unless input.is_a? String

  case format
  when 'kebab' then input.downcase.gsub(/[\s\-_]/, '-')
  when 'snake' then input.downcase.gsub(/[\s\-_]/, '_')
  when 'camel' then input.downcase.gsub(/[\s\-_]/, '_').camelize(:lower)
  when 'pascal' then input.downcase.gsub(/[\s\-_]/, '_').camelize(:upper)
  else input
  end
end

#url_decode(input) ⇒ String

Decodes a URL-encoded string.

Parameters:

  • input (String)
    The string to decode.

Returns:

  • (String)
    The decoded string.


183
184
185
186
187
188
189
190
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 183

def url_decode input
  return input unless input.is_a? String

  CGI.unescape(input)
rescue ArgumentError
  # Return original input if decoding fails
  input
end

#url_encode(input) ⇒ String

URL-encodes a string.

Parameters:

  • input (String)
    The string to encode.

Returns:

  • (String)
    The URL-encoded string.


174
175
176
177
178
# File 'lib/sourcerer/jekyll/liquid/filters.rb', line 174

def url_encode input
  return input unless input.is_a? String

  CGI.escape(input)
end