Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/jekyll-icon-tag.rb
1677 views
1
# frozen_string_literal: true
2
3
module Jekyll
4
module Tags
5
6
# Our {% icon X %} tag
7
class IconTag < Liquid::Tag
8
def initialize(tag_name, text, tokens)
9
super
10
parts = text.strip.split
11
@text = parts[0]
12
@aria = true
13
return unless parts[1] == 'aria=false'
14
15
@aria = false
16
end
17
18
##
19
# This function renders the icon tag
20
# Params:
21
# +icon+:: The icon to render
22
# +@area+:: Whether to add aria-hidden
23
# +@text+:: The text to add to the icon
24
#
25
# Returns:
26
# The HTML for the icon
27
# Note: The icon text label is wrapped in a span with class
28
# "visually-hidden" to make it accessible to screen readers.
29
#
30
# Example:
31
# {% icon fa fa-github %}
32
# => <i class="fa fa-github" aria-hidden="true"></i>
33
# {% icon fa fa-github aria=false %}
34
# => <i class="fa fa-github"></i>
35
def render_for_text(icon)
36
if icon.empty?
37
raise SyntaxError, "No icon defined for: '#{@text}'. Please define it in `_config.yml` (under `icon-tag:`)."
38
end
39
40
if icon.start_with?('fa')
41
if @aria
42
%(<i class="#{icon}" aria-hidden="true"></i><span class="visually-hidden">#{@text}</span>)
43
else
44
%(<i class="#{icon}" aria-hidden="true"></i>)
45
end
46
elsif icon.start_with?('ai')
47
if @aria
48
%(<i class="ai #{icon}" aria-hidden="true"></i><span class="visually-hidden">#{@text}</span>)
49
else
50
%(<i class="ai #{icon}" aria-hidden="true"></i>)
51
end
52
end
53
end
54
55
##
56
# icon - Include an icon from our _config.yml into your tutorial
57
#
58
# Examples:
59
#
60
# {% icon email %}
61
# {% icon galaxy-history %}
62
#
63
def render(context)
64
cfg = get_config(context)
65
icon = cfg[@text] || ''
66
render_for_text(icon)
67
end
68
69
def get_config(context)
70
context.registers[:site].config['icon-tag']
71
end
72
end
73
74
# The variable version that can accept a variable name instead of a string
75
class IconTagVar < IconTag
76
def initialize(tag_name, text, tokens)
77
super
78
@text = text.strip
79
end
80
81
##
82
# icon_var - Include an icon from our _config.yml into your tutorial, but accessing a variable rather than expecting a string.
83
#
84
# Examples:
85
#
86
# {% icon_var var1 %}
87
# {% icon_var var2 %}
88
#
89
def render(context)
90
cfg = get_config(context)
91
icon = cfg[context[@text]] || ''
92
render_for_text(icon)
93
end
94
end
95
end
96
end
97
98
Liquid::Template.register_tag('icon_var', Jekyll::Tags::IconTagVar)
99
Liquid::Template.register_tag('icon', Jekyll::Tags::IconTag)
100
101