Module: Commonmarker::Merge

Defined in:
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

Defined Under Namespace

Modules: Backend, DebugLogger, Version Classes: FileAnalysis, PartialTemplateMerger, 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



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

def available_markdown_backends
  [BACKEND_REFERENCE]
end

.markdown_backend_feature_profile(backend: nil) ⇒ Object



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

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



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

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

.markdown_feature_profileObject



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

def markdown_feature_profile
  Markdown::Merge.markdown_feature_profile
end

.markdown_plan_context(backend: nil) ⇒ Object



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

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



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

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

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



86
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
# File 'lib/commonmarker/merge.rb', line 86

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



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

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



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

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



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

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



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

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



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

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

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



54
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
# File 'lib/commonmarker/merge.rb', line 54

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

.unsupported_feature_result(message) ⇒ Object



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

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