-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtabs.rb
More file actions
71 lines (61 loc) · 1.9 KB
/
tabs.rb
File metadata and controls
71 lines (61 loc) · 1.9 KB
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
# frozen_string_literal: true
module SP
# Sets up a tabs with top switcher bar
class Tabs < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@group = markup.strip.empty? ? 'default' : markup.strip
end
def render(context)
tab_bar_content = ''
context['tabs'] = []
context['tab_group'] = @group
result = super
context['tabs'].each_with_index do |(label, title), index|
res = index.zero? ? ' btn-purple' : ''
tab_bar_content += <<~CONTENT
<button class="skhep-bar-item #{@group}-#{label}-btn btn m-2#{res}" onclick="openTab('#{label}', '#{@group}')">#{title}</button>
CONTENT
end
<<~RETURN
<div class="skhep-bar d-flex m-2" style="justify-content:center;">
#{tab_bar_content}
</div>
#{result}
RETURN
end
end
# Sets up tabs without the top switcher bar
class TabBodies < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@group = markup.strip.empty? ? 'default' : markup.strip
end
def render(context)
context['tabs'] = []
context['tab_group'] = @group
super
end
end
# This is the content of each tab
class Tab < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@label, @title = markup.strip.split(/ /, 2)
end
def render(context)
raise SyntaxError, "'tab' be in 'tabs' or 'tabbodies'" unless context.key?('tabs')
group = context['tab_group'] || 'default'
res = context['tabs'].empty? ? '' : ' style="display:none;"'
context['tabs'] << [@label, @title]
<<~RETURN
<div class="skhep-tab #{group}-#{@label}-tab" markdown="1"#{res}>
#{super}
</div>
RETURN
end
end
end
Liquid::Template.register_tag('tabs', SP::Tabs)
Liquid::Template.register_tag('tabbodies', SP::TabBodies)
Liquid::Template.register_tag('tab', SP::Tab)