Module: Jekyll::UJPowertools

Defined in:
lib/filters/main.rb,
lib/tags/iffalsy.rb,
lib/tags/iftruthy.rb

Defined Under Namespace

Classes: IfFalsyTag, IfTruthyTag

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_timestampObject

Accessor for the consistent timestamp



67
68
69
# File 'lib/filters/main.rb', line 67

def self.cache_timestamp
  @cache_timestamp
end

Instance Method Details

#uj_content_format(input) ⇒ Object

Format content based on file extension - apply liquify and markdownify for .md files



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/filters/main.rb', line 77

def uj_content_format(input)
  # Return empty string if input is nil
  return '' unless input
  
  # Get the current page from context
  page = @context.registers[:page] if @context.respond_to?(:registers)
  page ||= @context[:registers][:page] if @context.is_a?(Hash)
  
  # Get site from context
  site = @context.registers[:site] if @context.respond_to?(:registers)
  site ||= @context[:registers][:site] if @context.is_a?(Hash)

  # Simply apply liquify first (markdown images are already converted to uj_image tags by the hook)
  liquified = if @context.respond_to?(:registers)
    Liquid::Template.parse(input).render(@context)
  else
    Liquid::Template.parse(input).render(@context[:registers] || {})
  end

  # Check if the page extension is .md
  if page && page['extension'] == '.md' && site
    # Apply markdownify for markdown files
    converter = site.find_converter_instance(Jekyll::Converters::Markdown)
    converter.convert(liquified)
  else
    # Return just liquified content for non-markdown files
    liquified
  end
end

#uj_increment_return(input) ⇒ Object

Increment a global counter that can be accessed from any page then return the new value def uj_increment_return(input)

@context.registers[:uj_incremental_return] ||= 0
@context.registers[:uj_incremental_return]
@context.registers[:uj_incremental_return] += input

end



41
42
43
44
45
# File 'lib/filters/main.rb', line 41

def uj_increment_return(input)
  @context ||= { registers: {} }
  @context[:registers][:uj_incremental_return] ||= 0
  @context[:registers][:uj_incremental_return] += input
end

#uj_json_escape(value) ⇒ Object

Escape a string for use in JSON def uj_json_escape(value)

value
  .gsub('\\', '\\\\')  # Escape backslashes
  .gsub('"', '\"')     # Escape double quotes
  .gsub("\b", '\\b')   # Escape backspace
  .gsub("\f", '\\f')   # Escape formfeed
  .gsub("\n", '\\n')   # Escape newline
  .gsub("\r", '\\r')   # Escape carriage return
  .gsub("\t", '\\t')   # Escape tab

end



31
32
33
# File 'lib/filters/main.rb', line 31

def uj_json_escape(value)
  value.to_json[1..-2]  # Convert to JSON and remove the surrounding quotes
end

#uj_jsonify(input, indent_size = 2) ⇒ Object

Pretty print JSON with configurable indentation (default 2 spaces)



108
109
110
111
# File 'lib/filters/main.rb', line 108

def uj_jsonify(input, indent_size = 2)
  indent_string = ' ' * indent_size.to_i
  JSON.pretty_generate(input, indent: indent_string)
end

#uj_random(input) ⇒ Object

Return a random number between 0 and the input



48
49
50
# File 'lib/filters/main.rb', line 48

def uj_random(input)
  rand(input)
end

#uj_strip_ads(input) ⇒ Object

Strip ads from the input



12
13
14
15
16
17
18
# File 'lib/filters/main.rb', line 12

def uj_strip_ads(input)
  input
    # Remove HTML <ad-units>
    .gsub(/\s*<ad-unit>[\s\S]*?<\/ad-unit>\s*/m, '')
    # Remove includes starting with "/master/modules/adunits/"
    .gsub(/\s*\{% include \/master\/modules\/adunits\/.*? %\}\s*/m, '')
end

#uj_title_case(input) ⇒ Object

Title case



53
54
55
# File 'lib/filters/main.rb', line 53

def uj_title_case(input)
  input.split(' ').map(&:capitalize).join(' ')
end