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_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) ⇒ Object
-
#coordinate_documents(docs) ⇒ Object
assigns natural permalinks to documents and prioritizes documents with active_lang languages over others.
- #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
- #prepare ⇒ Object
- #process ⇒ Object
- #process_active_language ⇒ Object
- #process_default_language ⇒ Object
-
#process_documents(docs) ⇒ Object
performs any necesarry operations on the documents before rendering them.
- #process_language(lang) ⇒ Object
- #process_orig ⇒ Object
-
#relative_url_regex ⇒ Object
a regex that matches relative urls in a html document matches href=“baseurl/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
Instance Attribute Details
#active_lang ⇒ Object
Returns the value of attribute active_lang.
5 6 7 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 5 def active_lang @active_lang end |
#default_lang ⇒ Object (readonly)
Returns the value of attribute default_lang.
4 5 6 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 4 def default_lang @default_lang end |
#exclude_from_localization ⇒ Object (readonly)
Returns the value of attribute exclude_from_localization.
4 5 6 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 4 def exclude_from_localization @exclude_from_localization end |
#file_langs ⇒ Object
Returns the value of attribute file_langs.
5 6 7 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 5 def file_langs @file_langs end |
#lang_vars ⇒ Object (readonly)
Returns the value of attribute lang_vars.
4 5 6 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 4 def lang_vars @lang_vars end |
#languages ⇒ Object (readonly)
Returns the value of attribute languages.
4 5 6 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 4 def languages @languages end |
Instance Method Details
#absolute_url_regex(url) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 176 def absolute_url_regex(url) regex = '' (@exclude).each do |x| regex += "(?!#{x})" end (@languages).each do |x| regex += "(?!#{x}\/)" end %r{href=\"?#{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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 120 def coordinate_documents(docs) regex = document_url_regex approved = {} docs.each do |doc| lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang url = doc.url.gsub(regex, '/') doc.data['permalink'] = url next if @file_langs[url] == @active_lang next if @file_langs[url] == @default_lang && lang != @active_lang approved[url] = doc @file_langs[url] = lang end approved.values end |
#derive_lang_from_path(doc) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 99 def derive_lang_from_path(doc) if !@lang_from_path return nil end segments = doc.relative_path.split('/') if doc.relative_path[0] == '_' \ and segments.length > 2 \ and segments[1] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/ return segments[1] elsif segments.length > 1 \ and segments[0] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/ return segments[0] else return nil end 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
152 153 154 155 156 157 158 159 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 152 def document_url_regex regex = '' @languages.each do |lang| regex += "([\/\.]#{lang}[\/\.])|" end regex.chomp! '|' %r{#{regex}} end |
#fetch_languages ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 22 def fetch_languages @default_lang = config.fetch('default_lang', 'en') @languages = config.fetch('languages', ['en']) @keep_files += (@languages - [@default_lang]) @active_lang = @default_lang @lang_vars = config.fetch('lang_vars', []) end |
#prepare ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 7 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) and e[-1] != '/' e + '/' else e end end end |
#process ⇒ Object
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 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 31 def process prepare all_langs = (@languages + [@default_lang]).uniq if @parallel_localization pids = {} all_langs.each do |lang| pids[lang] = fork do process_language lang end end Signal.trap('INT') do all_langs.each do |lang| puts "Killing #{pids[lang]} : #{lang}" kill('INT', pids[lang]) end end all_langs.each do |lang| waitpid pids[lang] detach pids[lang] end else all_langs.each do |lang| process_language lang end end end |
#process_active_language ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 88 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
82 83 84 85 86 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 82 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
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 136 def process_documents(docs) return if @active_lang == @default_lang url = config.fetch('url', false) rel_regex = relative_url_regex abs_regex = absolute_url_regex(url) docs.each do |doc| relativize_urls(doc, rel_regex) if url then relativize_absolute_urls(doc, abs_regex, url) end end end |
#process_language(lang) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 70 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
30 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 30 alias_method :process_orig, :process |
#relative_url_regex ⇒ Object
a regex that matches relative 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.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 165 def relative_url_regex regex = '' (@exclude).each do |x| regex += "(?!#{x})" end (@languages).each do |x| regex += "(?!#{x}\/)" end %r{href=\"?#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.#]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"} end |
#relativize_absolute_urls(doc, regex, url) ⇒ Object
193 194 195 196 197 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 193 def relativize_absolute_urls(doc, regex, url) return if doc.output.nil? doc.output.gsub!(regex, "href=\"#{url}#{@baseurl}/#{@active_lang}/" + '\1"') end |
#relativize_urls(doc, regex) ⇒ Object
187 188 189 190 191 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 187 def relativize_urls(doc, regex) return if doc.output.nil? doc.output.gsub!(regex, "href=\"#{@baseurl}/#{@active_lang}/" + '\1"') end |
#site_payload ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 59 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
58 |
# File 'lib/jekyll/polyglot/patches/jekyll/site.rb', line 58 alias_method :site_payload_orig, :site_payload |