Sha256: 250e9f6f965afb30f9334f0a882ade783fe3979a5ab3cb687607389a08ff1d5e

Contents?: true

Size: 1.7 KB

Versions: 13

Compression:

Stored size: 1.7 KB

Contents

require 'zine/data_page'

module Zine
  # Posts' tags
  class Tag
    def initialize(tags_by_post, tag_templates, tag_index_templates, options)
      @options = options
      @posts_by_tag = sort_tags tags_by_post
      @templates = { tag: tag_templates, tag_index: tag_index_templates }
      @tag_dir = File.join @options['directories']['build'], 'tags'
      FileUtils.remove_dir @tag_dir, force: true
      FileUtils.mkdir_p @tag_dir
    end

    def write_tags
      @posts_by_tag.each do |tag_name, struct_array|
        sorted_array = sort_tags_by_date struct_array
        data = { name: tag_name, title: 'Tags: ' + tag_name,
                 post_array: sorted_array, build_dir: @tag_dir }
        tag_page = Zine::DataPage.new(data, @templates[:tag], @options)
        tag_page.write
      end
      write_tag_index
    end

    private

    # Take an array of posts each with the tags it has, & make an array of tags,
    # each with the posts it's in
    def sort_tags(tags_by_post)
      posts_by_tag = {}
      tags_by_post.each do |post|
        post['tagsArray'].each do |tag|
          name = tag[:name]
          if posts_by_tag.key?(name)
            posts_by_tag[name] << post
          else
            posts_by_tag[name] = [post]
          end
        end
      end
      posts_by_tag
    end

    def sort_tags_by_date(tag_array)
      tag_array.sort_by do |post|
        post[:pageDate]
      end.reverse!
    end

    def write_tag_index
      tag_index_data = { build_dir: @tag_dir, name: 'index', title: 'All tags',
                         post_array: @posts_by_tag.sort_by { |key, _v| key } }
      tag_index = DataPage.new(tag_index_data, @templates[:tag_index], @options)
      tag_index.write
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
zine-0.16.0 lib/zine/tag.rb
zine-0.15.0 lib/zine/tag.rb
zine-0.14.0 lib/zine/tag.rb
zine-0.13.0 lib/zine/tag.rb
zine-0.11.0 lib/zine/tag.rb
zine-0.10.0 lib/zine/tag.rb
zine-0.9.0 lib/zine/tag.rb
zine-0.8.0 lib/zine/tag.rb
zine-0.7.0 lib/zine/tag.rb
zine-0.6.0 lib/zine/tag.rb
zine-0.5.0 lib/zine/tag.rb
zine-0.4.0 lib/zine/tag.rb
zine-0.3.0 lib/zine/tag.rb