forked from boostorg/boostlook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboostlook.rb
76 lines (65 loc) · 2.78 KB
/
boostlook.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
Asciidoctor::Extensions.register do
postprocessor do
process do |doc, output|
output = output.sub(/<body(.*?)>/, '<body\\1><div class="boostlook">')
output = output.sub(/<\/body>/, "</div></body>")
output = output.sub(/(<div id="toc".*?>)/, '<button id="toggle-toc" title="Show Table of Contents" aria-expanded="false" aria-controls="toc">☰</button>\\1')
output = output.sub(/(<div id="footer".*?>)/, '</div>\\1')
inline_script = <<~SCRIPT
<script>
(function() {
var isVisible = localStorage.getItem('tocVisible') === 'true';
var isPinned = localStorage.getItem('tocPinned') === 'true';
document.documentElement.classList.toggle('toc-visible', isVisible);
document.documentElement.classList.toggle('toc-hidden', !isVisible);
document.documentElement.classList.toggle('toc-pinned', isPinned);
})();
</script>
SCRIPT
output = output.sub(/<\/head>/, "#{inline_script}</head>")
script_tag = <<~SCRIPT
<script>
document.addEventListener("DOMContentLoaded", (event) => {
const tocButton = document.getElementById("toggle-toc");
const toc = document.getElementById("toc");
const html = document.documentElement;
let isVisible = localStorage.getItem("tocVisible") === "true";
let isPinned = localStorage.getItem("tocPinned") === "true";
function updateTocVisibility(visible) {
html.classList.toggle("toc-visible", visible);
html.classList.toggle("toc-hidden", !visible);
tocButton.setAttribute("aria-expanded", visible);
tocButton.textContent = visible ? "×" : "☰";
tocButton.setAttribute("title", visible ? "Hide Table of Contents" : "Show Table of Contents");
}
function updateTocPinned(pinned) {
html.classList.toggle("toc-pinned", pinned);
localStorage.setItem("tocPinned", pinned);
}
tocButton.addEventListener("click", () => {
isVisible = !isVisible;
isPinned = !isPinned;
updateTocVisibility(isVisible);
updateTocPinned(isPinned);
localStorage.setItem("tocVisible", isVisible);
localStorage.setItem("tocPinned", isPinned);
});
tocButton.addEventListener("mouseenter", () => {
if (!isVisible) {
updateTocVisibility(true);
}
});
toc.addEventListener("mouseleave", () => {
if (!isVisible) {
updateTocVisibility(false);
}
});
updateTocVisibility(isVisible);
});
</script>
SCRIPT
output = output.sub(/<\/body>/, "#{script_tag}</body>")
output
end
end
end