Sha256: 978f4b91e8ca55ddbfc685b8b89afa0de1f13cdf7d259a3f0b2199eeb151c99d

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

module Jekyll
  module Zettel
    # Generate tags.json from page front matter
    class Tags < Jekyll::Generator
      include Jekyll::Zettel

      SLUG_FORMAT = %r{glosse/(?<slug>.*)/index.(?<ext>html|md)}i.freeze

      attr_reader :site

      def generate(site)
        @site = site
        @site.data['tags'] = {}
        @site.data['tag2glosse'] = {}

        site.pages.each do |page|
          next unless SLUG_FORMAT.match?(page.path.to_s)

          register_tag(page)
        end

        write_catalog 'tags'
        write_catalog 'tag2glosse'
      end

      def register_tag(doc)
        parts = doc.path.to_s.match(SLUG_FORMAT)
        @site.data['tags'][parts[:slug]] = {
          'slug' => parts[:slug],
          'tag' => doc.data['tag'] || 'Missing @tag',
          'label' => doc.data['title'] || 'Missing @title',
          'description' => doc.data['description'] || 'Missing @description',
          'tags' => doc.data['tags']
        }
        doc.data['slug'] = parts[:slug]
        register_tags(doc)
      end

      def register_tags(doc)
        return unless doc.data.key?('tags')

        doc.data['tags'].each { |tag|
          @site.data['tag2glosse'][tag] = [] unless @site.data['tag2glosse'].key?(tag)

          @site.data['tag2glosse'][tag] << doc.data['slug']
        }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jekyll-zettel-0.6.9 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.8 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.7 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.5 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.4 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.3 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.2 lib/jekyll/zettel/tags.rb
jekyll-zettel-0.6.1 lib/jekyll/zettel/tags.rb