Jekyll encoding name of category special characters

允我心安 提交于 2021-02-18 17:09:33

问题


My Jekyll installation used to work. Since an update, I face an issue with URL containing tag names which have some special characters.

I now get an error message when trying to reach a URL with special characters in it like http://127.0.0.1:4000/tag/Actualit%C3%A9%20europ%C3%A9enne/, where Actualité européenne is the name of a category.

The error message is incompatible character encodings: UTF-8 and ASCII-8BIT. All the files in _posts directory are utf-8.

Here is the stack trace :

[2017-01-30 17:39:09] ERROR Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:313:in 'set_filename' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:282:in 'exec_handler' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:217:in 'do_GET' /var/lib/gems/2.1.0/gems/jekyll-3.4.0/lib/jekyll/commands/serve/servlet.rb:30:in 'do_GET' /usr/lib/ruby/2.1.0/webrick/httpservlet/abstract.rb:106:in 'service' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:213:in 'service' /usr/lib/ruby/2.1.0/webrick/httpserver.rb:138:in 'service' /usr/lib/ruby/2.1.0/webrick/httpserver.rb:94:in 'run' /usr/lib/ruby/2.1.0/webrick/server.rb:295:in 'block in start_thread'

[2017-01-30 17:41:59] ERROR Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:313:in 'set_filename' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:282:in 'exec_handler' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:217:in 'do_GET' /var/lib/gems/2.1.0/gems/jekyll-3.4.0/lib/jekyll/commands/serve/servlet.rb:30:in 'do_GET' /usr/lib/ruby/2.1.0/webrick/httpservlet/abstract.rb:106:in 'service' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:213:in 'service' /usr/lib/ruby/2.1.0/webrick/httpserver.rb:138:in 'service' /usr/lib/ruby/2.1.0/webrick/httpserver.rb:94:in 'run' /usr/lib/ruby/2.1.0/webrick/server.rb:295:in 'block in start_thread'

I've renamed all the files in _posts to remove special characters in the filenames, but still does not work. I don't want to rename the tags.


回答1:


all the pages are encoded to 'utf-8' by default. but you can override this in config.yml:

encoding: ENCODING 

but it seems that jekyll doesn't works well (until now: jan-2017) with unicode no english characters, see this similar issue Slugify a string doesn't seem to work on Unicode/Swedish letters #4623. the space also my cause a little problem if you don't put the category inside ' '

a fix whould be to slugify your "Catégories" explicitly before integrating them in the url, using a generator, with:

slug = category.strip.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') # categories slugiffier  
// use this slug as the category id

the slugifier above just down case, replace space with -, and remove all non ascii letter, so you'll need to add other substitutions gsub before the last one .gsub(/[^\w-]/, '') to replace:

é è ê -> e
à â   -> a
...

Update

while reading the old jekyll issues in GitHub list to implement a "fix" for that one, I found this detailed solution posted by @david-jacquel on 2014 :

This needs to change the way Jekyll generates urls for posts. This can be done with a plugin.

# _plugins/post.rb
module Jekyll

  class Post

    # override post method in order to return categories names as slug
    # instead of strings
    #
    # An url for a post with category "category with space" will be in
    # slugified form : /category-with-space
    # instead of url encoded form : /category%20with%20space
    #
    # @see utils.slugify
    def url_placeholders
      {
          :year        => date.strftime("%Y"),
          :month       => date.strftime("%m"),
          :day         => date.strftime("%d"),
          :title       => slug,
          :i_day       => date.strftime("%-d"),
          :i_month     => date.strftime("%-m"),
          :categories  => (categories || []).map { |c| Utils.slugify(c) }.join('/'),
          :short_month => date.strftime("%b"),
          :short_year  => date.strftime("%y"),
          :y_day       => date.strftime("%j"),
          :output_ext  => output_ext
      }
    end

  end

end

-- David Jacquel on Jekyll/jekyll-help/issues/129#

that will resolve the space issue, and give a starter point to solve the encoding name



来源:https://stackoverflow.com/questions/41941320/jekyll-encoding-name-of-category-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!