avillafiorita / jekyll-datapage_gen

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

Using yaml anchors crashes with sanitize_filename #68

Open tgermer opened 4 years ago

tgermer commented 4 years ago

Hi thank you for that great and very useful plugin.

jekyll serve --trace produce the following error:

_plugins/data_page_generator.rb:14:in `sanitize_filename': undefined method `tr' for ["BW", "-", "B"]:Array (NoMethodError)

My data-file:

- stg: Betriebswirtschaftslehre
  stg_kurz: &stg_kurz BW
  abschluss: Bachelor
  abschluss_kurz: &abschluss_kurz B
  grade: Bachelor of Administration
  grade_kurz: B.A.
  stg_kurz_abschluss_kurz: [*stg_kurz, '-', *abschluss_kurz]
- stg: Maschinenbau
  stg_kurz: &stg_kurz MA
  abschluss: Bachelor
  abschluss_kurz: &abschluss_kurz B
  grade: Bachelor of Enginering
  grade_kurz: B.Eng.
  stg_kurz_abschluss_kurz: [*stg_kurz, '-', *abschluss_kurz]

I configured in _config.yml

page_gen:
  - index_files: 
    data: stg
    template: stg
    name: stg_kurz_abschluss_kurz

Is't it possible to use anchors with jekyll-datapage_gen? Can you please solve the problem or give me an advice?

Thank you very much in advance. Tristan

tgermer commented 4 years ago

I used https://learnxinyminutes.com/docs/yaml/ to learn about anchors.

avillafiorita commented 4 years ago

Hi [sorry I took ages to get back to you]

the issue is with the data type used to generate the name. The plugin expects a string and the argument passed is an array.

One solution could be using join at the end of the array. Something like:

stg_kurz_abschluss_kurz: [*stg_kurz, '-', *abschluss_kurz].join

Another possibility could be that of using string interpolators, e.g.:

 stg_kurz_abschluss_kurz: "#{*stg_kurz}-#{*abschluss_kurz}"

but I am not sure whether you can use these constructs in a YAML file. If you have not yet solved it and you want to give it a try, let me know if it works! thanks!

tgermer commented 4 years ago

Thank you very much for your replay @avillafiorita

Unfortunately none of your suggestions work.

I have found the following websites:

But even this one doesn't get me anywhere. Do you have another tip?

avillafiorita commented 4 years ago

You can try changing the sanitize_filename function in lib/data_page_generator.rb as follows:

def sanitize_filename(name)
  if(name.is_a? Integer)
    return name.to_s
  end

  ### MANAGE ARRAYS IN INPUT ###
  if (name.is_a? Array)
      name = name.join("")
  end

  return name.tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÑñÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž","AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz").downcase.strip.gsub(' ', '-').gsub(/[^\w.-]/, '')
end

the important lines are those checking whether the input is an Array and making it into a string, if it is.

If you do not care about replacing accented chars, you can directly return from the condition, that is:

  if (name.is_a? Array)
      return name.join("")
  end

let me know!