Class: Jekyll::Zettel::Tags

Inherits:
Generator
  • Object
show all
Includes:
Jekyll::Zettel
Defined in:
lib/jekyll/zettel/tags.rb

Overview

Generate tags.json from page front matter

Constant Summary collapse

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

Constants included from Jekyll::Zettel

LOG_KEY, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Jekyll::Zettel

#args_empty?, #create_dir_defensively, #create_page, #create_slug, #evaluate_template, #write_catalog

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/jekyll/zettel/tags.rb', line 9

def site
  @site
end

Instance Method Details

#generate(site) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jekyll/zettel/tags.rb', line 11

def generate(site)
  @site = site

  @site.data['aliases'] = {}
  @site.data['tags'] = {}
  @site.data['tag2glosse'] = {}

  register

  write_catalog 'aliases'
  write_catalog 'tags'
  write_catalog 'tag2glosse'
end

#registerObject



25
26
27
28
29
30
31
32
33
# File 'lib/jekyll/zettel/tags.rb', line 25

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

    register_tag(page)
    register_tags(page)
    register_aliases(page)
  end
end

#register_aliases(doc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll/zettel/tags.rb', line 47

def register_aliases(doc)
  @site.data['aliases'][doc.data['tag']] = {
    'slug' => doc.data['slug'],
    'tag' => doc.data['tag'],
    'description' => doc.data['description']
  }
  return unless doc.data.key?('aliases')

  doc.data['aliases'].each do |item|
    @site.data['aliases'][item] = {
      'slug' => doc.data['slug'],
      'tag' => doc.data['tag'],
      'description' => doc.data['description']
    }
  end
end

#register_tag(doc) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll/zettel/tags.rb', line 35

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',
    'title' => doc.data['title'] || 'Missing @title',
    'description' => doc.data['description'] || 'Missing @description',
    'tags' => doc.data['tags']
  }
  doc.data['slug'] = parts[:slug]
end

#register_tags(doc) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/jekyll/zettel/tags.rb', line 64

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

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

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