Class: Jekyll::L10n::ExtractionConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/extraction/config_loader.rb

Overview

Loads and validates extraction configuration for files during build.

ExtractionConfigLoader finds extraction configuration for generated HTML files by matching them against Jekyll site pages. It validates whether extraction is enabled and configured for a file, loads page-specific settings, and identifies files to skip (localized page variants).

Key responsibilities:

  • Match generated HTML files to Jekyll site pages

  • Validate that extraction is enabled for a file

  • Load page-specific extraction configuration

  • Identify localized page variants to skip

  • Extract CSS selectors for element exclusion

Examples:

loader = ExtractionConfigLoader.new(site, '_site')
if loader.valid_for_extraction?(file_path)
  config = loader.load_page_config(file_path)
end

Instance Method Summary collapse

Constructor Details

#initialize(site, dest) ⇒ ExtractionConfigLoader

Initialize a new ExtractionConfigLoader.

Parameters:

  • site (Jekyll::Site)

    Jekyll site object

  • dest (String)

    Destination build directory



31
32
33
34
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 31

def initialize(site, dest)
  @site = site
  @dest = dest
end

Instance Method Details

#extract_exclude_selectors(config) ⇒ Array<String>

Extract CSS selectors for element exclusion from configuration.

Returns CSS selectors of elements to exclude from extraction (e.g., script, style, code blocks). Defaults to sensible defaults if not configured.

Parameters:

  • config (Hash)

    Page front matter data

Returns:

  • (Array<String>)

    CSS selectors for excluded elements



108
109
110
111
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 108

def extract_exclude_selectors(config)
  config.data.dig('with_locales_data', 'extraction', 'exclude_selectors') ||
    ['script', 'style', 'code.language-plaintext', 'pre code']
end

#find_page_config_for_file(file_path) ⇒ Hash?

Find page configuration for a file.

Matches a generated file path to a Jekyll page with extraction enabled. Returns the page’s front matter data, or nil if no match found.

Parameters:

  • file_path (String)

    Path to generated file

Returns:

  • (Hash, nil)

    Page front matter data if found, nil otherwise



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 70

def find_page_config_for_file(file_path)
  @site.pages.each do |page|
    next unless page.data['with_locales'] == true

    page_output = page.output_ext ? page.destination('') : page.destination('/')
    next unless file_path.end_with?(page_output.sub(%r{/$}, '/index.html'))

    return page.data
  end

  nil
end

#load_page_config(file_path) ⇒ PageLocalesConfig

Load extraction configuration for a file.

Finds the Jekyll page matching this file and returns its localization configuration wrapped in PageLocalesConfig.

Parameters:

  • file_path (String)

    Path to file

Returns:



58
59
60
61
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 58

def load_page_config(file_path)
  page_config = find_page_config_for_file(file_path)
  PageLocalesConfig.new(page_config)
end

#skip_localized_page?(file_path) ⇒ Boolean

Check if a file is a localized page variant that should be skipped.

Localized pages are generated copies in locale subdirectories that shouldn’t be re-extracted (extraction happens on original pages only).

Parameters:

  • file_path (String)

    Path to file to check

Returns:

  • (Boolean)

    True if file is in a locale subdirectory



90
91
92
93
94
95
96
97
98
99
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 90

def skip_localized_page?(file_path)
  relative_path = file_path.sub(@dest, '')

  all_locales = @site.pages.map do |p|
    p.data.dig('with_locales_data', 'locales') || []
  end
  all_locales.flatten!
  all_locales.uniq!
  all_locales.any? { |locale| relative_path.start_with?("/#{locale}/") }
end

#valid_for_extraction?(file_path) ⇒ Boolean

Check if a file is valid for extraction.

Matches the file to a Jekyll page and verifies extraction is enabled for that page in the configuration.

Parameters:

  • file_path (String)

    Path to file to check

Returns:

  • (Boolean)

    True if file matches a page with extraction enabled



43
44
45
46
47
48
49
# File 'lib/jekyll-l10n/extraction/config_loader.rb', line 43

def valid_for_extraction?(file_path)
  page_config = find_page_config_for_file(file_path)
  return false unless page_config

  config = PageLocalesConfig.new(page_config)
  config.enabled? && config.extract_on_build?
end