سؤال

I wrote a simple plugin for Jeklly that basically takes my site.owner.email which I defined in _config.yml and obfuscate it, then put it on my About page. I have the following code:

_plugins/obfuscate_email.rb

class ObfuscateEmail < Liquid::Tag
  def initialize(tag_name, text, tokens)
    @text = text
    super
  end

  def render(context)
    output_array = []
    char_array = @text.split('')
    char_array.each do |char|
    output = "[dot]" if char == '.'
    output = "[at]" if char == '@'
    output = "[plus]" if char == '+'
      if output
        output_array << output
      else
      output_array << char
      end
    end
    output_array.join
  end
  Liquid::Template.register_tag "obfuscate_email", self
end

Use it in this case: {% obfuscate_email site.owner.email %} will give result like this: site[dot]owner[dot]email, but I was expecting something like this to return owner[dot]something[at]gmail[dot]com

How would I suppose to call this?

Thanks

هل كانت مفيدة؟

المحلول

You can convert your plugin into liquid filter like this:

module MyFilters
  def obfuscate_email(input)
    # your code here
  end
end

Liquid::Template.register_filter(MyFilters)

That way this syntax should work:

{{site.owner.email | obfuscate_email}}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top