middleman icon indicating copy to clipboard operation
middleman copied to clipboard

broken datafiles ingestion in config.rb

Open expilo opened this issue 1 year ago • 2 comments

Expected behavior and actual behavior

Former functionality which allowed using datafiles in config.rb is broken. I would like generate files dynamically using a list of filenames from a datafile, however this results in an error:

~/.asdf/installs/ruby/3.3.2/lib/ruby/gems/3.3.0/gems/contracts-0.16.1/lib/contracts.rb:135:in `+': can't convert Middleman::CoreExtensions::Collections::LazyCollectorStep to String (Middleman::CoreExtensions::Collections::LazyCollectorStep#to_str gives Middleman::CoreExtensions::Collections::LazyCollectorStep) (TypeError)

expected_line = expected_prefix + expected_value + ","
                                  ^^^^^^^^^^^^^^

Steps to reproduce the problem (from a clean middleman installation)

config.rb:

data.files.each do |file|
  proxy "/#{file[:filename]}.html", "/file.html", locals: { file: file }, ignore: true
end

data/files.yaml

---
- filename: first
- filename: second 

source/file.html.erb

---
layout: layout
---

<h1><%= file.filename %></h1>

Additional information

  • Ruby version: 3.3.2
  • Middleman version: 4.5.1
  • OS version: Debian 12

expilo avatar Jun 09 '24 09:06 expilo

I was able to bypass the problem by avoiding alogether Middleman's data loading mechanism

config.rb

require 'yaml'


class Hash
  def symbolize_keys
    transform_keys(&:to_sym)
  end
end

data_files = YAML.load_file(File.join(__dir__, 'data/files.yaml'))

data_files = data_files.map(&:symbolize_keys)

data_files.each do |file|
  proxy "/#{file[:filename]}.html", "/file.html", locals: { file: file }, ignore: true
end

file.html.erb

---
layout: layout
---
<h1><%= "#{file[:filename]}" %></h1>

expilo avatar Jun 09 '24 12:06 expilo

Hey @expilo 👋🏼

That is weird 🤔, I'm using a quite similar approach in a tool built on top of Middleman, with no problems: https://github.com/Subgin/tonic/blob/455f912da5896d7d823994cebcc7fb0487c90f07/lib/tonic.rb#L30-L32 (this start method is invoked from the config.rb file, from where the context is passed). Basically, I iterate over a collection under the data folder to create specific pages for each of those items.

NOTE Actually running under Middleman v4.4.3

markets avatar Jun 10 '24 09:06 markets

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 26 '24 00:11 stale[bot]