Module: Asciidoctor::Html::Jupyterlite

Defined in:
lib/asciidoctor/html/jupyterlite.rb

Overview

Jupyterlite library

Constant Summary collapse

PATH =
"jupyterlite"
REPL_PATH =
"repl/index.html"
CONFIG =
{
  toolbar: true,
  kernel: "python",
  promptCellPosition: "left",
  clearCellsOnExecute: true,
  hideCodeInput: true,
  clearCodeContentOnExecute: false,
  showBanner: false,
  execute: false
}.freeze

Class Method Summary collapse

Class Method Details

.html(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/asciidoctor/html/jupyterlite.rb', line 38

def self.html(node)
  cell_id = "jupyter-cell-frame-#{node.attr "jupyter-cell-id"}"
  height_attr = %( style="height: #{node.attr("height")}px;") if node.attr?("height")
  <<~HTML
    <div id="#{cell_id}"#{height_attr} class="jupyter-cell"></div>
    <script>
      (function(){
        const cell = document.getElementById('#{cell_id}');
        addEventListener('load', () => {
          const frame = document.createElement('iframe');
          frame.src = '#{PATH}/#{repl_path_with_options node}';
          frame.width = '100%';
          frame.height = '100%';
          cell.appendChild(frame);
        });
      })();
    </script>
  HTML
end

.repl_path_with_options(node) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/html/jupyterlite.rb', line 21

def self.repl_path_with_options(node)
  query_string = ["code=#{URI.encode_uri_component node.content}"]
  query_string << CONFIG.merge(
    { promptCellPosition: node.attr("prompt-position", "left") }
  ).map do |k, v|
    value = if v == true
              1
            elsif v == false
              0
            else
              v
            end
    "#{k}=#{URI.encode_uri_component value}"
  end
  "#{REPL_PATH}?#{query_string.join "&"}"
end