Class: Html::Merge::CrisprAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/html/merge/crispr_adapter.rb

Defined Under Namespace

Classes: Location, Owner

Instance Method Summary collapse

Instance Method Details

#comment_region_text(_document, _comment_region) ⇒ Object

Raises:

  • (Ast::Crispr::Error)


53
54
55
# File 'lib/html/merge/crispr_adapter.rb', line 53

def comment_region_text(_document, _comment_region)
  raise Ast::Crispr::Error, "HTML CRISPR comment regions are not implemented"
end

#comment_regions_for(_document, _owner, region: :leading, owner_scope: :html_element) ⇒ Object

Raises:

  • (Ast::Crispr::Error)


46
47
48
49
50
51
# File 'lib/html/merge/crispr_adapter.rb', line 46

def comment_regions_for(_document, _owner, region: :leading, owner_scope: :html_element)
  raise Ast::Crispr::Error.new(
    "HTML CRISPR comment regions are not implemented",
    details: { region: region, owner_scope: owner_scope }
  )
end

#read_ast(document) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/html/merge/crispr_adapter.rb', line 11

def read_ast(document)
  result = Html::Merge.parse_html(document.content)
  unless result.fetch(:ok)
    raise Ast::Crispr::Error.new(
      "Unable to parse HTML for CRISPR",
      details: { source_label: document.source_label, diagnostics: result.fetch(:diagnostics) }
    )
  end

  result
end

#structural_owners(document, owner_scope: :html_element) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/html/merge/crispr_adapter.rb', line 23

def structural_owners(document, owner_scope: :html_element)
  unless owner_scope.to_sym == :html_element
    raise Ast::Crispr::Error.new("Unsupported HTML CRISPR owner scope",
                                 details: { owner_scope: owner_scope })
  end

  root = Html::Merge.html_root_node(document.content)
  owners = []
  Html::Merge.visit_html_node(root) do |node|
    next unless node.structural?
    next unless node.type == "element"

    span = Html::Merge.html_node_span(node)
    owners << Owner.new(
      node: node,
      type: node.type,
      text: node.text.to_s,
      location: Location.new(start_line: span.fetch(:start_line), end_line: span.fetch(:end_line))
    )
  end
  owners
end

#structure_profile(owner_scope: :html_element) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/html/merge/crispr_adapter.rb', line 57

def structure_profile(owner_scope: :html_element)
  Ast::Crispr::StructureProfile.new(
    owner_scope: owner_scope,
    owner_selector: :html_element,
    metadata: { source: :html_merge }
  )
end