diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /scripts/lib/glfm/shared.rb | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'scripts/lib/glfm/shared.rb')
-rw-r--r-- | scripts/lib/glfm/shared.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/scripts/lib/glfm/shared.rb b/scripts/lib/glfm/shared.rb index f11c66eb8be..b529d9ba94f 100644 --- a/scripts/lib/glfm/shared.rb +++ b/scripts/lib/glfm/shared.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'fileutils' require 'open3' +require 'active_support/core_ext/hash/keys' module Glfm module Shared @@ -39,5 +40,38 @@ module Glfm warn(stdout_and_stderr_str) raise end + + # Construct an AST so we can control YAML formatting for + # YAML block scalar literals and key quoting. + # + # Note that when Psych dumps the markdown to YAML, it will + # automatically use the default "clip" behavior of the Block Chomping Indicator (`|`) + # https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator, + # when the markdown strings contain a trailing newline. The type of + # Block Chomping Indicator is automatically determined, you cannot specify it + # manually. + def dump_yaml_with_formatting(hash, literal_scalars: false) + stringified_keys_hash = hash.deep_stringify_keys + visitor = Psych::Visitors::YAMLTree.create + visitor << stringified_keys_hash + ast = visitor.tree + + # Force all scalars to have literal formatting (using Block Chomping Indicator instead of quotes) + if literal_scalars + ast.grep(Psych::Nodes::Scalar).each do |node| + node.style = Psych::Nodes::Scalar::LITERAL + end + end + + # Do not quote the keys + ast.grep(Psych::Nodes::Mapping).each do |node| + node.children.each_slice(2) do |k, _| + k.quoted = false + k.style = Psych::Nodes::Scalar::PLAIN + end + end + + ast.to_yaml + end end end |