1# Copyright 2016-2023 The Khronos Group Inc.
2#
3# SPDX-License-Identifier: Apache-2.0
4
5# This script adds CSS and markup to indicate the document is (perhaps
6# slowly) loading. It also inserts HTML comments marking where JavaScript
7# and HTML specific to the chunked HTML output target should be inserted.
8
9require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
10
11include ::Asciidoctor
12
13class MakeHtmlLoadable < Extensions::Postprocessor
14
15  def process document, output
16
17    if document.attr? 'stem'
18
19      loading_msg = '<div id="loading_msg" class="hidden" hidden><p>Loading&hellip; please wait.</p></div>'
20      loadable_class = 'class="loadable"'
21
22      loaded_script = '
23<!--ChunkedSearchJSMarker-->
24<style>
25    #loading_msg {
26        width: 100%;
27        margin-left: auto;
28        margin-right: auto;
29        margin-top: 1ex;
30        margin-bottom: 1ex;
31        max-width: 62.5em;
32        position: relative;
33        padding-left: 1.5em;
34        padding-right: 1.5em;
35    }
36    .hidden {display: none;}
37</style>
38<script>
39    function hideElement(e){
40        e.setAttribute("hidden", "");
41        e.classList.add("hidden");
42    }
43
44    function unhideElement(e){
45        e.classList.remove("hidden");
46        e.removeAttribute("hidden");
47    }
48
49    function hideLoadableContent(){
50        unhideElement( document.getElementById("loading_msg") );
51        for( var loadable of document.getElementsByClassName("loadable") ) hideElement(loadable);
52    }
53
54    function unhideLoadableContent(){
55        hideElement( document.getElementById("loading_msg") );
56        for( var loadable of document.getElementsByClassName("loadable") ) unhideElement(loadable);
57    }
58
59    window.addEventListener("load", unhideLoadableContent);
60</script>
61'
62
63      hide_script = '<script>hideLoadableContent();</script>'
64
65      output.sub! /(?=<\/head>)/, loaded_script
66      output.sub! /(<div id="content")/, '\1' + " " + loadable_class + " "
67      output.sub! /(<div id="content".*?>)/, '\1' + hide_script
68      output.sub! /(?=<div id="content")/, loading_msg + "\n" + "<!--ChunkedSearchboxMarker-->\n"
69    end
70    output
71  end
72end
73