Module: Commonmarker::Merge

Defined in:
lib/commonmarker/merge/provider.rb,
lib/commonmarker/merge.rb,
lib/commonmarker/merge/backend.rb,
lib/commonmarker/merge/version.rb,
lib/commonmarker/merge/freeze_node.rb,
lib/commonmarker/merge/debug_logger.rb,
lib/commonmarker/merge/smart_merger.rb,
lib/commonmarker/merge/file_analysis.rb,
lib/commonmarker/merge/comment_tracker.rb,
lib/commonmarker/merge/partial_template_merger.rb,
sig/commonmarker/merge.rbs

Overview

Source-preserving Markdown provider integration for Commonmarker.

Defined Under Namespace

Modules: Backend, DebugLogger, Version Classes: FileAnalysis, PartialTemplateMerger, Provider, SmartMerger

Constant Summary collapse

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

Current gem version exposed at the traditional constant location.

Returns:

  • (String)
Version::VERSION
FreezeNode =
Markdown::Merge::FreezeNode
CommentTracker =
Markdown::Merge::CommentTracker

Class Method Summary collapse

Class Method Details

.available_markdown_backendsObject



28
29
30
# File 'lib/commonmarker/merge.rb', line 28

def available_markdown_backends
  [BACKEND_REFERENCE]
end

.markdown_backend_feature_profile(backend: nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/commonmarker/merge.rb', line 32

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



197
198
199
# File 'lib/commonmarker/merge.rb', line 197

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

.markdown_feature_profileObject



24
25
26
# File 'lib/commonmarker/merge.rb', line 24

def markdown_feature_profile
  Markdown::Merge.markdown_feature_profile
end

.markdown_plan_context(backend: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/commonmarker/merge.rb', line 41

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



83
84
85
# File 'lib/commonmarker/merge.rb', line 83

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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/commonmarker/merge.rb', line 87

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

  template = parse_markdown(template_source, dialect, backend: backend)
  return template unless template[:ok]

  destination = parse_markdown(destination_source, dialect, backend: backend)
  return destination unless destination[:ok]

  destination_sections = Markdown::Merge.collect_markdown_sections(
    destination.dig(:analysis, :normalized_source),
    destination.dig(:analysis, :owners)
  )
  template_sections = Markdown::Merge.collect_markdown_sections(
    template.dig(:analysis, :normalized_source),
    template.dig(:analysis, :owners)
  )
  destination_paths = destination_sections.to_h { |section| [section[:path], true] }
  merged_sections = destination_sections.map { |section| section[:text] }.reject(&:empty?) +
                    template_sections
                    .reject { |section| destination_paths[section[:path]] || section[:text].empty? }
                    .map { |section| section[:text] }

  {
    ok: true,
    diagnostics: [],
    output: "#{merged_sections.join("\n\n").strip}\n",
    policies: []
  }
end

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/commonmarker/merge.rb', line 121

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
  )
end

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/commonmarker/merge.rb', line 137

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
  )
end

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/commonmarker/merge.rb', line 152

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
  )
end

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/commonmarker/merge.rb', line 167

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
  )
end

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/commonmarker/merge.rb', line 182

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
  )
end

.merge_providerProvider

Returns:



30
31
32
# File 'lib/commonmarker/merge/provider.rb', line 30

def merge_provider
  @merge_provider ||= Provider.new
end

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/commonmarker/merge.rb', line 55

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

  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)


34
35
36
37
38
# File 'lib/commonmarker/merge/provider.rb', line 34

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



201
202
203
204
205
206
207
# File 'lib/commonmarker/merge.rb', line 201

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