Class: Metanorma::Mirror::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/mirror/transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry: Mirror.default_registry, id_strategy: Mirror::DEFAULT_ID_STRATEGY) ⇒ Transformer

Returns a new instance of Transformer.



8
9
10
11
12
# File 'lib/metanorma/mirror/transformer.rb', line 8

def initialize(registry: Mirror.default_registry,
               id_strategy: Mirror::DEFAULT_ID_STRATEGY)
  @registry = registry
  @id_strategy = id_strategy
end

Instance Attribute Details

#id_strategyObject (readonly)

Returns the value of attribute id_strategy.



6
7
8
# File 'lib/metanorma/mirror/transformer.rb', line 6

def id_strategy
  @id_strategy
end

#registryObject (readonly)

Returns the value of attribute registry.



6
7
8
# File 'lib/metanorma/mirror/transformer.rb', line 6

def registry
  @registry
end

Instance Method Details

#call(root) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metanorma/mirror/transformer.rb', line 14

def call(root)
  attrs = {}
  attrs[:flavor] = root.flavor if root.flavor
  attrs[:type] = root.type if root.type
  attrs[:schema_version] = root.schema_version if root.schema_version

  title = extract_root_title(root)
  attrs[:title] = title if title

  content = []

  preface = root.preface
  if preface
    result = @registry.handle(preface, context: self)
    result.append_to(content)
  end

  sections = root.sections
  if sections
    result = @registry.handle(sections, context: self)
    result.append_to(content)
  end

  annex = root.annex
  annex&.each do |a|
    result = @registry.handle(a, context: self)
    result.append_to(content)
  end

  bibliography = root.bibliography
  if bibliography
    result = @registry.handle(bibliography, context: self)
    result.append_to(content)
  end

  Handlers.build_node("doc", attrs: attrs, content: content)
end

#extract_blocks(element) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/mirror/transformer.rb', line 61

def extract_blocks(element)
  content = []

  element.each_mixed_content do |node|
    next if node.is_a?(String)

    result = @registry.handle(node, context: self)
    result.append_to(content)
  end
  content
end

#extract_named_collections(element, collection_attrs) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/metanorma/mirror/transformer.rb', line 87

def extract_named_collections(element, collection_attrs)
  content = []
  collection_attrs.each do |attr|
    collection = SafeAttr.read(element, attr)
    next unless collection

    collection.each do |child|
      result = @registry.handle(child, context: self)
      result.append_to(content)
    end
  end
  content
end

#extract_root_title(root) ⇒ Object



105
106
107
# File 'lib/metanorma/mirror/transformer.rb', line 105

def extract_root_title(root)
  Metadata.title_from_bibdata(SafeAttr.read(root, :bibdata))
end

#extract_section_children(element) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/mirror/transformer.rb', line 73

def extract_section_children(element)
  content = []
  %i[clause terms definitions references floating_title].each do |attr|
    collection = SafeAttr.read(element, attr)
    next unless collection

    collection.each do |child|
      result = @registry.handle(child, context: self)
      result.append_to(content)
    end
  end
  content
end

#from_metanorma(root) ⇒ Object



52
53
54
55
# File 'lib/metanorma/mirror/transformer.rb', line 52

def from_metanorma(root)
  document = call(root)
  @id_strategy.finalize!(document)
end

#rewrite(mirror_node) ⇒ Object



57
58
59
# File 'lib/metanorma/mirror/transformer.rb', line 57

def rewrite(mirror_node)
  Rewriter.new.call(mirror_node)
end

#text_node(text, marks: []) ⇒ Object



101
102
103
# File 'lib/metanorma/mirror/transformer.rb', line 101

def text_node(text, marks: [])
  Handlers.build_text(text, marks: marks)
end