From 11eba46cc5b51b90732aeb783c06467da9100d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Wed, 10 Apr 2019 17:09:58 +0200 Subject: [PATCH 1/2] POC of using erb for wagon, the idea is to be able to includ file in content type, more to come --- .../steam/adapters/filesystem/yaml_loader.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb index ad017d76..02302624 100644 --- a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb +++ b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb @@ -1,3 +1,5 @@ +require 'erb' + module Locomotive::Steam module Adapters module Filesystem @@ -34,9 +36,18 @@ def _load(path, frontmatter = false, &block) end end + def parse_erb(data) + def inject(file_path) + File.open(File.join(path, file_path)).read + end + ERB.new(data).result(binding) + end + def safe_yaml_load(yaml, template, path, &block) return {} if yaml.blank? + yaml = parse_erb(yaml) + begin HashConverter.to_sym(YAML.load(yaml)).tap do |attributes| block.call(attributes, template) if block_given? @@ -49,7 +60,7 @@ def safe_yaml_load(yaml, template, path, &block) def safe_json_load(path) return {} unless File.exists?(path) - json = File.read(path) + json = parse_erb(File.read(path)) begin MultiJson.load(json) From 287f90a5f86280b361c6bb8682224ec7c4eaa807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Tue, 16 Apr 2019 20:49:18 +0200 Subject: [PATCH 2/2] [FIX] fix issue with rendering yaml file. We have to parse the yaml and then to process the ERB to avoid indentation issue --- .../steam/adapters/filesystem/yaml_loader.rb | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb index 02302624..cc695c8f 100644 --- a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb +++ b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb @@ -43,13 +43,32 @@ def inject(file_path) ERB.new(data).result(binding) end + def parse_erb_struct(vals) + if vals.is_a? Array + vals.each do | val | + parse_erb_struct(val) + end + else + vals.each do | key, val | + if val.is_a? String + vals[key] = parse_erb(val) + else + parse_erb_struct(val) + end + end + end + end + def safe_yaml_load(yaml, template, path, &block) return {} if yaml.blank? - yaml = parse_erb(yaml) + erb_activated = parse_erb(yaml) != yaml begin HashConverter.to_sym(YAML.load(yaml)).tap do |attributes| + if erb_activated + parse_erb_struct(attributes) + end block.call(attributes, template) if block_given? end rescue Exception => e