Class: Jekyll::Site
- Inherits:
-
Object
- Object
- Jekyll::Site
- Defined in:
- lib/jekyll/polyglot/patches/jekyll/site.rb
Instance Attribute Summary collapse
-
#active_lang ⇒ Object
Returns the value of attribute active_lang.
-
#default_lang ⇒ Object
readonly
Returns the value of attribute default_lang.
-
#exclude_from_localization ⇒ Object
readonly
Returns the value of attribute exclude_from_localization.
-
#file_langs ⇒ Object
Returns the value of attribute file_langs.
-
#lang_from_path ⇒ Object
readonly
Returns the value of attribute lang_from_path.
-
#lang_vars ⇒ Object
readonly
Returns the value of attribute lang_vars.
-
#languages ⇒ Object
readonly
Returns the value of attribute languages.
Instance Method Summary collapse
-
#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.
- #assignPageLanguagePermalinks(doc, docs) ⇒ Object
- #assignPageRedirects(doc, docs) ⇒ Object
-
#coordinate_documents(docs) ⇒ Object
assigns natural permalinks to documents and prioritizes documents with active_lang languages over others.
- #correct_nonrelativized_absolute_urls(doc, regex, url) ⇒ Object
- #correct_nonrelativized_urls(doc, regex) ⇒ Object
- #derive_lang_from_path(doc) ⇒ Object
-
#document_url_regex ⇒ Object
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.
- #fetch_languages ⇒ Object
-
#glob_to_regex(pattern) ⇒ Object
Convert glob pattern to regex pattern * matches any characters except / ? matches any single character except /.
- #prepare ⇒ Object
- #process ⇒ Object
- #process_active_language ⇒ Object
- #process_default_language ⇒ Object
-
#process_documents(docs) ⇒ Object
performs any necessary operations on the documents before rendering them.
- #process_language(lang) ⇒ Object
- #process_orig ⇒ Object
-
#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.
- #relativize_absolute_urls(doc, regex, url) ⇒ Object
- #relativize_urls(doc, regex) ⇒ Object
- #site_payload ⇒ Object
- #site_payload_orig ⇒ Object
- #split_on_multiple_delimiters(string) ⇒ Object
Instance Attribute Details
#active_lang ⇒ Object
Returns the value of attribute active_lang.
8 9 10 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 8 def active_lang @active_lang end |
#default_lang ⇒ Object (readonly)
Returns the value of attribute default_lang.
7 8 9 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 def default_lang @default_lang end |
#exclude_from_localization ⇒ Object (readonly)
Returns the value of attribute exclude_from_localization.
7 8 9 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 def exclude_from_localization @exclude_from_localization end |
#file_langs ⇒ Object
Returns the value of attribute file_langs.
8 9 10 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 8 def file_langs @file_langs end |
#lang_from_path ⇒ Object (readonly)
Returns the value of attribute lang_from_path.
7 8 9 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 def lang_from_path @lang_from_path end |
#lang_vars ⇒ Object (readonly)
Returns the value of attribute lang_vars.
7 8 9 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 def lang_vars @lang_vars end |
#languages ⇒ Object (readonly)
Returns the value of attribute languages.
7 8 9 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 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.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 265 def absolute_url_regex(url, disabled = false) regex = '' unless disabled @exclude.each do |x| escaped_x = glob_to_regex(x) regex += "(?!#{escaped_x})" end @languages.each do |x| escaped_x = Regexp.escape(x) regex += "(?!#{escaped_x}/)" end end start = disabled ? 'ferh' : 'href' neglookbehind = disabled ? "" : "(?<!hreflang=\"#{@default_lang}\" |rel=\"canonical\" )" %r{#{neglookbehind}#{start}="?#{url}#{@baseurl}/((?:#{regex}[^,'"\s/?.]+\.?)*(?:/[^\]\[)("'\s]*)?)"} end |
#assignPageLanguagePermalinks(doc, docs) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 196 def assignPageLanguagePermalinks(doc, docs) pageId = doc.data['page_id'] if !pageId.nil? && !pageId.empty? unless doc.data['permalink_lang'] then doc.data['permalink_lang'] = {} end permalinkDocs = docs.select do |dd| dd.data['page_id'] == pageId end permalinkDocs.each do |dd| doclang = dd.data['lang'] || derive_lang_from_path(dd) || @default_lang doc.data['permalink_lang'][doclang] = dd.data['permalink'] end end end |
#assignPageRedirects(doc, docs) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 177 def assignPageRedirects(doc, docs) pageId = doc.data['page_id'] if !pageId.nil? && !pageId.empty? redirects = [] docs_with_same_id = docs.select { |dd| dd.data['page_id'] == pageId } # For each document with the same page_id docs_with_same_id.each do |dd| # Add redirect if it's a different permalink if dd.data['permalink'] != doc.data['permalink'] redirects << dd.data['permalink'] end end doc.data['redirect_from'] = redirects end 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 148 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, '/') page_id = doc.data['page_id'] || url doc.data['permalink'] = url if doc.data['permalink'].to_s.empty? && !doc.data['lang'].to_s.empty? # skip entirely if nothing to check next if @file_langs.nil? # skip this document if it has already been processed next if @file_langs[page_id] == @active_lang # skip this document if it has a fallback and it isn't assigned to the active language next if @file_langs[page_id] == @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[page_id] = doc @file_langs[page_id] = lang end approved.each_value do |doc| assignPageRedirects(doc, docs) assignPageLanguagePermalinks(doc, docs) end approved.values end |
#correct_nonrelativized_absolute_urls(doc, regex, url) ⇒ Object
298 299 300 301 302 303 304 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 298 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
306 307 308 309 310 311 312 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 306 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 128 def derive_lang_from_path(doc) unless @lang_from_path return nil end segments = split_on_multiple_delimiters(doc.path) # loop through all segments and check if they match the language regex segments.each do |segment| if @languages.include?(segment) return segment end end nil end |
#document_url_regex ⇒ Object
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
232 233 234 235 236 237 238 239 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 232 def document_url_regex regex = '' (@languages || []).each do |lang| regex += "([/.]#{lang}[/.])|" end regex.chomp! '|' /#{regex}/ end |
#fetch_languages ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 24 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 |
#glob_to_regex(pattern) ⇒ Object
Convert glob pattern to regex pattern
-
matches any characters except /
? matches any single character except /
121 122 123 124 125 126 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 121 def glob_to_regex(pattern) # Escape special regex characters first escaped = Regexp.escape(pattern) # Convert glob patterns to regex patterns escaped.gsub("\\*", '.*').gsub("\\?", '.') end |
#prepare ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 10 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 |
#process ⇒ Object
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 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 33 def process prepare all_langs = ([@default_lang] + @languages).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 |pid_lang, pid| next unless waitpid pid, Process::WNOHANG pids.delete pid_lang raise "Polyglot subprocess #{pid} (#{lang}) failed (#{$CHILD_STATUS.exitstatus})" unless $CHILD_STATUS.success? 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 Jekyll::Hooks.trigger :polyglot, :post_write, self end |
#process_active_language ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 101 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_language ⇒ Object
95 96 97 98 99 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 95 def process_default_language old_include = @include process_orig @include = old_include end |
#process_documents(docs) ⇒ Object
performs any necessary operations on the documents before rendering them
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 211 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
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 82 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_orig ⇒ Object
32 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 32 alias 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.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 245 def relative_url_regex(disabled = false) regex = '' unless disabled @exclude.each do |x| escaped_x = glob_to_regex(x) regex += "(?!#{escaped_x})" end @languages.each do |x| escaped_x = Regexp.escape(x) regex += "(?!#{escaped_x}/)" end end start = disabled ? 'ferh' : 'href' %r{#{start}="?#{@baseurl}/((?:#{regex}[^,'"\s/?.]+\.?)*(?:/[^\]\[)("'\s]*)?)"} end |
#relativize_absolute_urls(doc, regex, url) ⇒ Object
290 291 292 293 294 295 296 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 290 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
282 283 284 285 286 287 288 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 282 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_payload ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 71 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_orig ⇒ Object
70 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 70 alias site_payload_orig site_payload |
#split_on_multiple_delimiters(string) ⇒ Object
112 113 114 115 116 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 112 def split_on_multiple_delimiters(string) delimiters = ['.', '/'] regex = Regexp.union(delimiters) string.split(regex) end |