【问题标题】:Calling one jekyll plugin from another从另一个调用一个 jekyll 插件
【发布时间】:2018-09-20 19:32:29
【问题描述】:

我正在编写一个 jekyll 插件来创建自定义标签。它接受一个参数并输出一串 HTML。我已经让它大部分工作了——我可以传递参数并根据这些参数取回 HTML。伟大的。

这就是我难过的地方:我想将另一个插件的渲染作为我自己的一部分。

我的理想插件是jekyll_icon_list,我想使用的插件是jekyll-inline-svg。这是(缩写的)代码:

require 'jekyll_icon_list/version'
require 'jekyll'
require 'jekyll-inline-svg'

module JekyllIconList
  class IconList < Liquid::Tag
    def initialize(tag_name, raw_args, tokens)
      @raw_args = raw_args
      @tokens = tokens
      super
    end

    def parse_arguments(raw_args, settings)
        # (Unrelated stuff)
    end

    def generate_image(icon, settings, context)
      # (Unrelated stuff)

      # Problem Here: 
      Liquid::Tag.parse(
        'svg',
         icon,
        @tokens, 
        Liquid::ParseContext.new
      ).render(context)
    end

    def render(context)
      # Builds my HTML, using generate_image in the process        
    end
  end

end

Liquid::Template.register_tag('iconlist', JekyllIconList::IconList)

这不会引发任何错误,但也不会返回任何内容。

我尝试过的其他事情:

Jekyll::Tags::JekylInlineSvg.new( 返回私有方法错误。 Jekyll 不希望我直接制作自己的标签。

'{% svg #{icon} %}' 用替换的图标完全返回; jekyll 显然不会两次解析同一个文件。

我正试图从 Jekyll 的源代码中找出答案,但我在阅读源代码方面并没有那么熟练并一直走入死胡同。谁能指出我正确的方向?非常感激。

【问题讨论】:

    标签: ruby jekyll liquid


    【解决方案1】:

    回答我自己的问题:

    def build_svg(icon_filename)
      tag = "{% svg #{icon_filename} %}"
      liquid_parse(tag)
    end
    
    def liquid_parse(input)
      Liquid::Template.parse(input).render(@context)
    end
    

    基本上创建一个包含您要调用的标签的小模板,并将其交给 Liquid 进行解析。

    下面是我在找到正确方法之前使用的肮脏方法:

    Jekyll::Tags::JekyllInlineSvg.send(:new, 'svg', icon_filename, @tokens).render(context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多