How is the “priority” flag in Jekyll plugins supposed to work?

落花浮王杯 提交于 2020-01-05 03:46:23

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/38126629/how-is-the-priority-flag-in-jekyll-plugins-supposed-to-work

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