Class: Jekyll::UJIconTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/tags/icon.rb

Constant Summary collapse

DEFAULT_ICON =

Default icon to show when requested icon is not found

'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><!--!Font Awesome Free v7.0.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M320 64C334.7 64 348.2 72.1 355.2 85L571.2 485C577.9 497.4 577.6 512.4 570.4 524.5C563.2 536.6 550.1 544 536 544L104 544C89.9 544 76.9 536.6 69.6 524.5C62.3 512.4 62.1 497.4 68.8 485L284.8 85C291.8 72.1 305.3 64 320 64zM320 232C306.7 232 296 242.7 296 256L296 368C296 381.3 306.7 392 320 392C333.3 392 344 381.3 344 368L344 256C344 242.7 333.3 232 320 232zM346.7 448C347.3 438.1 342.4 428.7 333.9 423.5C325.4 418.4 314.7 418.4 306.2 423.5C297.7 428.7 292.8 438.1 293.4 448C292.8 457.9 297.7 467.3 306.2 472.5C314.7 477.6 325.4 477.6 333.9 472.5C342.4 467.3 347.3 457.9 346.7 448z"/></svg>'
LANGUAGE_TO_COUNTRY =

Language code to country code mapping for flags

{
  'en' => 'us',    # English -> United States (could also be 'gb' for Great Britain)
  'es' => 'es',    # Spanish -> Spain
  'fr' => 'fr',    # French -> France
  'de' => 'de',    # German -> Germany
  'it' => 'it',    # Italian -> Italy
  'pt' => 'pt',    # Portuguese -> Portugal
  'ru' => 'ru',    # Russian -> Russia
  'ja' => 'jp',    # Japanese -> Japan
  'ko' => 'kr',    # Korean -> South Korea
  'zh' => 'cn',    # Chinese -> China
  'ar' => 'sa',    # Arabic -> Saudi Arabia
  'hi' => 'in',    # Hindi -> India
  'tr' => 'tr',    # Turkish -> Turkey
  'pl' => 'pl',    # Polish -> Poland
  'nl' => 'nl',    # Dutch -> Netherlands
  'sv' => 'se',    # Swedish -> Sweden
  'no' => 'no',    # Norwegian -> Norway
  'da' => 'dk',    # Danish -> Denmark
  'fi' => 'fi',    # Finnish -> Finland
  'he' => 'il',    # Hebrew -> Israel
  'th' => 'th',    # Thai -> Thailand
  'vi' => 'vn',    # Vietnamese -> Vietnam
  'uk' => 'ua',    # Ukrainian -> Ukraine
  'cs' => 'cz',    # Czech -> Czech Republic
  'hu' => 'hu',    # Hungarian -> Hungary
  'ro' => 'ro',    # Romanian -> Romania
  'bg' => 'bg',    # Bulgarian -> Bulgaria
  'hr' => 'hr',    # Croatian -> Croatia
  'sk' => 'sk',    # Slovak -> Slovakia
  'sl' => 'si',    # Slovenian -> Slovenia
  'et' => 'ee',    # Estonian -> Estonia
  'lv' => 'lv',    # Latvian -> Latvia
  'lt' => 'lt',    # Lithuanian -> Lithuania
  'mt' => 'mt',    # Maltese -> Malta
  'ga' => 'ie',    # Irish -> Ireland
  'cy' => 'gb',    # Welsh -> Great Britain
  'ca' => 'es',    # Catalan -> Spain (could also be ad for Andorra)
  'eu' => 'es',    # Basque -> Spain
  'gl' => 'es',    # Galician -> Spain
}
@@icon_cache =

Cache for loaded icons to improve performance

{}

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ UJIconTag

Returns a new instance of UJIconTag.



66
67
68
69
# File 'lib/tags/icon.rb', line 66

def initialize(tag_name, markup, tokens)
  super
  @markup = markup.strip
end

Instance Method Details

#render(context) ⇒ Object



71
72
73
74
75
76
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tags/icon.rb', line 71

def render(context)
  # Parse arguments that can be quoted or unquoted
  parts = parse_arguments(@markup)
  icon_name_input = parts[0]
  css_classes = parts[1]

  # Check if the input was originally quoted (literal string)
  is_quoted = @markup.strip.match(/^['"]/)

  # If quoted, use as literal. Otherwise, try to resolve as variable
  if is_quoted
    icon_name = icon_name_input
  else
    # Try to resolve as a variable
    icon_name = resolve_variable(context, icon_name_input)
    # If it didn't resolve to a string, use the input as literal
    icon_name = icon_name_input if icon_name.nil? || !icon_name.is_a?(String)
  end

  # Strip quotes from resolved icon name if present
  if icon_name.is_a?(String) && icon_name.match(/^['"].*['"]$/)
    icon_name = icon_name[1..-2]
  end

  # Get site from context
  site = context.registers[:site]
  return '' unless site

  # Load the icon SVG from file
  icon_svg = load_icon_from_file(site, icon_name.to_s)
  return '' unless icon_svg

  # Process SVG to inject required attributes
  processed_svg = inject_svg_attributes(icon_svg)

  # Determine CSS classes
  # font_size = '1em' # default
  # if size_input && !size_input.empty?
  #   # Check if it's a Font Awesome preset size
  #   font_size = FA_SIZES[size_input] || size_input
  # end

  # Wrap in i tag with CSS classes (always include 'fa' class)
  if css_classes && !css_classes.empty?
    "<i class=\"fa #{css_classes}\">#{processed_svg}</i>"
  else
    "<i class=\"fa\">#{processed_svg}</i>"
  end
end