Module: MarkdownComposer::Capabilities

Defined in:
lib/markdown_composer/capabilities.rb

Constant Summary collapse

VERSION =
1

Class Method Summary collapse

Class Method Details

.build(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/markdown_composer/capabilities.rb', line 9

def build(options = {})
  registries = Registries.default
  json_compatible({
    version: VERSION,
    headless: true,
    fields: ,
    formats: ,
    sources: entries(registries.sources, :source, options),
    actions: entries(registries.actions, :action, options),
    targets: entries(registries.targets, :target, options),
    unit_tokens: entries(registries.unit_tokens, nil, options),
    take: entries(registries.take, :take, options),
    where: {
      fields: entries(registries.condition_fields, nil, options),
      predicates: entries(registries.predicates, :predicate, options),
      groups: entries(registries.where_groups, nil, options),
      condition_map: registries.conditions
    },
    transforms: transform_entries(registries.transforms, options),
    outputs: ,
    diagnostic_code_families: diagnostic_code_families,
    policy_options: policy_options
  })
end

.diagnostic_code_familiesObject



199
200
201
# File 'lib/markdown_composer/capabilities.rb', line 199

def diagnostic_code_families
  %w[action json output source selector take target token transform where yaml]
end

.enabled_status?(status) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/markdown_composer/capabilities.rb', line 126

def enabled_status?(status)
  !%w[disabled disabled_optional disabled_policy policy_gated].include?(status.to_s)
end

.entries(registry, support_key, options) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/markdown_composer/capabilities.rb', line 34

def entries(registry, support_key, options)
  registry.to_a.map do |entry|
    support = entry[:support] || entry["support"] || {}
    base = entry.dup
    base[:support_status] = support_key ? support_status(support[support_key] || support[support_key.to_s], options) : support_status_from_support_hash(support, options)
    base[:enabled] = enabled_status?(base[:support_status])
    base
  end
end

.field_metadataObject



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
181
182
183
184
185
186
187
188
189
190
# File 'lib/markdown_composer/capabilities.rb', line 145

def 
  {
    source: {
      label: "Source",
      tooltip: "First row normally uses current or explicit sources; previous falls back to current when no prior row exists. Later rows may use inherited, previous source, or previous output after a row has produced output.",
      first_row: %w[current explicit previous],
      later_rows: %w[current explicit inherited previous buffer]
    },
    document: {
      label: "Document",
      tooltip: "Select a host document when Source is Explicit or Inherited. Current Source uses the preview source supplied by the host."
    },
    select: {
      label: "Select",
      tooltip: "Selector token from the registry. Supports take and where syntax such as heading_2_section[first:1]."
    },
    include: {
      label: "Include",
      tooltip: "Include token from the registry. Use all for the full selected content or a unit token such as heading_title."
    },
    action: {
      label: "Action",
      tooltip: "Composition action from the registry. Some actions require a target."
    },
    target: {
      label: "Target",
      tooltip: "Target selector or position used by insert, replace, copy, move, remove_buffer_target, and transform_buffer_target actions. For modify with source buffer, in_place replaces selected buffer content inline."
    },
    transform: {
      label: "Transform",
      tooltip: "Registered transform. GUIs should filter transforms and modes by enabled capability metadata."
    },
    mode: {
      label: "Mode",
      tooltip: "Transform mode from the capability contract. Some modes require options or HTML output."
    },
    scope: {
      label: "Scope",
      tooltip: "Transform scope selector. Usually output or a registry selector such as heading_2_section."
    },
    options: {
      label: "Options JSON",
      tooltip: "JSON object passed to the selected transform mode. Required and optional keys come from capabilities."
    }
  }
end

.format_metadataObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/markdown_composer/capabilities.rb', line 130

def 
  {
    input: [
      { from: "markdown", support_status: "normal", fidelity: "source" },
      { from: "html", support_status: "best_effort", fidelity: "lossy" }
    ],
    conversions: [
      { from: "markdown", to: "markdown", support_status: "normal", fidelity: "source" },
      { from: "markdown", to: "html", support_status: "normal", fidelity: "rendered" },
      { from: "html", to: "markdown", support_status: "best_effort", fidelity: "lossy" },
      { from: "html", to: "html", support_status: "best_effort", fidelity: "lossy_intermediate_markdown" }
    ]
  }
end

.json_compatible(value) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/markdown_composer/capabilities.rb', line 210

def json_compatible(value)
  case value
  when Hash
    value.each_with_object({}) { |(key, child), hash| hash[key.to_s] = json_compatible(child) }
  when Array
    value.map { |child| json_compatible(child) }
  when Symbol
    value.to_s
  else
    value
  end
end

.output_metadataObject



192
193
194
195
196
197
# File 'lib/markdown_composer/capabilities.rb', line 192

def 
  [
    { token: "markdown", aliases: [ "md" ], label: "Markdown", support_status: "normal" },
    { token: "html", aliases: [], label: "HTML", support_status: "normal" }
  ]
end

.policy_optionsObject



203
204
205
206
207
208
# File 'lib/markdown_composer/capabilities.rb', line 203

def policy_options
  [
    { token: "adapter_policy_tokens", default: false, unlocks: "host-policy token families such as raw_html" },
    { token: "adapter_transforms", default: false, unlocks: "host-policy transform families" }
  ]
end

.required_output(transform, mode) ⇒ Object



93
94
95
96
97
# File 'lib/markdown_composer/capabilities.rb', line 93

def required_output(transform, mode)
  return "html" if transform.to_s == "links" && %w[nofollow target_blank].include?(mode.to_s)

  nil
end

.required_policy_options(transform, mode) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/markdown_composer/capabilities.rb', line 99

def required_policy_options(transform, mode)
  transform = transform.to_s
  mode = mode.to_s
  return [ "adapter_transforms" ] if transform == "adapter" || transform == "sanitise"

  []
end

.support_status(value, options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/markdown_composer/capabilities.rb', line 107

def support_status(value, options)
  case value.to_sym
  when :normal then "normal"
  when :advanced then "advanced"
  when :output_only then "output_only"
  when :include_only then "include_only"
  when :optional then options.fetch(:optional_tokens, false) ? "optional" : "disabled_optional"
  when :adapter_policy then options.fetch(:adapter_policy_tokens, false) ? "policy_gated" : "disabled_policy"
  else "disabled"
  end
end

.support_status_from_support_hash(support, options) ⇒ Object



119
120
121
122
123
124
# File 'lib/markdown_composer/capabilities.rb', line 119

def support_status_from_support_hash(support, options)
  statuses = support.values.map { |value| support_status(value, options) }
  return "disabled" if statuses.empty?

  %w[normal include_only output_only advanced optional policy_gated].find { |status| statuses.include?(status) } || statuses.first
end

.transform_entries(registry, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/markdown_composer/capabilities.rb', line 44

def transform_entries(registry, options)
  registry.entries.map do |entry|
    modes = Array(entry.support[:modes])
     = modes.map { |mode| (entry, mode, options) }
    support_status = support_status(entry.support[:transform], options)
    {
      token: entry.token,
      aliases: entry.aliases,
      label: entry.label,
      tooltip: entry.tooltip,
      meaning: entry.meaning,
      row_sentence: entry.row_sentence,
      support: entry.support,
      support_status: support_status,
      enabled: enabled_status?(support_status),
      modes: ,
      source_formats: entry.source_formats,
      condition_fields: entry.condition_fields
    }
  end
end

.transform_mode_metadata(entry, mode, options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/markdown_composer/capabilities.rb', line 66

def (entry, mode, options)
  required = TransformOptions.required(entry.token, mode)
  optional = TransformOptions.optional(entry.token, mode, adapter_option_keys: options[:adapter_option_keys])
  status = transform_mode_status(entry.token, mode, options)
  {
    token: mode,
    label: mode.to_s.split("_").map(&:capitalize).join(" "),
    support_status: status,
    enabled: enabled_status?(status),
    required_options: required,
    optional_options: optional,
    policy_gated: %w[policy_gated disabled_policy].include?(status),
    required_output: required_output(entry.token, mode),
    required_policy_options: required_policy_options(entry.token, mode)
  }
end

.transform_mode_status(transform, mode, options) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/markdown_composer/capabilities.rb', line 83

def transform_mode_status(transform, mode, options)
  transform = transform.to_s
  mode = mode.to_s
  return "policy_gated" if transform == "adapter"
  return "policy_gated" if transform == "sanitise"
  return "policy_gated" if transform == "order" && mode == "target_order"

  "normal"
end