Sha256: eda1de4d62f334aad2d222d23ae0e30d8e2b45dc46f62fe615dc81ceadb48122

Contents?: true

Size: 1005 Bytes

Versions: 7

Compression:

Stored size: 1005 Bytes

Contents

module Moft
  module Tags
    class PostComparer
      MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/

      attr_accessor :date, :slug

      def initialize(name)
        who, cares, date, slug = *name.match(MATCHER)
        @slug = slug
        @date = Time.parse(date)
      end

      def ==(other)
        slug == other.slug &&
          date.year  == other.date.year &&
          date.month == other.date.month &&
          date.day   == other.date.day
      end
    end

    class PostUrl < Liquid::Tag
      def initialize(tag_name, post, tokens)
        super
        @orig_post = post.strip
        @post = PostComparer.new(@orig_post)
      end

      def render(context)
        site = context.registers[:site]

        site.posts.each do |p|
          if @post == p
            return p.url
          end
        end

        puts "ERROR: post_url: \"#{@orig_post}\" could not be found"

        return "#"
      end
    end
  end
end

Liquid::Template.register_tag('post_url', Moft::Tags::PostUrl)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
moft-1.0.5 lib/moft/tags/post_url.rb
moft-1.0.4 lib/moft/tags/post_url.rb
moft-1.0.3 lib/moft/tags/post_url.rb
moft-1.0.2.1 lib/moft/tags/post_url.rb
moft-1.0.2 lib/moft/tags/post_url.rb
moft-1.0.1 lib/moft/tags/post_url.rb
moft-1.0.0 lib/moft/tags/post_url.rb