module Octopress::Ink::Utils

Public Instance Methods

pretty_print_yaml(yaml) click to toggle source
# File lib/octopress-ink/utils.rb, line 8
def pretty_print_yaml(yaml)
  # Use json pretty_print, but make it look like yaml
  #
  JSON.pretty_generate(yaml)
    .sub(/\A{\n/,'')          # remove leading {
    .sub(/}\z/,'')            # remove trailing }
    .gsub(/^/,' ')            # indent
    .gsub(/"(.+?)":/,'\1:')   # remove quotes around keys
    .gsub(/,$/,'')            # remove commas from end of lines
    .gsub(/{\n/,"\n")         # remove keys with empty hashes
    .gsub(/^\s+}\n/,'')       # remove keys with empty hashes
    .gsub(/\[\s+\]/,'[]')       # remove whitespace in empty arrays
end