-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathextension.rb
61 lines (56 loc) · 1.55 KB
/
extension.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
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
# An extension that automatically hides blocks marked with the style
# "result" and adds a link to the previous element that has the style
# "title" that allows displaying the "result".
#
# Usage
#
# = View Result Sample
#
# .This will have a link next to it
# ----
# * always displayed
# * always displayed 2
# ----
#
# [.result]
# ====
# * hidden till clicked
# * hidden till clicked 2
# ====
#
#
class ViewResultDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
use_dsl
at_location :head
def process doc
styles = %(<style>
.listingblock a.view-result {
float: right;
font-weight: normal;
text-decoration: none;
font-size: 0.9em;
line-height: 1.4;
margin-top: 0.15em;
}
</style>)
scripts = %(<script>
function toggle_result_block(e) {
var resultNode = e.target.parentNode.parentNode.nextElementSibling;
resultNode.style.display = getComputedStyle(resultNode).display == 'none' ? '' : 'none';
return false;
}
document.addEventListener('DOMContentLoaded', function() {
[].forEach.call(document.querySelectorAll('.result'), function(resultNode) {
resultNode.style.display = 'none';
var viewLink = document.createElement('a');
viewLink.className = 'view-result';
viewLink.appendChild(document.createTextNode('[ view result ]'));
resultNode.previousElementSibling.querySelector('.title').appendChild(viewLink);
viewLink.addEventListener('click', toggle_result_block);
});
});
</script>);
[styles, scripts].join "\n"
end
end