Class: Jekyll::L10n::SiteConfigAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/utils/site_config_accessor.rb

Overview

Accesses Jekyll site configuration properties.

SiteConfigAccessor provides a unified interface for accessing Jekyll site properties, handling both normal Jekyll site objects and test doubles (hashes). This enables easier testing and more flexible configuration handling.

Key responsibilities:

  • Extract localization data from site configuration
  • Compute the union of all configured locales across site and page front matter
  • Access site source directory
  • Access site destination directory
  • Handle both Jekyll site objects and hash-based doubles

Examples:

locales_data = SiteConfigAccessor.extract_locales_data(site)
all_locales  = SiteConfigAccessor.all_configured_locales(site)
source = SiteConfigAccessor.source(site)
dest = SiteConfigAccessor.dest(site)

Class Method Summary collapse

Class Method Details

.all_configured_locales(site) ⇒ Array<String>

Compute the union of all locales configured anywhere on the site.

Combines locales from two sources:

  • The site-wide with_locales_data.locales default (from _config.yml)
  • Any page-level with_locales_data.locales front-matter override, since a page-level override replaces the site default for that page rather than inheriting it, so page scanning alone misses site-wide locales when few or no ordinary pages are present.

This lets callers discover locales that exist only as a page-level override, not declared in site config, while also retaining all site-wide locales.

Parameters:

  • site (Jekyll::Site, Hash)

    Jekyll site object or hash double

Returns:

  • (Array<String>)

    Unique locale codes configured anywhere on the site



52
53
54
55
56
57
# File 'lib/jekyll-l10n/utils/site_config_accessor.rb', line 52

def self.all_configured_locales(site)
  site_wide_locales = extract_locales_data(site)['locales'] || []
  pages = site.is_a?(Hash) ? site['pages'] : site.pages
  page_locales = pages.flat_map { |p| p.data.dig('with_locales_data', 'locales') || [] }
  (site_wide_locales + page_locales).uniq
end

.dest(site) ⇒ String

Get the site destination directory.

Parameters:

  • site (Jekyll::Site, Hash)

    Jekyll site object or hash double

Returns:

  • (String)

    Path to site destination directory



71
72
73
# File 'lib/jekyll-l10n/utils/site_config_accessor.rb', line 71

def self.dest(site)
  site.is_a?(Hash) ? site['dest'] : site.dest
end

.extract_locales_data(site) ⇒ Hash

Extract localization configuration from site.

Accesses the with_locales_data configuration which contains the locales, extraction settings, and other localization options.

Parameters:

  • site (Jekyll::Site, Hash)

    Jekyll site object or hash double

Returns:

  • (Hash)

    Localization configuration hash or empty hash if not found



31
32
33
34
35
36
# File 'lib/jekyll-l10n/utils/site_config_accessor.rb', line 31

def self.extract_locales_data(site)
  config = site.is_a?(Hash) ? site['config'] : site.config
  defaults = config['defaults'] || []
  entry = defaults.find { |d| d.dig('values', 'with_locales_data') }
  entry&.dig('values', 'with_locales_data') || {}
end

.source(site) ⇒ String

Get the site source directory.

Parameters:

  • site (Jekyll::Site, Hash)

    Jekyll site object or hash double

Returns:

  • (String)

    Path to site source directory



63
64
65
# File 'lib/jekyll-l10n/utils/site_config_accessor.rb', line 63

def self.source(site)
  site.is_a?(Hash) ? site['source'] : site.source
end