Class: AlMarimo::EmbedTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/al_marimo.rb

Overview

al_marimo_embed src="https://marimo.app/l/abc" height="600px" caption="..." %

Constant Summary collapse

ATTRIBUTES =
/(\w+)\s*=\s*("[^"]*"|'[^']*'|[^\s]+)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ EmbedTag

Returns a new instance of EmbedTag.



137
138
139
140
141
142
143
# File 'lib/al_marimo.rb', line 137

def initialize(tag_name, markup, options)
  super
  @attributes = {}
  markup.scan(ATTRIBUTES) do |key, value|
    @attributes[key] = value.gsub(/\A["']|["']\z/, "")
  end
end

Instance Method Details

#render(context) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/al_marimo.rb', line 145

def render(context)
  site = context.registers[:site]
  return "" unless site

  src = resolve(@attributes["src"], context)
  url = AlMarimo.build_url(
    src,
    embed: @attributes.fetch("embed", "true") != "false",
    mode: @attributes["mode"] || "read"
  )
  # A missing or empty src is an authoring mistake. Emit nothing rather than
  # an iframe pointed at the site itself, which would silently render the
  # page inside its own post.
  return "" if url.nil?

  height = @attributes["height"] || "600px"
  width = @attributes["width"] || "100%"
  title = @attributes["title"] || "marimo notebook"
  caption = @attributes["caption"]
  extra_class = @attributes["class"]

  figure = +%(<figure class="al-marimo">)
  figure << %(<div class="al-marimo-embed">)
  figure << %(<iframe src="#{AlMarimo.escape(url)}")
  figure << %( class="#{AlMarimo.escape(extra_class)}") if extra_class
  # Deliberately omits allow-same-origin: the notebook is third-party code,
  # and granting it same-origin access to the embedding site would let it
  # read the parent document's storage and cookies.
  figure << %( sandbox="allow-scripts allow-downloads allow-popups allow-forms")
  figure << %( width="#{AlMarimo.escape(width)}" height="#{AlMarimo.escape(height)}")
  figure << %( title="#{AlMarimo.escape(title)}" loading="lazy" frameborder="0" allowfullscreen></iframe>)
  figure << %(</div>)
  figure << %(<figcaption class="caption">#{AlMarimo.escape(caption)}</figcaption>) if caption
  figure << %(</figure>)
  figure
end