本文介绍了Jekyll编码类别特殊字符的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jekyll安装程序过去可以正常工作.自更新以来,我遇到的URL包含标签名称(其中包含一些特殊字符)的问题.

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

当我尝试访问带有特殊字符的URL(例如http://127.0.0.1:4000/tag/Actualit%C3%A9%20europ%C3%A9enne/,其中Actualité européenne是类别的名称)时,我会收到一条错误消息.

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.

错误消息是incompatible character encodings: UTF-8 and ASCII-8BIT. _posts目录中的所有文件均为utf-8.

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

这是堆栈跟踪:

[2017-01-30 17:41:59]错误编码:: CompatibilityError:不兼容 字符编码:UTF-8和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 '服务' /usr/lib/ruby/2.1.0/webrick/httpservlet/filehandler.rb:213:in '服务'/usr/lib/ruby/2.1.0/webrick/httpserver.rb:138:在'服务'中 /usr/lib/ruby/2.1.0/webrick/httpserver.rb:94:在'run'中 /usr/lib/ruby/2.1.0/webrick/server.rb:295:in'在启动线程中阻止'

[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'

我已重命名_posts中的所有文件以删除文件名中的特殊字符,但仍然无法正常工作.我不想重命名标签.

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.

推荐答案

默认情况下,所有页面均编码为'utf-8'.但您可以在config.yml中将其覆盖:

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

encoding: ENCODING

但是jekyll似乎无法正常工作(直到:2017年1月),并且unicode没有英文字符,请参见类似的问题关闭字符串似乎不适用于Unicode/瑞典字母#4623 .如果您不将类别放在' '

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 ' '

使用发电机,其中:

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

小写字母上方的子弹符,用-替换空格,并删除所有非ascii字母,因此您需要在最后一个.gsub(/[^\w-]/, '')之前添加其他替换gsub来替换:

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
...

更新

在阅读GitHub列表中的旧jekyll问题以实现该问题的修复"时,我发现了这个详细解决方案 @ david-jacquel在2014年发布:

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 :

# _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在 Jekyll/jekyll-help/issues/129#

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

这将解决空间问题,并提供一个起点来解决编码名称

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

这篇关于Jekyll编码类别特殊字符的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:10