forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.rb
83 lines (72 loc) · 2.48 KB
/
alerts.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Jekyll
module HomeAssistant
class AlertBlock < Liquid::Block
def initialize(tag_name, args, tokens)
super
raise SyntaxError, <<~MSG unless args.strip =~ SYNTAX
Syntax error in alert block while parsing the following options:
#{args}
Valid syntax:
{% <note|tip|important|warning|caution> [title="Extra title"] [icon="mdi:alert"] %}
MSG
@type = tag_name
@options = Regexp.last_match(1)
end
def render(context)
# We parse on render, as we now have context
options = parse_options(@options, context)
contents = super(context)
title = @type.capitalize
if options.include? :title
title += ": #{options[:title]}"
end
if options.include? :icon
icon = options[:icon]
elsif @type == 'tip'
icon = "mdi:lightbulb-outline"
elsif @type == 'important'
icon = "mdi:message-alert-outline"
elsif @type == 'warning'
icon = "mdi:alert-outline"
elsif @type == 'caution'
icon = "mdi:alert-circle-outline"
else
icon = "mdi:information-outline"
end
<<~MARKUP
<div class="alert alert-#{@type}">
<p class="alert-title"><iconify-icon inline icon='#{icon}'></iconify-icon> #{title}</p>
<p class="alert-content">
#{contents}
</p>
</div>
MARKUP
end
private
SYNTAX = /^((\s+\w+(=([\w.]+?|".+?"))?)*)$/
OPTIONS_REGEX = /(?:\w="[^"]*"|\w=[\w.]+|\w)+/
def parse_options(input, context)
options = {}
return options if input.empty?
# Split along 3 possible forms: key="value", key=value, or just key
input.scan(OPTIONS_REGEX) do |opt|
key, value = opt.split('=')
unless value.nil?
if value&.include?('"')
value.delete!('"')
else
value = context[value]
end
end
options[key.to_sym] = value || true
end
options
end
end
end
end
Liquid::Template.register_tag('note', Jekyll::HomeAssistant::AlertBlock)
Liquid::Template.register_tag('tip', Jekyll::HomeAssistant::AlertBlock)
Liquid::Template.register_tag('important', Jekyll::HomeAssistant::AlertBlock)
Liquid::Template.register_tag('warning', Jekyll::HomeAssistant::AlertBlock)
Liquid::Template.register_tag('caution', Jekyll::HomeAssistant::AlertBlock)