Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Created March 21, 2018 16:37
Show Gist options
  • Save otobrglez/9dd64f1a1fb3bb5afa214095ee0cf4c9 to your computer and use it in GitHub Desktop.
Save otobrglez/9dd64f1a1fb3bb5afa214095ee0cf4c9 to your computer and use it in GitHub Desktop.
Transforming yaml to another yaml.
header:
label: 'Header'
breakpoint:
fallback:
name: header_fallback
label: 'Header @ fallback (306 x 172)'
width: 306
height: 172
small:
name: header_small
label: 'Header @ small (674 x 379)'
width: 674
height: 379
medium:
name: header_medium
label: 'Header @ medium (674 x 379)'
width: 674
height: 379
#!/usr/bin/env ruby
require 'yaml'
require 'pp'
require 'erb'
require 'ostruct'
SHOW_YAML = true
def render_breakpoint(template, data = {})
ERB.new(File.read(template)).result(OpenStruct.new(**data).instance_eval { binding })
end
def save_as_yaml(file_name, yaml)
puts "Writing to #{file_name}"
File.open(file_name, 'w') { |f| f.write yaml }
end
data = YAML.load_file('./data.yml')
data['header']['breakpoint'].each do |label, breakpoint|
content = render_breakpoint('./trans_template.yml.erb', {
header: data['header'],
label: label,
breakpoint: OpenStruct.new(breakpoint)
})
puts(content) if SHOW_YAML
yaml = YAML.load(content)
save_as_yaml("#{label}.yml", yaml.to_yaml)
end
name: <%= breakpoint.name %>
label: <%= breakpoint.label %>
effects:
id: image_scale_and_crop
weight: 1
data:
width: <%= breakpoint.width %>
height: <%= breakpoint.height %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment