forked from mkredpoint/braze-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabs.rb
117 lines (102 loc) · 4.64 KB
/
tabs.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module Tags
class TabsBlock < Liquid::Block
def initialize(tag_name, tabonly = 'false', tokens)
super
@tabclass = 'tab_toggle'
@tabid = 'tab_' + (0...12).map { (97 + rand(26)).chr }.join
if tabonly.downcase.strip == 'local'
@tabclass = 'tab_toggle_only'
end
end
def render(context)
tabs = super.scan(/data\-tab=\"(.*?)\"/)
tabslist = '<ul class="ab-nav ab-nav-tabs ' + @tabclass + '_ul" id="' + @tabid + '_nav">' + "\n"
if tabs.length > 0
tabs.each_with_index do |tab, ind|
# scan returns array of results, only care about first match
tabslist += ' <li class="coderow ' + tab[0].gsub(' ', '-').gsub(/[^\w-]/, '')
if ind == 0
tabslist += ' active'
end
tabslist += '"><a class="' + @tabclass + '" data-tab-target="' + @tabid + '" data-tab="' + tab[0].gsub(' ', '-').gsub(/[^\w-]/, '') + '">' + tab[0] + '</a></li>' + "\n"
end
end
tabslist += '</ul>' + "\n"
tabslist + '<div id="' + @tabid + '" class="ab-tab-content ' + @tabclass + '_div">' + "\n" + super + "\n</div>\n"
end
end
class TabBlock < Liquid::Block
def initialize(tag_name, tab, tokens)
super
@tab = tab.strip.downcase
end
def render(context)
return "" if @tab.empty?
site = context.registers[:site]
converter = site.find_converter_instance(Jekyll::Converters::Markdown)
lines = super.rstrip.split(/\r\n|\r|\n/).select { |line| line.size > 0 }
indentation = lines.map do |line|
match = line.match(/^(\s+)[^\s]+/)
match ? match[1].size : 0
end
indentation = indentation.min
content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(' ', '-').gsub(/[^\w-]/, '')
'<div class="ab-tab-pane ' + tabslug + '_tab " data-tab="' + @tab + '">' + content + "</div>"
end
end
class SubTabsBlock < Liquid::Block
def initialize(tag_name, tabonly = 'false', tokens)
super
@tabclass = 'sub_tab_toggle'
@tabid = 'sub_tab_' + (0...12).map { (97 + rand(26)).chr }.join
if tabonly.downcase.strip != 'global'
@tabclass = 'sub_tab_toggle_only'
end
end
def render(context)
tabs = super.scan(/data\-sub\_tab=\"(.*?)\"/)
tabslist = '<ul class="ab-sub_nav ab-sub_nav-sub_tabs ' + @tabclass + '_ul" id="' + @tabid + '_nav">' + "\n"
if tabs.length > 0
tabs.each_with_index do |tab, ind|
# scan returns array of results, only care about first match
tabslist += ' <li class="coderow ' + tab[0].gsub(' ', '-').gsub(/[^\w-]/, '') + '_sub_tab'
if ind == 0
tabslist += ' sub_active'
end
tabslist += '"><a class="' + @tabclass + '" data-sub_tab-target="' + @tabid + '" data-sub_tab="' + tab[0].gsub(' ', '-').gsub(/[^\w-]/, '') + '_sub_tab">' + tab[0] + '</a></li>' + "\n"
end
end
tabslist += '</ul>' + "\n"
tabslist + '<div id="' + @tabid + '" class="ab-sub_tab-content ' + @tabclass + '_div">' + "\n" + super + "\n</div>\n"
end
end
class SubTabBlock < Liquid::Block
def initialize(tag_name, tab, tokens)
super
@tab = tab.strip.downcase
end
def render(context)
return "" if @tab.empty?
site = context.registers[:site]
converter = site.find_converter_instance(Jekyll::Converters::Markdown)
lines = super.rstrip.split(/\r\n|\r|\n/).select { |line| line.size > 0 }
indentation = lines.map do |line|
match = line.match(/^(\s+)[^\s]+/)
match ? match[1].size : 0
end
indentation = indentation.min
content = indentation ? super.gsub(/^#{' |\t' * indentation}/, '') : super
content = converter.convert(content)
content = content.strip # Strip again to avoid "\n"
tabslug = @tab.gsub(' ', '-').gsub(/[^\w-]/, '')
'<div class="ab-sub_tab-pane ' + tabslug + '_sub_tab " data-sub_tab="' + @tab + '">' + content + "</div>"
end
end
end
Liquid::Template.register_tag("tabs", Tags::TabsBlock)
Liquid::Template.register_tag("tab", Tags::TabBlock)
Liquid::Template.register_tag("subtabs", Tags::SubTabsBlock)
Liquid::Template.register_tag("subtab", Tags::SubTabBlock)