Module: Jekyll::Tabler

Defined in:
lib/jekyll-tabler.rb,
lib/version.rb

Overview

Shared helpers and Liquid tag implementations for Tabler icons.

Defined Under Namespace

Classes: FilledTag, OutlineTag

Constant Summary collapse

VERSION =
"0.1.1"
VARIABLE_LOOKUP =
/\A[a-zA-Z_][\w-]*(?:\.[\w-]+|\[[^\]]+\])*\z/
OPTION_LOOKUP =
/\A([^=\s]+)=(.+)\z/
VALID_OPTIONS =
%w[size color].freeze

Class Method Summary collapse

Class Method Details

.filled_wrapper(icon_name, size = 24, color = "currentColor") ⇒ Object

Builds the filled SVG after render-time values have been resolved.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jekyll-tabler.rb', line 71

def filled_wrapper(icon_name, size = 24, color = "currentColor") # rubocop:disable Metrics/MethodLength
  icons = tabler_icons("filled")
  ds = Array(icons[icon_name])
  paths = ds.map { |d| %(<path d="#{d}" />) }.join("\n")
  <<~HTML
    <svg xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 24 24"
    width="#{size}"
    height="#{size}"
    fill="#{color}"
    class="jekyll-tabler-icon #{icon_name}"
    >
    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
    #{paths}
    </svg>
  HTML
end

.outline_wrapper(icon_name, size = 24, color = "currentColor") ⇒ Object

Builds the outline SVG after render-time values have been resolved.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-tabler.rb', line 48

def outline_wrapper(icon_name, size = 24, color = "currentColor") # rubocop:disable Metrics/MethodLength
  icons = tabler_icons("outline")
  ds = Array(icons[icon_name])
  paths = ds.map { |d| %(<path d="#{d}" />) }.join("\n")
  <<~HTML
    <svg xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 24 24"
    fill="none"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
    stroke="#{color}"
    width="#{size}"
    height="#{size}"
    class="jekyll-tabler-icon #{icon_name}"
    >
    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
    #{paths}
    </svg>
  HTML
end

.parse_optional_args(arguments) ⇒ Object

Splits optional arguments into named options and positional arguments.

Named options are validated here so each tag class can keep its initializer focused on assigning the final values.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jekyll-tabler.rb', line 108

def parse_optional_args(arguments) # rubocop:disable Metrics/MethodLength
  arguments.each_with_object([{}, []]) do |argument, memo|
    options = memo[0]
    positional_args = memo[1]
    match = argument.match(OPTION_LOOKUP)

    unless match
      positional_args << argument
      next
    end

    key = match[1]
    raise Liquid::SyntaxError, "Unknown #{key} option in tabler tag" unless VALID_OPTIONS.include?(key)
    raise Liquid::SyntaxError, "Duplicate #{key} option in tabler tag" if options.key?(key)

    options[key] = match[2]
  end
end

.resolve_argument(argument, context) ⇒ Object

Resolves a tag argument against the Liquid context when it looks like a variable lookup. Literal values such as ‘24` or `currentColor` pass through unchanged.



92
93
94
95
96
97
# File 'lib/jekyll-tabler.rb', line 92

def resolve_argument(argument, context)
  return argument unless argument.is_a?(String) && argument.match?(VARIABLE_LOOKUP)

  resolved = context.evaluate(Liquid::Expression.parse(argument))
  resolved.nil? ? argument : resolved
end

.syntax_messageObject

Shared syntax error message used by both tags.



100
101
102
# File 'lib/jekyll-tabler.rb', line 100

def syntax_message
  "Syntax: {% tabler|tabler_filled icon_name [size] [color] [size=value] [color=value] %}"
end

.tabler_icons(type) ⇒ Object

Loads the icon path data that ships with the gem.

The YAML files map an icon name to one or more SVG path definitions. This is the only place where the plugin reaches into packaged assets.



38
39
40
41
42
43
44
45
# File 'lib/jekyll-tabler.rb', line 38

def tabler_icons(type)
  data_path = File.join(
    Gem.loaded_specs["jekyll-tabler"].full_gem_path,
    "assets",
    "#{type}.yml"
  )
  YAML.load_file(data_path)
end