【问题标题】:Jekyll/Liquid: Create custom tag/filter with parametersJekyll/Liquid:使用参数创建自定义标签/过滤器
【发布时间】:2018-04-13 11:15:45
【问题描述】:

我正在尝试为基于 Jekyll 的网站编写一个自定义标签,该标签接收 bibtex 字符串并替换/删除一些内容。

标签接收这样的 bibtex 字符串:

@article{heineman2001component,
title={Component-based software engineering},
author={Heineman, George T and Councill, William T},
journal={Putting the pieces together},
pages={5},
year={2001}
}

ruby 代码如下:

module Jekyll
  class RenderBibTag < Liquid::Tag
   def initialize(tag_name, input, tokens)
      super
      @input = input
   end

   def render(context)
      output = (@input)#.gsub(/\bjournal\b[\w\s= \{\-\.\,\(\)]+\},/,'')
      return output;
   end
  end
end

Liquid::Template.register_tag('render_bib', Jekyll::RenderBibTag)

使用 Jekyll 模板中的标签可以正常工作

{%render_bib bibstring %} #bibstring is the string above

当我尝试使用 Jekyll 变量时(例如,具有 bibtex 字符串的 page.bibtex)

{%render_bib page.bibtex %} 

它不识别/传递字符串。

有什么想法吗?

【问题讨论】:

标签: ruby jekyll liquid


【解决方案1】:

我找到的解决方案使用过滤器而不是标签

(第一次回答我自己的问题)

module Jekyll
  module BibFilter
    REGEXP = /\bjournal\b[\w\s= \{\-\.\,\(\)\-\:\+\'\/\..]+\},?/

    def bibFilter(bib)
      #filter text using regexp
      str = "#{bib}".gsub(REGEXP,'')
      #print filtered text
      "#{str}"
    end
 end
end

Liquid::Template.register_filter(Jekyll::BibFilter)

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 2011-08-23
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2017-04-25
    • 2019-04-04
    • 1970-01-01
    相关资源
    最近更新 更多