Module: Kramdown::Merge

Defined in:
lib/kramdown/merge/provider.rb,
lib/kramdown/merge.rb,
lib/kramdown/merge/backend.rb,
lib/kramdown/merge/version.rb,
sig/kramdown/merge.rbs

Overview

Source-preserving Markdown provider integration for Kramdown.

Defined Under Namespace

Modules: Backend, Version Classes: Provider

Constant Summary collapse

PACKAGE_NAME =
'kramdown-merge'
BACKEND_REFERENCE =
TreeHaver::BackendReference.new(id: 'kramdown', family: 'native').freeze
VERSION =

Current gem version exposed at the traditional constant location.

Returns:

  • (String)
Version::VERSION

Class Method Summary collapse

Class Method Details

.available_markdown_backendsObject



20
21
22
# File 'lib/kramdown/merge.rb', line 20

def available_markdown_backends
  [BACKEND_REFERENCE]
end

.markdown_backend_feature_profile(backend: nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/kramdown/merge.rb', line 24

def markdown_backend_feature_profile(backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  markdown_feature_profile.merge(backend: BACKEND_REFERENCE.id, backend_ref: BACKEND_REFERENCE.to_h)
end

.markdown_embedded_families(analysis) ⇒ Object



171
172
173
# File 'lib/kramdown/merge.rb', line 171

def markdown_embedded_families(analysis)
  Markdown::Merge.markdown_embedded_families(analysis)
end

.markdown_feature_profileObject



16
17
18
# File 'lib/kramdown/merge.rb', line 16

def markdown_feature_profile
  Markdown::Merge.markdown_feature_profile
end

.markdown_plan_context(backend: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kramdown/merge.rb', line 33

def markdown_plan_context(backend: nil)
  profile = markdown_backend_feature_profile(backend: backend)
  return profile if profile[:ok] == false

  {
    family_profile: markdown_feature_profile,
    feature_profile: {
      backend: profile[:backend],
      supports_dialects: true,
      supported_policies: []
    }
  }
end

.match_markdown_owners(template, destination) ⇒ Object



77
78
79
# File 'lib/kramdown/merge.rb', line 77

def match_markdown_owners(template, destination)
  Markdown::Merge.match_markdown_owners(template, destination)
end

.merge_markdown(template_source, destination_source, dialect, backend: nil) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/kramdown/merge.rb', line 81

def merge_markdown(template_source, destination_source, dialect, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown(template_source, destination_source, dialect, backend: BACKEND_REFERENCE.id)
end

.merge_markdown_with_reviewed_nested_outputs(template_source, destination_source, dialect, review_state, applied_children, backend: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kramdown/merge.rb', line 90

def merge_markdown_with_reviewed_nested_outputs(template_source, destination_source, dialect, review_state,
                                                applied_children, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown_with_reviewed_nested_outputs(
    template_source,
    destination_source,
    dialect,
    review_state,
    applied_children,
    backend: BACKEND_REFERENCE.id
  )
end

.merge_markdown_with_reviewed_nested_outputs_from_replay_bundle(template_source, destination_source, dialect, replay_bundle, backend: nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kramdown/merge.rb', line 107

def merge_markdown_with_reviewed_nested_outputs_from_replay_bundle(template_source, destination_source, dialect,
                                                                   replay_bundle, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown_with_reviewed_nested_outputs_from_replay_bundle(
    template_source,
    destination_source,
    dialect,
    replay_bundle,
    backend: BACKEND_REFERENCE.id
  )
end

.merge_markdown_with_reviewed_nested_outputs_from_replay_bundle_envelope(template_source, destination_source, dialect, envelope, backend: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kramdown/merge.rb', line 123

def merge_markdown_with_reviewed_nested_outputs_from_replay_bundle_envelope(template_source, destination_source,
                                                                            dialect, envelope, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown_with_reviewed_nested_outputs_from_replay_bundle_envelope(
    template_source,
    destination_source,
    dialect,
    envelope,
    backend: BACKEND_REFERENCE.id
  )
end

.merge_markdown_with_reviewed_nested_outputs_from_review_state(template_source, destination_source, dialect, review_state, backend: nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/kramdown/merge.rb', line 139

def merge_markdown_with_reviewed_nested_outputs_from_review_state(template_source, destination_source, dialect,
                                                                  review_state, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown_with_reviewed_nested_outputs_from_review_state(
    template_source,
    destination_source,
    dialect,
    review_state,
    backend: BACKEND_REFERENCE.id
  )
end

.merge_markdown_with_reviewed_nested_outputs_from_review_state_envelope(template_source, destination_source, dialect, envelope, backend: nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/kramdown/merge.rb', line 155

def merge_markdown_with_reviewed_nested_outputs_from_review_state_envelope(template_source, destination_source,
                                                                           dialect, envelope, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  Markdown::Merge.merge_markdown_with_reviewed_nested_outputs_from_review_state_envelope(
    template_source,
    destination_source,
    dialect,
    envelope,
    backend: BACKEND_REFERENCE.id
  )
end

.merge_providerProvider

Returns:



32
33
34
# File 'lib/kramdown/merge/provider.rb', line 32

def merge_provider
  @merge_provider ||= Provider.new
end

.parse_markdown(source, dialect, backend: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kramdown/merge.rb', line 47

def parse_markdown(source, dialect, backend: nil)
  requested = backend.to_s.empty? ? BACKEND_REFERENCE.id : backend.to_s
  unless requested == BACKEND_REFERENCE.id
    return unsupported_feature_result("Unsupported Markdown backend #{requested}.")
  end

  return unsupported_feature_result("Unsupported Markdown dialect #{dialect}.") unless dialect == 'markdown'

  TreeHaver.with_backend(BACKEND_REFERENCE.id) { TreeHaver.parser_for(:markdown).parse(source) }
  normalized = Markdown::Merge.normalize_source(source)
  {
    ok: true,
    diagnostics: [],
    analysis: {
      kind: 'markdown',
      dialect: dialect,
      normalized_source: normalized,
      root_kind: 'document',
      owners: Markdown::Merge.collect_markdown_owners(normalized)
    },
    policies: []
  }
rescue StandardError => e
  {
    ok: false,
    diagnostics: [{ severity: 'error', category: 'parse_error', message: e.message }],
    policies: []
  }
end

.register_provider!(replace: false) ⇒ Object

Parameters:

  • replace: (Boolean) (defaults to: false)

Returns:

  • (Object)


36
37
38
39
40
# File 'lib/kramdown/merge/provider.rb', line 36

def register_provider!(replace: false)
  return unless Ast::Merge.respond_to?(:register_provider)

  Ast::Merge.register_provider(merge_provider, replace: replace)
end

.unsupported_feature_result(message) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/kramdown/merge.rb', line 175

def unsupported_feature_result(message)
  {
    ok: false,
    diagnostics: [{ severity: 'error', category: 'unsupported_feature', message: message }],
    policies: []
  }
end