Class: LiquidXlsx::Tags::ImageTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/liquid_xlsx/tags/image_tag.rb

Overview

Handles the image_tag % Liquid tag for inserting images.

Syntax:

{% image_tag <src_expr> [, colspan: N, rowspan: N, to: "F10",
                      width: N, height: N] %}

Collects image insertion operations in context.registers. Returns empty string (image lives in the drawing layer, not in the cell).

Constant Summary collapse

SYNTAX =
/\A\s*(.+?)\s*(?:,\s*(.+))?\s*\z/
OPT_PAIR =
/(\w+)\s*:\s*(?:"([^"]*)"|([^\s,]+))/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ ImageTag

Returns a new instance of ImageTag.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/liquid_xlsx/tags/image_tag.rb', line 17

def initialize(tag_name, markup, options)
  super

  match = markup.match(SYNTAX)
  raise_syntax("Expected: {% image_tag <expr> [, options] %}") unless match

  @src_expr = match[1]&.strip
  raise_syntax("Missing source expression") if @src_expr.nil? || @src_expr.empty?

  @options = {}
  match[2]&.scan(OPT_PAIR) do |key, quoted, bare|
    val = quoted || bare
    raise_syntax("Invalid option: #{key.inspect}") unless val

    @options[key] = normalize_option(key, val)
  end
end

Instance Method Details

#render(context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/liquid_xlsx/tags/image_tag.rb', line 35

def render(context)
  src_val = evaluate_expr(@src_expr, context)

  op = {
    op: :insert_image,
    source: src_val,
    sheet_r_id: context.registers[:liquid_xlsx_sheet_r_id],
    anchor_col: context.registers[:liquid_xlsx_anchor_col],
    anchor_row: context.registers[:liquid_xlsx_anchor_row],
    template_anchor_row: context.registers[:liquid_xlsx_template_anchor_row],
    colspan: @options["colspan"],
    rowspan: @options["rowspan"],
    to: @options["to"],
    width: @options["width"],
    height: @options["height"],
    source_sheet: context.registers[:liquid_xlsx_sheet_name],
    source_cell: context.registers[:liquid_xlsx_source_cell]
  }

  ops = context.registers[:liquid_xlsx_image_ops] ||= []
  ops << op

  ""
end