Module: LexxyVariables

Defined in:
lib/lexxy_variables.rb,
lib/lexxy_variables/engine.rb,
lib/lexxy_variables/helper.rb,
lib/lexxy_variables/version.rb,
lib/lexxy_variables/pipeline.rb,
lib/lexxy_variables/registry.rb,
lib/lexxy_variables/attachable.rb,
lib/lexxy_variables/placeholder.rb,
lib/lexxy_variables/configuration.rb,
lib/lexxy_variables/attachment_type.rb,
lib/lexxy_variables/renderers/liquid.rb,
lib/lexxy_variables/renderers/substitution.rb

Overview

Insert and safely resolve variable/attachment tokens in Lexxy rich text.

The gem owns the mechanism: the editor extension, the nonce-safe render pipeline, the attachment-type registry, and the renderers. The host app owns the policy: what variables exist (catalog), what a key resolves to (assigns), and any extra attachment types (register_attachment, e.g. snippets).

Defined Under Namespace

Modules: Attachable, Helper, Placeholder, Renderers Classes: AttachmentType, Configuration, Engine, Pipeline, Registry

Constant Summary collapse

VARIABLE_CONTENT_TYPE =

Default content-type carried by variable attachment chips. A host may register additional types (snippets, etc.) via Configuration#register_attachment.

"application/vnd.actiontext.variable"
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.attachable_from(node) ⇒ Object

Resolves an attachment node's sgid to its attachable, or nil if the sgid is missing or stale. Handy for host-supplied :value / :fragment resolvers.



46
47
48
49
50
# File 'lib/lexxy_variables.rb', line 46

def attachable_from(node)
  ActionText::Attachable.from_attachable_sgid(node["sgid"])
rescue StandardError
  nil
end

.block_content_type?(content_type) ⇒ Boolean

True when a content-type is registered as a :fragment type (e.g. snippets), so its chip should render in the block style.

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/lexxy_variables.rb', line 110

def block_content_type?(content_type)
  type = registered_type(content_type)
  type ? type.fragment? : false
end

.chip_key(node) ⇒ Object

Reads the key that lexxy_variable_chip embeds as data-lexxy-key in the chip content, or nil if absent. This is how a plain-hash catalog (which has no sgid-backed attachable) maps a chip back to its catalog key at render.



55
56
57
58
59
60
61
62
63
64
# File 'lib/lexxy_variables.rb', line 55

def chip_key(node)
  content = node["content"]
  return nil if content.nil? || content.empty?

  span = Nokogiri::HTML5.fragment(content).at_css("[data-lexxy-key]")
  key = span && span["data-lexxy-key"]
  key unless key.nil? || key.empty?
rescue StandardError
  nil
end

.configObject



35
36
37
# File 'lib/lexxy_variables.rb', line 35

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



30
31
32
33
# File 'lib/lexxy_variables.rb', line 30

def configure
  yield config
  config
end

.item_content_type(item) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/lexxy_variables.rb', line 100

def item_content_type(item)
  if item.is_a?(Hash)
    item[:content_type]
  elsif item.respond_to?(:attachable_content_type)
    item.attachable_content_type
  end
end

.item_key(item) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/lexxy_variables.rb', line 76

def item_key(item)
  if item.is_a?(Hash)
    item[:key]
  else
    item.key
  end
end

.item_name(item) ⇒ Object

Catalog items can be plain hashes or any objects that respond to the same fields. These accessors read the fields the prompt needs either way.



68
69
70
71
72
73
74
# File 'lib/lexxy_variables.rb', line 68

def item_name(item)
  if item.is_a?(Hash)
    item[:name]
  else
    item.name
  end
end

.item_sgid(item) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/lexxy_variables.rb', line 92

def item_sgid(item)
  if item.is_a?(Hash)
    item[:attachable_sgid]
  else
    item.attachable_sgid
  end
end

.item_type_label(item) ⇒ Object

The badge label for an item's type, or nil if none is registered.



116
117
118
# File 'lib/lexxy_variables.rb', line 116

def item_type_label(item)
  registered_type(item_content_type(item))&.label
end

.item_value(item) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/lexxy_variables.rb', line 84

def item_value(item)
  if item.is_a?(Hash)
    item[:value]
  else
    item.value
  end
end

.reset_config!Object

Primarily for tests / reloading.



40
41
42
# File 'lib/lexxy_variables.rb', line 40

def reset_config!
  @config = Configuration.new
end