Module: Asciidoctor::Html::Jupyterlite

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

Overview

Jupyterlite library

Constant Summary collapse

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



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
# File 'lib/asciidoctor/html/jupyterlite.rb', line 37

def self.html(node)
  error_msg = "Set a <code>repl_url</code> in your <code>config.yaml</code>."
  return Utils.show_error(error_msg) unless node.document.attr("repl-url")

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

.repl_path_with_options(node) ⇒ Object



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

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", "top") }
  ).map do |k, v|
    value = if v == true
              1
            elsif v == false
              0
            else
              v
            end
    "#{k}=#{URI.encode_uri_component value}"
  end
  "#{node.document.attr "repl-url"}?#{query_string.join "&"}"
end