Module: Jekyll::UJPowertools
- Defined in:
- lib/tags/iffile.rb,
lib/filters/main.rb,
lib/tags/iffalsy.rb,
lib/tags/external.rb,
lib/tags/iftruthy.rb,
lib/tags/urlmatches.rb,
lib/helpers/variable_resolver.rb
Defined Under Namespace
Modules: VariableResolver Classes: ExternalTag, IfFalsyTag, IfFileTag, IfTruthyTag, UrlMatchesTag
Class Method Summary collapse
-
.cache_timestamp ⇒ Object
Accessor for the consistent timestamp.
Instance Method Summary collapse
-
#uj_commaify(input) ⇒ Object
Format a number with commas (e.g., 10000 becomes 10,000).
-
#uj_content_format(input) ⇒ Object
Format content based on file extension - apply liquify and markdownify for .md files.
-
#uj_hash(input, max) ⇒ Object
Return a deterministic number between 0 and max (exclusive) based on input string hash Usage: “some-string” | uj_hash: 1000 } => 0-999 (always same for same input).
-
#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 ||= 0 @context.registers @context.registers += 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.
-
#uj_jsonify(input, indent_size = 2) ⇒ Object
Pretty print JSON with configurable indentation (default 2 spaces).
-
#uj_liquify(input, max_depth = 10) ⇒ Object
Process Liquid template syntax within a string, recursively handling nested Liquid variables.
-
#uj_pluralize(count, singular, plural = nil) ⇒ Object
Pluralize a word based on a count Usage: count | uj_pluralize: ‘singular’, ‘plural’ } Example: 5 | uj_pluralize: ‘post’, ‘posts’ } => ‘posts’ Example: 1 | uj_pluralize: ‘post’, ‘posts’ } => ‘post’.
-
#uj_random(input) ⇒ Object
Return a random number between 0 and the input.
-
#uj_strip_ads(input) ⇒ Object
Strip ads from the input.
-
#uj_title_case(input) ⇒ Object
Title case.
Class Method Details
.cache_timestamp ⇒ Object
Accessor for the consistent timestamp
74 75 76 |
# File 'lib/filters/main.rb', line 74 def self. @cache_timestamp end |
Instance Method Details
#uj_commaify(input) ⇒ Object
Format a number with commas (e.g., 10000 becomes 10,000)
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/filters/main.rb', line 168 def uj_commaify(input) return input unless input str = input.to_s.strip return input if str.empty? # Check if the string is a valid number (integer or decimal, possibly negative) return input unless str.match?(/^-?\d+(\.\d+)?$/) str.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
#uj_content_format(input) ⇒ Object
Format content based on file extension - apply liquify and markdownify for .md files
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 |
# File 'lib/filters/main.rb', line 84 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) # Apply recursive liquify (markdown images are already converted to uj_image tags by the hook) liquified = uj_liquify(input) # 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_hash(input, max) ⇒ Object
Return a deterministic number between 0 and max (exclusive) based on input string hash Usage: “some-string” | uj_hash: 1000 } => 0-999 (always same for same input)
55 56 57 |
# File 'lib/filters/main.rb', line 55 def uj_hash(input, max) Digest::MD5.hexdigest(input.to_s).hex % max.to_i 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
42 43 44 45 46 |
# File 'lib/filters/main.rb', line 42 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
32 33 34 |
# File 'lib/filters/main.rb', line 32 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)
147 148 149 150 |
# File 'lib/filters/main.rb', line 147 def uj_jsonify(input, indent_size = 2) indent_string = ' ' * indent_size.to_i JSON.pretty_generate(input, indent: indent_string) end |
#uj_liquify(input, max_depth = 10) ⇒ Object
Process Liquid template syntax within a string, recursively handling nested Liquid variables
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/filters/main.rb', line 122 def uj_liquify(input, max_depth = 10) return '' unless input depth = 0 result = input.to_s # Keep processing while we detect Liquid syntax and haven't exceeded max depth while (result.include?('{{') || result.include?('{%')) && depth < max_depth new_result = if @context.respond_to?(:registers) Liquid::Template.parse(result).render(@context) else Liquid::Template.parse(result).render(@context[:registers] || {}) end # If nothing changed, we're done (prevents infinite loops) break if new_result == result result = new_result depth += 1 end result end |
#uj_pluralize(count, singular, plural = nil) ⇒ Object
Pluralize a word based on a count Usage: count | uj_pluralize: ‘singular’, ‘plural’ } Example: 5 | uj_pluralize: ‘post’, ‘posts’ } => ‘posts’ Example: 1 | uj_pluralize: ‘post’, ‘posts’ } => ‘post’
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/filters/main.rb', line 156 def uj_pluralize(count, singular, plural = nil) # Default plural adds 's' to singular if not provided plural ||= "#{singular}s" # Convert count to integer for comparison count_int = count.to_i # Return singular for 1, plural for everything else (including 0) count_int == 1 ? singular : plural end |
#uj_random(input) ⇒ Object
Return a random number between 0 and the input
49 50 51 |
# File 'lib/filters/main.rb', line 49 def uj_random(input) rand(input) end |
#uj_strip_ads(input) ⇒ Object
Strip ads from the input
13 14 15 16 17 18 19 |
# File 'lib/filters/main.rb', line 13 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
60 61 62 |
# File 'lib/filters/main.rb', line 60 def uj_title_case(input) input.split(' ').map(&:capitalize).join(' ') end |