Module: Psych::Comments::NodeUtils

Defined in:
lib/psych/comments/emitter.rb

Class Method Summary collapse

Class Method Details

.has_anchor(node) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/psych/comments/emitter.rb', line 53

module_function def has_anchor(node)
  case node
  when Psych::Nodes::Scalar, Psych::Nodes::Mapping, Psych::Nodes::Sequence
    !!node.anchor
  else
    false
  end
end

.has_bullet(node) ⇒ Object



49
50
51
# File 'lib/psych/comments/emitter.rb', line 49

module_function def has_bullet(node)
  node.is_a?(Psych::Nodes::Sequence) && !node.children.empty?
end

.single_line(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/psych/comments/emitter.rb', line 38

module_function def single_line(node)
  case node
  when Psych::Nodes::Scalar, Psych::Nodes::Alias
    node.leading_comments.empty? && node.trailing_comments.empty?
  when Psych::Nodes::Mapping, Psych::Nodes::Sequence
    node.children.empty?
  else
    false
  end
end

.stringify_adjust_scalar(node, indent_str = 0) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/psych/comments/emitter.rb', line 19

module_function def stringify_adjust_scalar(node, indent_str = 0)
  node2 = Psych::Nodes::Scalar.new(node.value, nil, nil, node.plain, node.quoted, node.style)
  if node.tag
    if node.style == Psych::Nodes::Scalar::PLAIN
      node2.plain = true
    else
      node2.quoted = true
    end
  end

  s = stringify_node(node2).sub(/\n\z/, "")
  if node.style == Psych::Nodes::Scalar::DOUBLE_QUOTED || node.style == Psych::Nodes::Scalar::SINGLE_QUOTED || node.style == Psych::Nodes::Scalar::PLAIN
    s = s.gsub(/\s*\n\s*/, " ")
  else
    s = s.gsub(/\n/, "\n#{indent_str}")
  end
  s.gsub(/\n\s+$/, "\n")
end

.stringify_node(node) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/psych/comments/emitter.rb', line 4

module_function def stringify_node(node)
  case node
  when Psych::Nodes::Stream
    node.to_yaml
  when Psych::Nodes::Document
    strm = Psych::Nodes::Stream.new
    strm.children << node
    stringify_node(strm)
  else
    doc = Psych::Nodes::Document.new([], [], true)
    doc.children << node
    stringify_node(doc)
  end
end