Module: Ast::Crispr::Markdown::Markly::Selectors

Defined in:
lib/ast/crispr/markdown/markly.rb

Class Method Summary collapse

Class Method Details

.heading_section(heading_text:, level: nil, id: nil, limit: nil, metadata: {}, **options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ast/crispr/markdown/markly.rb', line 106

def heading_section(heading_text:, level: nil, id: nil, limit: nil, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || "heading_section_#{heading_text}",
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :heading_sections,
      selector_kind: :heading_section,
      selection_intent: :section_branch,
      include_trailing_gap: false
    ).merge(options),
    locate: lambda do |context|
      context.structural_owners(owner_scope: :heading_sections).filter_map do |owner|
        next unless owner.heading_text.to_s.strip == heading_text.to_s.strip
        next if level && owner.level != level

        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: owner.location.end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: :owner_end,
            payload_kind: :section_branch,
            heading_text: owner.heading_text,
            level: owner.level,
            base: owner.base
          }
        )
      end
    end
  )
end

.html_comment(text:, id: nil, limit: nil, metadata: {}, **options) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ast/crispr/markdown/markly.rb', line 173

def html_comment(text:, id: nil, limit: nil, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || "html_comment_#{text}",
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :html_comments,
      selector_kind: :html_comment,
      selection_intent: :predicate_filter,
      include_trailing_gap: false
    ).merge(options),
    locate: lambda do |context|
      context.structural_owners(owner_scope: :html_comments).filter_map do |owner|
        next unless owner.text.to_s == text.to_s

        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: owner.location.end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: :owner_end,
            payload_kind: :structural_owner_body,
            text: owner.text
          }
        )
      end
    end
  )
end

.html_comment_block(start_text:, end_text:, id: nil, limit: nil, span: :nearest, include_trailing_gap: false, metadata: {}, **options) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ast/crispr/markdown/markly.rb', line 273

def html_comment_block(start_text:, end_text:, id: nil, limit: nil, span: :nearest,
                       include_trailing_gap: false, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || "html_comment_block_#{start_text}",
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :html_comments,
      selector_kind: :html_comment_block,
      selection_intent: :predicate_filter,
      include_trailing_gap: include_trailing_gap
    ).merge(options),
    locate: lambda do |context|
      comments = context.structural_owners(owner_scope: :html_comments)
      if span.to_sym == :outermost
        opening = comments.find { |comment| comment.text.to_s == start_text.to_s }
        closing = comments.reverse.find do |comment|
          opening && comment.text.to_s == end_text.to_s && comment.location.end_line >= opening.location.start_line
        end
        next [] unless opening && closing

        end_line = closing.location.end_line
        end_line = context.expand_following_gap(end_line) if include_trailing_gap
        next [
          Ast::Crispr::Match.new(
            node: opening,
            start_line: opening.location.start_line,
            end_line: end_line,
            metadata: {
              start_boundary: :owner_start,
              end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
              payload_kind: :structural_owner_body,
              start_text: start_text,
              end_text: end_text
            }
          )
        ]
      end

      comments.each_with_index.filter_map do |owner, index|
        next unless owner.text.to_s == start_text.to_s

        closing = comments[index + 1..]&.find { |comment| comment.text.to_s == end_text.to_s }
        next unless closing

        end_line = closing.location.end_line
        end_line = context.expand_following_gap(end_line) if include_trailing_gap
        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
            payload_kind: :structural_owner_body,
            start_text: start_text,
            end_text: end_text
          }
        )
      end
    end
  )
end

.inline_reference(label: nil, reference_kind: nil, id: nil, limit: nil, metadata: {}, **options) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/ast/crispr/markdown/markly.rb', line 237

def inline_reference(label: nil, reference_kind: nil, id: nil, limit: nil, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || ['inline_reference', label, reference_kind].compact.join(':'),
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :inline_references,
      selector_kind: :inline_reference,
      selection_intent: :predicate_filter,
      include_trailing_gap: false
    ).merge(options),
    locate: lambda do |context|
      context.structural_owners(owner_scope: :inline_references).filter_map do |owner|
        next if label && !owner.labels.include?(label.to_s)
        next if reference_kind && owner.reference_kind != reference_kind.to_sym

        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: owner.location.end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: :owner_end,
            payload_kind: :structural_owner_body,
            label: owner.label,
            labels: owner.labels,
            reference_kind: owner.reference_kind,
            start_column: owner.start_column,
            end_column: owner.end_column
          }
        )
      end
    end
  )
end


140
141
142
143
144
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
# File 'lib/ast/crispr/markdown/markly.rb', line 140

def link_definition(label: nil, url: nil, id: nil, limit: nil, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || ['link_definition', label, url].compact.join(':'),
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :link_definitions,
      selector_kind: :link_definition,
      selection_intent: :predicate_filter,
      include_trailing_gap: false
    ).merge(options),
    locate: lambda do |context|
      context.structural_owners(owner_scope: :link_definitions).filter_map do |owner|
        next if label && owner.label.to_s != label.to_s
        next if url && owner.url.to_s != url.to_s

        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: owner.location.end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: :owner_end,
            payload_kind: :structural_owner_body,
            label: owner.label,
            url: owner.url
          }
        )
      end
    end
  )
end

.list_item(text: nil, id: nil, limit: nil, metadata: {}, **options) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ast/crispr/markdown/markly.rb', line 204

def list_item(text: nil, id: nil, limit: nil, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || ['list_item', text].compact.join(':'),
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Markdown::Markly.adapter,
      owner_scope: :list_items,
      selector_kind: :list_item,
      selection_intent: :predicate_filter,
      include_trailing_gap: false
    ).merge(options),
    locate: lambda do |context|
      context.structural_owners(owner_scope: :list_items).filter_map do |owner|
        next if text && !owner.text.to_s.include?(text.to_s)

        Ast::Crispr::Match.new(
          node: owner,
          start_line: owner.location.start_line,
          end_line: owner.location.end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: :owner_end,
            payload_kind: :structural_owner_body,
            depth: owner.depth,
            marker: owner.marker,
            text: owner.text
          }
        )
      end
    end
  )
end