STiGMa
STiGMa

Reputation: 946

Jekyll/Liquid: Create custom tag/filter with parameters

I'm trying to write a custom tag for my Jekyll-based site that receives a bibtex string and replaces/removes some content.

The tag receives a bibtex string like this:

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

and the ruby code is the following:

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)

Using the tag from the Jekyll template as follows works fine

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

When I try to use a Jekyll variable (e.g., page.bibtex which has the bibtex string)

{%render_bib page.bibtex %} 

it does not recognise/pass the string.

Any thoughts?

Upvotes: 3

Views: 1965

Answers (1)

STiGMa
STiGMa

Reputation: 946

The solution I found uses filters instead of tags

(First time answering my own question)

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)

Upvotes: 3

Related Questions