Class: Jekyll::J1Sitemap

Inherits:
Generator
  • Object
show all
Defined in:
lib/starter_web/_plugins/seo/j1-sitemap.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
# File 'lib/starter_web/_plugins/seo/j1-sitemap.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 segment '_plugins/seo' never appears in
  # __FILE__), which left @project_path pointing at _plugins
  # instead of the site root. `site.source` is authoritative.
  #
  @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', 'sitemap.yml'))
  user_yaml                         = load_yaml(File.join(@module_config_path, 'sitemap.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)

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

  # Resolve the list of file extensions to include in the sitemap.
  # Configurable via `includedFileExtensions` in sitemap.yml; falls
  # back to DEFAULT_INCLUDED_EXTENSIONS when the key is missing,
  # nil, or empty. Each entry is normalized so users may write
  # either ".html" or "html" (defaults).
  #
  @included_file_extensions         = normalize_extensions(@module_config['includedFileExtensions'])
  @included_file_extensions         = DEFAULT_INCLUDED_EXTENSIONS if @included_file_extensions.empty?

  @robots_source_path             ||= File.expand_path @robots_theme_name.to_s,  @template_source_folder
  @sitemap_source_path            ||= File.expand_path @sitemap_theme_name.to_s, @template_source_folder

  if plugin_disabled?
    Jekyll.logger.info "J1 Sitemap:", "disabled"
    return
  else
    Jekyll.logger.info "J1 Sitemap:", "enabled"
    Jekyll.logger.info "J1 Sitemap:", "generate sitemap files"
  end

  @site.pages << sitemap unless file_exists?("sitemap.xml")
  @site.pages << robots  unless file_exists?("robots.txt")
end