forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminology_tooltip.rb
52 lines (39 loc) · 1.73 KB
/
terminology_tooltip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Jekyll
module HomeAssistant
class TerminologyTooltip < Liquid::Tag
def initialize(tag_name, args, tokens)
super
if args.strip =~ SYNTAX
@term = Regexp.last_match(1)
@text = Regexp.last_match(2)
else
raise SyntaxError, <<~MSG
Syntax error in tag 'term' while parsing the following options:
#{args}
Valid syntax:
{% term <term> [<text>] %}
MSG
end
end
def render(context)
entries = context.registers[:site].data["glossary"].select do |entry|
entry.key?("term") and (@term.casecmp(entry["term"]).zero? or (entry.key?("aliases") and entry["aliases"].any?{ |s| s.casecmp(@term)==0 }))
end
raise ArgumentError, "Term #{@term} was not found in the glossary" if entries.length == 0
raise ArgumentError, "Term #{@term} is in the glossary multiple times" if entries.length > 1
raise ArgumentError, "Term #{@term} is missing a definition" unless entries[0].key?("definition")
glossary = entries[0]
definition = glossary["excerpt"] || glossary["definition"]
if glossary.key?("link")
rendered_link = Liquid::Template.parse(glossary["link"]).render(context).strip
link = "<br><a class='terminology-link' href='#{rendered_link}' target='_blank'>[Learn more]</a>"
end
tooltip = "<span class='terminology-tooltip'>#{definition}#{link || ""}</span>"
"<span class='terminology'>#{@text || @term}#{tooltip}</span>"
end
private
SYNTAX = %r!^(\w+?|".+?")(?:\s+(\w+|".+"))?$!.freeze
end
end
end
Liquid::Template.register_tag('term', Jekyll::HomeAssistant::TerminologyTooltip)