“优先级"如何? Jekyll插件中的“国旗"功能正常吗? [英] How is the "priority" flag in Jekyll plugins supposed to work?

查看:73
本文介绍了“优先级"如何? Jekyll插件中的“国旗"功能正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为Jekyll开发一个插件,该插件将有助于将 Cloudinary 用于响应图像.当前来源是我的Github存储库中的 如果可行,将在以后使其成为宝石.

此插件具有两个功能:

  • 液体标签,用于定义像{% cloudinary [preset] path/to/img.jpg [attr="value"] %}
  • 这样的Cloudinary图像
  • 一个转换器,可以自动将图像的标准Markdown/Kramdown语法自动转换为这个Liquid标签:![alt text](path/to/img.jpg){:caption="caption text"}变为{% cloudinary path/to/img.jpg alt="alt text" caption="caption text" %}

当前,这两个功能均按预期工作,但它们不是链接"的.如果我在Markdown中使用Liquid标签,则会对其进行评估.如果我放置标准的Markdown图片,它会转换为{% cloudinary … %} Liquid标签,但不会对此标签进行评估.

我怀疑这两个功能没有以正确的顺序运行,因此我想确保在评估Liquid标签之前执行了Converter.

但是我还不了解如何使用优先级标志.

>

如果我取消注释 priority :normal行在我的插件中,然后运行bundle exec jekyll serve,我收到此错误:

$ bundle exec jekyll serve
Configuration file: /Users/nhoizey/Dropbox/Personnel/Devs/nicolas-hoizey.com/_config.yml
jekyll 3.1.6 | Error:  undefined method `priority' for Jekyll::CloudinaryTag:Class
Did you mean?  print

有什么主意吗?

编辑(7月11日):

我现在将其拆分为插件.

转换器仍在我网站的_plugins文件夹中: https://github.com/nhoizey/nicolas-hoizey.com/blob/52c92a38410e133890eea6044a033cb20344b971/_plugins/cloudinaryfy.rb#L26

Jekyll并没有抱怨这个优先"标志.

Liquid标签现在是一个名为jekyll-cloudinary的真正的自主插件gem: https://nhoizey. github.io/jekyll-cloudinary/

如果我在此插件中放置"priority"标志,我仍然会收到错误消息:

$ bundle exec jekyll serve                                                                                 bundler: failed to load command: jekyll (/usr/local/bin/jekyll)
Bundler::GemRequireError: There was an error while trying to load the gem 'jekyll-cloudinary'.
Gem Load Error is: undefined method `priority' for Jekyll::Cloudinary::CloudinaryTag:Class
Did you mean?  print
Backtrace for gem load error is:
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:5:in `<class:CloudinaryTag>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:4:in `<module:Cloudinary>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:2:in `<module:Jekyll>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:1:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll-cloudinary.rb:1:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll-cloudinary.rb:1:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `block (2 levels) in require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `block in require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:102:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/lib/jekyll/plugin_manager.rb:34:in `require_from_bundler'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/bin/jekyll:9:in `<top (required)>'
/usr/local/bin/jekyll:22:in `load'
/usr/local/bin/jekyll:22:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
/usr/local/bin/bundle:22:in `load'
/usr/local/bin/bundle:22:in `<main>'
Bundler Error Backtrace:

  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:89:in `rescue in block (2 levels) in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:85:in `block (2 levels) in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `each'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `block in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `each'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:102:in `require'
  /usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/lib/jekyll/plugin_manager.rb:34:in `require_from_bundler'
  /usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/bin/jekyll:9:in `<top (required)>'
  /usr/local/bin/jekyll:22:in `load'
  /usr/local/bin/jekyll:22:in `<top (required)>'

为什么它只适用于一个插件,而不能适用于另一个插件?

在解释jekyll-cloudinary Liquid标签之前,如何确保cloudinarify Converter插件运行?

解决方案

Jekyll::Plugin的子类引入了priority标志,以使插件作者能够显式地将订购信息提供给Jekyll.实例化新的Jekyll::Site时,实例化插件子类并按优先级进行排序.如果您希望cloudfoundry插件在另一个插件之前 之后运行,请修改其优先级,以便在处理方面排在另一个插件之前.

这仅对Jekyll::Plugin的子类有效,其中包括Jekyll::ConverterJekyll::Generator.这就是为什么它适用于您的转换器,但不适用于您的Liquid::Tag的原因-液体标签没有优先级的想法,因为它们不竞争.

例如,以具有类别生成器插件 sitemap插件的网站为例. Sitemap插件作者希望确保Sitemap中存在任何生成的页面(所有内容均应在Sitemap中!),因此将其设置为priority :low.类别生成器插件作者可能希望确保它在任何其他生成器(例如站点地图生成器)之前运行,因此它设置了priority :high.这样可以确保类别生成器插件在站点地图插件之前运行,从而使编译后的站点地图中具有类别页面.

默认优先级为:normal.按照优先级从高到低的顺序:

  • :highest
  • :high
  • :normal
  • :low
  • :lowest

I am currently trying to develop a plugin for Jekyll that would help use Cloudinary for responsive images. Current source is in my Github repo, I will make it a gem later when/if it works.

This plugin has two features:

  • a Liquid tag to define Cloudinary images like {% cloudinary [preset] path/to/img.jpg [attr="value"] %}
  • a Converter to optionnaly automate transformation of standard Markdown/Kramdown syntax for images into this Liquid tag: ![alt text](path/to/img.jpg){:caption="caption text"} becomes {% cloudinary path/to/img.jpg alt="alt text" caption="caption text" %}

Currently, both features work as intended, but they're not "chained". If I use the Liquid tag in my Markdown, it is evaluated. If I put a standard Markdown image, it is converted to the {% cloudinary … %} Liquid tag, but this tag is not evaluated.

I suspect the two features are not run in the right order, so I want to make sure the Converter is executed before the Liquid tag is evaluated.

But I didn't yet understand how to use the priority flag.

If I uncomment the priority :normal line in my plugin, and run bundle exec jekyll serve, I get this error:

$ bundle exec jekyll serve
Configuration file: /Users/nhoizey/Dropbox/Personnel/Devs/nicolas-hoizey.com/_config.yml
jekyll 3.1.6 | Error:  undefined method `priority' for Jekyll::CloudinaryTag:Class
Did you mean?  print

Any idea?

EDIT (July 11th):

I have now split it into to plugins.

The Converter is still in my site's _plugins folder: https://github.com/nhoizey/nicolas-hoizey.com/blob/52c92a38410e133890eea6044a033cb20344b971/_plugins/cloudinaryfy.rb#L26

Jekyll doesn't complain with this "priority" flag.

The Liquid tag is now a true autonomous plugin gem named jekyll-cloudinary: https://nhoizey.github.io/jekyll-cloudinary/

If I put the "priority" flag in this plugin, I still get the error:

$ bundle exec jekyll serve                                                                                 bundler: failed to load command: jekyll (/usr/local/bin/jekyll)
Bundler::GemRequireError: There was an error while trying to load the gem 'jekyll-cloudinary'.
Gem Load Error is: undefined method `priority' for Jekyll::Cloudinary::CloudinaryTag:Class
Did you mean?  print
Backtrace for gem load error is:
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:5:in `<class:CloudinaryTag>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:4:in `<module:Cloudinary>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:2:in `<module:Jekyll>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll/cloudinary.rb:1:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll-cloudinary.rb:1:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-cloudinary-1.2.2/lib/jekyll-cloudinary.rb:1:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:86:in `block (2 levels) in require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `block in require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `each'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:102:in `require'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/lib/jekyll/plugin_manager.rb:34:in `require_from_bundler'
/usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/bin/jekyll:9:in `<top (required)>'
/usr/local/bin/jekyll:22:in `load'
/usr/local/bin/jekyll:22:in `<top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
/usr/local/bin/bundle:22:in `load'
/usr/local/bin/bundle:22:in `<main>'
Bundler Error Backtrace:

  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:89:in `rescue in block (2 levels) in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:85:in `block (2 levels) in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `each'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:81:in `block in require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `each'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:70:in `require'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:102:in `require'
  /usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/lib/jekyll/plugin_manager.rb:34:in `require_from_bundler'
  /usr/local/lib/ruby/gems/2.3.0/gems/jekyll-3.1.6/bin/jekyll:9:in `<top (required)>'
  /usr/local/bin/jekyll:22:in `load'
  /usr/local/bin/jekyll:22:in `<top (required)>'

Why is it working for one plugin but not the other?

How can I make sure the cloudinarify Converter plugin runs before the jekyll-cloudinary Liquid tag is interpreted?

解决方案

The priority flag was introduced for subclasses of Jekyll::Plugin in order to give plugin authors the ability to explicitly give ordering information to Jekyll. Plugin subsclasses are instantiated and sorted by priority when a new Jekyll::Site is instantiated. If you want your cloudfoundry plugin to be run before or after another plugin, you modify its priority so it will come before the other plugin in terms of processing.

This is only valid for subclasses of Jekyll::Plugin, which includes Jekyll::Converter and Jekyll::Generator. This is why it works for your converter, but not for your Liquid::Tag – liquid tags do not have the idea of a priority because they do not compete.

For example, take the case of a site which has a category generator plugin and a sitemap plugin. The sitemap plugin author wants to ensure that any generated pages are present in the sitemap (all content should be in the sitemap!), so it sets priority :low. The category generator plugin author may want to ensure it runs before any other generator, such as a sitemap generator, so it sets priority :high. This ensures the category generator plugin is run before the sitemap plugin such that the compiled sitemap has the category pages in it.

The default priority is :normal. In descending order of precedence, there is:

  • :highest
  • :high
  • :normal
  • :low
  • :lowest

这篇关于“优先级"如何? Jekyll插件中的“国旗"功能正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆