Skip to content

Instantly share code, notes, and snippets.

@we4tech
Created November 4, 2019 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save we4tech/438bf2f64e9c37cac023ede14bc2d31c to your computer and use it in GitHub Desktop.
Save we4tech/438bf2f64e9c37cac023ede14bc2d31c to your computer and use it in GitHub Desktop.
How to use double quoted a string node
class YAMLPreservedStringQuote
def initialize(*doublequoted_nodes)
@doublequoted_nodes = doublequoted_nodes
end
def load(str)
ast = YAML.parse_stream(StringIO.new(str))
ast.grep(Psych::Nodes::Mapping).each do |node|
node.children.each_slice(2) do |key, value|
next unless @doublequoted_nodes.include?(key.value)
if value.is_a?(Psych::Nodes::Sequence)
value.children.each(&method(:double_quote!))
else
double_quote!(value)
end
end
end
ast
end
private
def double_quote!(node)
node.plain = false
node.quoted = true
node.style = Psych::Nodes::Scalar::DOUBLE_QUOTED
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment