Class: Jekyll::TaxonomyRedirects::Generator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll/taxonomy_redirects.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll/taxonomy_redirects.rb', line 41

def generate(site)
  cfg   = site.config["taxonomy_redirects"] || {}
  roots = Array(cfg["roots"])
  maps  = Array(cfg["maps"])
  emit_canonical = cfg.key?("emit_canonical") ? !!cfg["emit_canonical"] : true

  # Root redirects like /category/ -> /venues/
  roots.each do |r|
    from = trim_slashes(r["from"].to_s)
    to   = ensure_trailing_slash(r["to"].to_s)
    next if from.empty? || to.empty?
    site.static_files << RedirectStaticFile.new(site, from, "index.html", to, emit_canonical: emit_canonical)
  end

  # Per-slug redirects from taxonomies
  maps.each do |m|
    type = m["type"].to_s # "category" or "tag"
    from = trim_slashes(m["from"].to_s)
    to   = trim_slashes(m["to"].to_s)
    next if type.empty? || from.empty? || to.empty?

    slugs = case type
            when "category" then site.categories.keys
            when "tag"      then site.tags.keys
            else []
            end

    slugs.each do |slug|
      from_dir = File.join(from, slug)               # e.g. "category/union-chapel"
      dest_url = "/" + File.join(to, slug, "")       # e.g. "/venues/union-chapel/"
      site.static_files << RedirectStaticFile.new(site, from_dir, "index.html", dest_url, emit_canonical: emit_canonical)
    end
  end
end