Class: J1Feed::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/starter_web/_plugins/seo/j1-feed.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object

Main plugin action, called by Jekyll Core



27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/starter_web/_plugins/seo/j1-feed.rb', line 27

def generate(site)
  @site                             = site

  @mode                             = site.config['environment']
  @template                         = site.config['theme']

  # Resolve config paths relative to the Jekyll source directory.
  # The original `File.dirname(__FILE__).sub('_plugins/seo', '')`
  # was a no-op (the path segment '_plugins/seo' never appears in
  # __FILE__), which left @project_path pointing at the _plugins
  # directory instead of the site root. `site.source` is the
  # authoritative location of the Jekyll site source folder.
  #
  @project_path                     = site.source
  @plugin_data_path                 = File.join(@project_path, site.config['data_dir'])
  @module_config_path               = File.join(@plugin_data_path, 'plugins')

  # Safely load default + user settings. Replaces the original
  # `YAML::load(File.open(...))` calls which leaked file handles,
  # used the unsafe loader, and crashed on a missing file.
  #
  default_yaml                      = load_yaml(File.join(@module_config_path, 'defaults', 'feed.yml'))
  user_yaml                         = load_yaml(File.join(@module_config_path, 'feed.yml'))

  @module_config_default_settings   = default_yaml['defaults'] || {}
  @module_config_user_settings      = user_yaml['settings']    || {}

  # Non-destructive merge: user settings override defaults without
  # mutating the loaded defaults hash (the original used `merge!`).
  #
  @module_config                    = @module_config_default_settings.merge(@module_config_user_settings)

  @feed_generation_path             = @module_config['path'].to_s.sub('feed.xml', 'for_categories')
  @feed_generation_enabled          = @module_config['enabled']

  @template_source_folder           = File.join(@project_path, @module_config['template_source_folder'].to_s)
  @template_name                    = @module_config['template_name']

  # Removed `@feed_output = @module_config['feed_output']` - the
  # ivar was assigned but never referenced anywhere in the plugin.

  if disabled_in_development?
    Jekyll.logger.info "J1 Feeds:", "skipped in mode development"
    return
  end

  if plugin_disabled?
    Jekyll.logger.info "J1 Feeds:", "disabled"
    return
  else
    Jekyll.logger.info "J1 Feeds:", "enabled"
  end

  if @module_config['excerpt_only']
    Jekyll.logger.info "J1 Feeds:", "generate rss feeds for: excerpts only"
  end

  if @module_config['posts_limit'].to_i < 100
    Jekyll.logger.info "J1 Feeds:", "generate rss feeds for: #posts of #{@module_config['posts_limit']}"
  else
    Jekyll.logger.info "J1 Feeds:", "generate rss feeds for: #posts of unlimited"
  end

  collections.each do |name, meta|
    Jekyll.logger.info "J1 Feeds:", "generate rss feeds for: all #{name}"
    (meta["categories"] + [nil]).each do |category|
      Jekyll.logger.info "J1 Feeds:", "generate rss feeds for posts by category: #{category}" if category
      path = feed_path(:collection => name, :category => category)

      # Honor the documented "rebuild on every build" toggle. The
      # defaults YAML ships the key as `rebuild_feed` (singular),
      # but the original code read `rebuild_feeds` (plural) - the
      # mismatch meant the option silently never took effect. We
      # accept either spelling for backwards compatibility with any
      # user configs that may have used the plural form. Also
      # removed a redundant re-assignment of `path` inside the
      # `unless` block (it was already computed above).
      #
      unless rebuild_feeds?
        if file_exists?(path)
          Jekyll.logger.info "J1 Feeds:", "feed already exist, skip rebuild"
          next
        end
      end

      @site.pages << make_page(path, :collection => name, :category => category)
    end
  end
  generate_feed_by_tag if config['tags'] && !@site.tags.empty?
end