Class: Jekyll::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/polyglot/patches/jekyll/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_langObject

Returns the value of attribute active_lang.



7
8
9
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7

def active_lang
  @active_lang
end

#default_langObject (readonly)

Returns the value of attribute default_lang.



6
7
8
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 6

def default_lang
  @default_lang
end

#exclude_from_localizationObject (readonly)

Returns the value of attribute exclude_from_localization.



6
7
8
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 6

def exclude_from_localization
  @exclude_from_localization
end

#file_langsObject

Returns the value of attribute file_langs.



7
8
9
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7

def file_langs
  @file_langs
end

#lang_varsObject (readonly)

Returns the value of attribute lang_vars.



6
7
8
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 6

def lang_vars
  @lang_vars
end

#languagesObject (readonly)

Returns the value of attribute languages.



6
7
8
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 6

def languages
  @languages
end

Instance Method Details

#absolute_url_regex(url, disabled = false) ⇒ Object

a regex that matches absolute urls in a html document matches href=“baseurl/foo/bar-baz” and others like it avoids matching excluded files. prepare makes sure that all @exclude dirs have a trailing slash.



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 200

def absolute_url_regex(url, disabled = false)
  regex = ''
  unless disabled
    @exclude.each do |x|
      regex += "(?!#{x})"
    end
    @languages.each do |x|
      regex += "(?!#{x}\/)"
    end
  end
  start = disabled ? 'ferh' : 'href'
  %r{(?<!hreflang="#{@default_lang}" )#{start}=\"?#{url}#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
end

#coordinate_documents(docs) ⇒ Object

assigns natural permalinks to documents and prioritizes documents with active_lang languages over others. If lang is not set in front matter, then this tries to derive from the path, if the lang_from_path is set. otherwise it will assign the document to the default_lang



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 126

def coordinate_documents(docs)
  regex = document_url_regex
  approved = {}
  docs.each do |doc|
    lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang
    lang_exclusive = doc.data['lang-exclusive'] || []
    url = doc.url.gsub(regex, '/')
    doc.data['permalink'] = url
    # skip this document if it has already been processed
    next if @file_langs[url] == @active_lang
    # skip this document if it has a fallback and it isn't assigned to the active language
    next if @file_langs[url] == @default_lang && lang != @active_lang
    # skip this document if it has lang-exclusive defined and the active_lang is not included
    next if !lang_exclusive.empty? && !lang_exclusive.include?(@active_lang)

    approved[url] = doc
    @file_langs[url] = lang
  end
  approved.values
end

#correct_nonrelativized_absolute_urls(doc, regex, url) ⇒ Object



228
229
230
231
232
233
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 228

def correct_nonrelativized_absolute_urls(doc, regex, url)
  return if doc.output.nil?
  modified_output = doc.output.dup
  modified_output.gsub!(regex, "href=\"#{url}#{@baseurl}/" + '\1"')
  doc.output = modified_output
end

#correct_nonrelativized_urls(doc, regex) ⇒ Object



235
236
237
238
239
240
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 235

def correct_nonrelativized_urls(doc, regex)
  return if doc.output.nil?
  modified_output = doc.output.dup
  modified_output.gsub!(regex, "href=\"#{@baseurl}/" + '\1"')
  doc.output = modified_output
end

#derive_lang_from_path(doc) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 105

def derive_lang_from_path(doc)
  if !@lang_from_path
    return nil
  end
  segments = doc.relative_path.split('/')
  if doc.relative_path[0] == '_' \
    && segments.length > 2 \
    && segments[1] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/
    return segments[1]
  elsif segments.length > 1 \
    && segments[0] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/
    return segments[0]
  else
    return nil
  end
end

#document_url_regexObject

a regex that matches urls or permalinks with i18n prefixes or suffixes matches /en/foo , .en/foo , foo.en/ and other simmilar default urls made by jekyll when parsing documents without explicitly set permalinks



169
170
171
172
173
174
175
176
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 169

def document_url_regex
  regex = ''
  @languages.each do |lang|
    regex += "([\/\.]#{lang}[\/\.])|"
  end
  regex.chomp! '|'
  %r{#{regex}}
end

#fetch_languagesObject



23
24
25
26
27
28
29
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 23

def fetch_languages
  @default_lang = config.fetch('default_lang', 'en')
  @languages = config.fetch('languages', ['en']).uniq
  @keep_files += (@languages - [@default_lang])
  @active_lang = @default_lang
  @lang_vars = config.fetch('lang_vars', [])
end

#prepareObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 9

def prepare
  @file_langs = {}
  fetch_languages
  @parallel_localization = config.fetch('parallel_localization', true)
  @lang_from_path = config.fetch('lang_from_path', false)
  @exclude_from_localization = config.fetch('exclude_from_localization', []).map do |e|
    if File.directory?(e) && e[-1] != '/'
      e + '/'
    else
      e
    end
  end
end

#processObject



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
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 32

def process
  prepare
  all_langs = (@languages + [@default_lang]).uniq
  if @parallel_localization
    nproc = Etc.nprocessors
    pids = {}
    begin
      all_langs.each do |lang|
        pids[lang] = fork do
          process_language lang
        end
        while pids.length >= (lang == all_langs[-1] ? 1 : nproc)
          sleep 0.1
          pids.map do |lang, pid|
            pids.delete lang if waitpid pid, Process::WNOHANG
          end
        end
      end
    rescue Interrupt
      all_langs.each do |lang|
        next unless pids.key? lang
        puts "Killing #{pids[lang]} : #{lang}"
        kill('INT', pids[lang])
      end
    end
  else
    all_langs.each do |lang|
      process_language lang
    end
  end
end

#process_active_languageObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 94

def process_active_language
  old_dest = @dest
  old_exclude = @exclude
  @file_langs = {}
  @dest = @dest + '/' + @active_lang
  @exclude += @exclude_from_localization
  process_orig
  @dest = old_dest
  @exclude = old_exclude
end

#process_default_languageObject



88
89
90
91
92
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 88

def process_default_language
  old_include = @include
  process_orig
  @include = old_include
end

#process_documents(docs) ⇒ Object

performs any necesarry operations on the documents before rendering them



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 148

def process_documents(docs)
  # return if @active_lang == @default_lang

  url = config.fetch('url', false)
  rel_regex = relative_url_regex(false)
  abs_regex = absolute_url_regex(url, false)
  non_rel_regex = relative_url_regex(true)
  non_abs_regex = absolute_url_regex(url, true)
  docs.each do |doc|
    unless @active_lang == @default_lang then relativize_urls(doc, rel_regex) end
    correct_nonrelativized_urls(doc, non_rel_regex)
    if url
      unless @active_lang == @default_lang then relativize_absolute_urls(doc, abs_regex, url) end
      correct_nonrelativized_absolute_urls(doc, non_abs_regex, url)
    end
  end
end

#process_language(lang) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 76

def process_language(lang)
  @active_lang = lang
  config['active_lang'] = @active_lang
  lang_vars.each do |v|
    config[v] = @active_lang
  end
  if @active_lang == @default_lang
  then process_default_language
  else process_active_language
  end
end

#process_origObject



31
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 31

alias_method :process_orig, :process

#relative_url_regex(disabled = false) ⇒ Object

a regex that matches relative urls in a html document matches href=“baseurl/foo/bar-baz” href=“/foo/bar-baz” and others like it avoids matching excluded files. prepare makes sure that all @exclude dirs have a trailing slash.



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 182

def relative_url_regex(disabled = false)
  regex = ''
  unless disabled
    @exclude.each do |x|
      regex += "(?!#{x})"
    end
    @languages.each do |x|
      regex += "(?!#{x}\/)"
    end
  end
  start = disabled ? 'ferh' : 'href'
  %r{#{start}=\"?#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
end

#relativize_absolute_urls(doc, regex, url) ⇒ Object



221
222
223
224
225
226
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 221

def relativize_absolute_urls(doc, regex, url)
  return if doc.output.nil?
  modified_output = doc.output.dup
  modified_output.gsub!(regex, "href=\"#{url}#{@baseurl}/#{@active_lang}/" + '\1"')
  doc.output = modified_output
end

#relativize_urls(doc, regex) ⇒ Object



214
215
216
217
218
219
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 214

def relativize_urls(doc, regex)
  return if doc.output.nil?
  modified_output = doc.output.dup
  modified_output.gsub!(regex, "href=\"#{@baseurl}/#{@active_lang}/" + '\1"')
  doc.output = modified_output
end

#site_payloadObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 65

def site_payload
  payload = site_payload_orig
  payload['site']['default_lang'] = default_lang
  payload['site']['languages'] = languages
  payload['site']['active_lang'] = active_lang
  lang_vars.each do |v|
    payload['site'][v] = active_lang
  end
  payload
end

#site_payload_origObject



64
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 64

alias_method :site_payload_orig, :site_payload