Created
March 21, 2018 16:37
-
-
Save otobrglez/9dd64f1a1fb3bb5afa214095ee0cf4c9 to your computer and use it in GitHub Desktop.
Transforming yaml to another yaml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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