Sha256: e0bf7b56f33997e3ac734a33c471ab72dec2ce8faeff4cc4e38b8a84db2ff5fc

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require 'jekyll-seo-tag/filters'
require 'jekyll-seo-tag/version'

module Jekyll
  class SeoTag < Liquid::Tag
    attr_accessor :context

    MINIFY_REGEX = /([>,]\n|[%}]})\s+?(<|{[{%]|[ ]+\")/

    def initialize(_tag_name, text, _tokens)
      super
      @text = text
    end

    def render(context)
      @context = context
      template.render!(payload, info)
    end

    private

    def options
      {
        'version' => Jekyll::SeoTag::VERSION,
        'title'   => title?
      }
    end

    def payload
      {
        'page'    => context.registers[:page],
        'site'    => context.registers[:site].site_payload['site'],
        'seo_tag' => options
      }
    end

    def title?
      !(@text =~ /title=false/i)
    end

    def info
      {
        registers: context.registers,
        filters: [Jekyll::Filters, JekyllSeoTag::Filters]
      }
    end

    def template
      @template ||= Liquid::Template.parse template_contents
    end

    def template_contents
      @template_contents ||= begin
        File.read(template_path).gsub(MINIFY_REGEX, '\1\2').chomp
      end
    end

    def template_path
      @template_path ||= begin
        File.expand_path './template.html', File.dirname(__FILE__)
      end
    end
  end
end

Liquid::Template.register_tag('seo', Jekyll::SeoTag)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jekyll-seo-tag-1.4.0 lib/jekyll-seo-tag.rb
jekyll-seo-tag-1.3.3 lib/jekyll-seo-tag.rb
jekyll-seo-tag-1.3.2 lib/jekyll-seo-tag.rb