ruby-on-railsrubyjekylljekyll-extensionsparametrize

Ruby: parametrize string without Ruby on rails framework (Jekyll plugin)


I have plugin that takes attribute from post's front matter and uses it in permalink. Problem is I need to clean up any accents and diacritics from the string before putting it in to the permalink. Ruby on rails has method called parametrize which does exactly what I need but I have no idea how to use it in plugin.

This is plugins code I have:

module JekyllCustomPermalink
  class CustomPermalink < Jekyll::Generator
    safe true
    priority :low

    def generate(site)
      # nothing to do, wait for hook
    end

    Jekyll::Hooks.register :documents, :pre_render do |doc|
      begin
        # check if jekyll can resolve the url template
        doc.url
      rescue NoMethodError => error
        begin
          if !doc.collection.metadata.fetch("custom_permalink_placeholders").is_a?(Array)
            raise CustomPermalinkSetupError, "The custom placeholders need to be an array! Check the settings of your '#{doc.collection.label}' collection."
          end
          def doc.url_template
            @custom_url_template ||= collection.metadata.fetch("custom_permalink_placeholders").inject(collection.url_template){|o,m| o.sub ":" + m, data[m].to_s.parameterize}
          end
        rescue KeyError
          # "custom_permalink_placeholders"
          raise CustomPermalinkSetupError, "No custom placeholders defined for the '#{doc.collection.label}' collection. Define an array of placeholders under the key 'custom_permalink_placeholders'. \nCaused by: " + error.to_s
        end
      end
    end
  end
end

but I get this error:

john@arch-thinkpad ~/P/blog (master)> bundle exec jekyll serve --trace
Configuration file: /home/john/Projects/lyricall/_config.yml
            Source: /home/john/Projects/lyricall
       Destination: /home/john/Projects/lyricall/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
       Jekyll Feed: Generating feed for posts
  Liquid Exception: undefined method `parameterize' for "Žďořšťáčik":String in feed.xml
bundler: failed to load command: jekyll (/home/john/.gem/ruby/3.0.0/bin/jekyll)
/usr/lib/ruby/gems/3.0.0/gems/jekyll_custom_permalink-0.0.1/lib/jekyll_custom_permalink/custom_permalink.rb:20:in `block in url_template': undefined method `parameterize' for "Žďořšťáčik":String (NoMethodError)

What am I doing wrong ? How can I use this method which should be part of a string class but apparently it is not ? How can I achieve same result without ruby on rails framework ?

INFO:

  • jekyll 4.1.1
  • ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]

Thank you for help


Solution

  • Rails additions to base Ruby classes, like String#parameterize, are part of the Active Support Core Extensions. The activesupport gem can be installed and used independent of Rails.

    To keep the default footprint low, ActiveSupport allows you to require only the individual extensions you want to use. In your case, you will need to require the string inflection extensions:

    require 'active_support/core_ext/string/inflections'
    "Kurt Gödel".parameterize 
    => "kurt-godel"