Top Level Namespace

Includes:
Process

Defined Under Namespace

Modules: Jekyll

Instance Method Summary collapse

Instance Method Details

#hook_coordinate(site) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jekyll/polyglot/hooks/coordinate.rb', line 7

def hook_coordinate(site)
  # Copy the language specific data, by recursively merging it with the default data.
  # Favour active_lang first, then default_lang, then any non-language-specific data.
  # See: https://www.ruby-forum.com/topic/142809
  merger = proc { |_key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : v2 }
  if site.data.include?(site.default_lang)
    site.data = site.data.merge(site.data[site.default_lang], &merger)
  end
  if site.data.include?(site.active_lang)
    site.data = site.data.merge(site.data[site.active_lang], &merger)
  end

  site.collections.each_value do |collection|
    collection.docs = site.coordinate_documents(collection.docs)
  end
  site.pages = site.coordinate_documents(site.pages)
end

#hook_process(site) ⇒ Object



6
7
8
9
10
11
# File 'lib/jekyll/polyglot/hooks/process.rb', line 6

def hook_process(site)
  site.collections.each_value do |collection|
    site.process_documents(collection.docs)
  end
  site.process_documents(site.pages)
end

#hook_redirects(site) ⇒ Object



23
24
25
26
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
# File 'lib/jekyll/polyglot/hooks/redirects.rb', line 23

def hook_redirects(site)
  return unless site.config.fetch('localize_redirects', false)

  redirects_path = File.join(site.source, '_redirects')
  return unless File.exist?(redirects_path)

  exclusions = site.config.fetch('exclude_from_redirect_localization', [])
  lines = File.readlines(redirects_path)
  localized_lines = []

  lines.each do |line|
    # Always include the original line
    localized_lines << line

    # Skip comments and empty lines
    stripped = line.strip
    next if stripped.empty? || stripped.start_with?('#')

    # Parse the redirect line: /source /target [status_code]
    parts = stripped.split(/\s+/)
    next if parts.length < 2

    source = parts[0]

    # Skip if source is in exclusion list
    next if exclusions.include?(source)

    # Only process paths that start with /
    next unless source.start_with?('/')

    # Skip if source already has a language prefix
    next if site.languages.any? { |lang| source.start_with?("/#{lang}/") || source == "/#{lang}" }

    # Add localized versions for non-default languages
    site.languages.each do |lang|
      next if lang == site.default_lang

      localized_source = "/#{lang}#{source}"
      destination = parts[1]

      # Localize destination if it's an internal path (starts with /)
      # but not if it's an external URL (contains ://)
      localized_destination = if destination.start_with?('/') && !destination.include?('://')
        "/#{lang}#{destination}"
      else
        destination
      end

      rest = parts.length > 2 ? " #{parts[2..].join(' ')}" : ''
      localized_lines << "#{localized_source} #{localized_destination}#{rest}\n"
    end
  end

  # Write to destination
  dest_path = File.join(site.dest, '_redirects')
  File.write(dest_path, localized_lines.join)
end