Class: Jekyll::SimpleIcons::SimpleIconsTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-simple-icons/simple_icons.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ SimpleIconsTag

Returns a new instance of SimpleIconsTag.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jekyll-simple-icons/simple_icons.rb', line 9

def initialize(tag_name, markup, options)
  super
  # Split the markup into icon name and opts
  if markup =~ %r!^([^\s]+)\s*(.+)?$!
    @icon_slug = ::Regexp.last_match(1).strip.downcase
    @opts = {}
    ::Regexp.last_match(2)&.scan(%r!(\w+):\s*([^\s,]+)!) do |key, value|
      @opts[key] = value
    end
  else
    raise SyntaxError, "Syntax Error in 'simpleicons' - Valid syntax: simpleicons icon-name [key:value]"
  end
end

Instance Method Details

#download_icon(url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-simple-icons/simple_icons.rb', line 34

def download_icon(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 5  # 5 seconds timeout for connection
  http.read_timeout = 5  # 5 seconds timeout for reading

  request = Net::HTTP::Get.new(uri)
  response = http.request(request)

  if response.code == "200" # net/http returns http status code as string
    response.body
  else
    raise Net::HTTPError.new("Cannot fetch icon from #{url}, got HTTP #{response.code}", response)
  end
end

#icon_urlObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll-simple-icons/simple_icons.rb', line 23

def icon_url
  color = @opts&.fetch("color", "black") || "black" # default to black
  dark_color = @opts&.fetch("dark", nil) || nil # default not to use dark color

  if dark_color.nil?
    "https://cdn.simpleicons.org/#{@icon_slug}/#{color}"
  else
    "https://cdn.simpleicons.org/#{@icon_slug}/#{color}/#{dark_color}"
  end
end

#render(_context) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jekyll-simple-icons/simple_icons.rb', line 60

def render(_context) # rubocop:disable Metrics/PerceivedComplexity
  height = @opts&.fetch("h", "32") || @opts&.fetch("height", "32") || "32"
  width = @opts&.fetch("w", "32") || @opts&.fetch("width", "32") || "32"

  begin
    url = icon_url
    svg_xml = download_icon(url)
    img_tag = svg_to_img_tag(svg_xml, height, width)
    return img_tag.to_s
  rescue SocketError, Net::OpenTimeout, Net::ReadTimeout
    Jekyll.logger.error "[jekyll-simple-icons] Error: Cannot fetch icon from #{url}, connection timeout"
  rescue Net::HTTPError => e
    Jekyll.logger.error "[jekyll-simple-icons] HTTP Error: #{e.message}"
  rescue StandardError => e
    Jekyll.logger.error "[jekyll-simple-icons] Error: #{e.message}"
  end
  ""
end

#svg_to_img_tag(svg_xml, height, width) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/jekyll-simple-icons/simple_icons.rb', line 51

def svg_to_img_tag(svg_xml, height, width)
  # Strip whitespace and newlines to make the SVG more compact
  svg_xml = svg_xml.gsub(%r!\s+!, " ").strip
  # Encode SVG to Base64
  base64_svg = Base64.strict_encode64(svg_xml)
  # Create the img tag with data URI
  "<img height=\"#{height}\" width=\"#{width}\" src=\"data:image/svg+xml;base64,#{base64_svg}\" alt=\"#{@icon_slug} simple-icon\">"
end