avillafiorita / jekyll-datapage_gen

Generate one page per yaml record in Jekyll sites.
369 stars 80 forks source link

gems/jekyll-datapage-generator-1.2.0/lib/jekyll-datapage-generator.rb:96:in `[]': no implicit conversion of String into Integer (TypeError) #78

Open drewbroadley opened 4 years ago

drewbroadley commented 4 years ago

I am not sure how this is working for anyone?

The value bring provided to the "data" variable on line 93 is an array, yet the code is treating it like a hash. This makes the do loop not needed.

I need to remove the loop and rename "data_spec" to "data" and it works.

` data = site.config['page_gen'] if data

    index_files_for_this_data = data["index_files"] ? true : index_files
    template = data['template'] || data['data']
    name = data['name']
    name_expr = data['name_expr']
    dir = data['dir'] || data['data']
    extension = data['extension'] || "html"

    if site.layouts.key? template
      # records is the list of records defined in _data.yml
      # for which we want to generate different pages
      records = nil
      data['data'].split('.').each do |level|
        if records.nil?
          records = site.data[level]
        else
          records = records[level]
        end
      end
      if (records.kind_of?(Hash))
        records = records.values
      end

      # apply filtering conditions:
      # - filter requires the name of a boolean field
      # - filter_condition evals a ruby expression
      records = records.select { |r| r[data['filter']] } if data['filter']
      records = records.select { |record| eval(data['filter_condition']) } if data['filter_condition']

      records.each do |record|
        site.pages << DataPage.new(site, site.source, index_files_for_this_data, dir, record, name, name_expr, template, extension)
      end
    else
      puts "error (datapage_gen). could not find template #{template}" if not site.layouts.key? template
    end
  end

`

I'm using Jekyll 3.8.x and Ruby 2.7.0.

avillafiorita commented 3 years ago

Hi, thanks for the report. Is the issue you are running into a configuration problem, namely the fact that page_gen is not specified in config.yml as an array of hashes?

Even if you have a single data structure for which you want to generate individual pages, you still need to define page_gen an array (of one element). Example:

  page_gen:
  - data: [name of a data set in _data]
    template: [name of template in _layouts: will be used to render pages]
    dir: [directory where filenames will be generated]
    index_files: [true|false]
    name: [field used to generate the filename]
    name_expr: [a Ruby expression to generate the filename (alternative to name)]
    title: [field used to generate the page title]
    title_expr: [a Ruby expression to generate the filename (alternative to title)]
    extension: [extension used to generate the filename]
    filter: [property to filter data records by]
    filter_condition: [a Ruby expression to filter data]

(note the "-" in front of data)

Let me know if it solves the issue or I'll dig more into it.

N7K4 commented 3 years ago

Hey, I had a similiar error (no implicit conversion of String into Integer (TypeError)), but it was on my side!

Here is the part of my correct _config_yml.

page_gen-dirs: true

page_gen:
  - data: 'product_categories'
    template: 'product_category'
    name: 'name'
    dir: 'products'

Here is my WRONG _config_yml. I had the file extension (.json) included near the data. Just omit the file extension and the issue was gone for me.

page_gen-dirs: true

page_gen:
  - data: 'product_categories.json'
    template: 'product_category'
    name: 'name'
    dir: 'products'