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



71
72
73
# File 'lib/filters/main.rb', line 71

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



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
106
107
108
109
# File 'lib/filters/main.rb', line 81

def uj_content_format(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)

  # Apply liquify first
  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'
    # Apply markdownify for markdown files
    site = @context.registers[:site] if @context.respond_to?(:registers)
    site ||= @context[:registers][:site] if @context.is_a?(Hash)

    if site
      converter = site.find_converter_instance(Jekyll::Converters::Markdown)
      converter.convert(liquified)
    else
      liquified
    end
  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



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

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



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

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

#uj_random(input) ⇒ Object

Return a random number between 0 and the input



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

def uj_random(input)
  rand(input)
end

#uj_strip_ads(input) ⇒ Object

Strip ads from the input



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

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



57
58
59
# File 'lib/filters/main.rb', line 57

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

#uj_year(input) ⇒ Object

Return the current year



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

def uj_year(input)
  Time.now.year
end