jekyll-polyglot 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52fcf4f09a9d96c2255ca7226c0a26d5dc7cf5e337fc03805e3e3d18ff00d7d9
4
- data.tar.gz: 6a136ac3a5459c28c39ec71876e01ebee0582154f448ead285169bede86acb9f
3
+ metadata.gz: ab5c70c038d85dbe9936e2a3bd021d75f860a01c272c2650eb6f77e8b386d141
4
+ data.tar.gz: 8d4e4656cbd532277bf708f71fabc3c2b63ee042110862c3aea64ea570434b78
5
5
  SHA512:
6
- metadata.gz: 9fe64a2a499c51edd8e3a861d8b2805f4283dc89301be66c942d297f44923a3a1203b58ffd5183615fa34a4d01b1a30df01c76bb29626f57258e8b5e7396cbb3
7
- data.tar.gz: f22f0727628cfed097dc18845fbd44de02629909d1a4db22767b1b9d52538a1a1287103bab7e8cc85e4e611f74365a7e789515b9b807d7c85751e8a660ef6a1e
6
+ metadata.gz: 508f349097b516f5d4d7f5e794e1e367ba1a64035191793b184314da8f3b35708047df73bc469a740a6042571b97d91802149f259e81581d741ff88a44bcd389
7
+ data.tar.gz: e88b634466df95b6b11c7cc04c8a3b5e1b157bfa1740094e1f2bfae14f1797c1e6d1d40cf80d787a5c8070874f6bf929aa3b86c0607a10811cc09017cd424908
data/README.md CHANGED
@@ -68,6 +68,37 @@ In short:
68
68
  * Don't overthink it, :wink:
69
69
 
70
70
 
71
+ #### Using different permalinks per language
72
+ _New in 1.7.0_
73
+
74
+ Optionally, for those who may want different URLs on different languages, translations may be identified by specifying a `page_id` in the frontmatter.
75
+
76
+ If available, polyglot will use `page_id` to identify the page, and will default to the `permalink` otherwise.
77
+
78
+ As an example, you may have an about page located in `/about/` while being in `/acerca-de/` in Spanish just by changing the permalink and specifying a `page_id` that will link the files as translations:
79
+ ```md
80
+ ---
81
+ title: About
82
+ permalink: /about
83
+ lang: en
84
+ page_id: about
85
+ ---
86
+ This is us!
87
+ ```
88
+
89
+ ```md
90
+ ---
91
+ title: Acerca de
92
+ permalink: /acerca-de
93
+ lang: es
94
+ page_id: about
95
+ ---
96
+ Estos somos nosotros!
97
+ ```
98
+
99
+ Additionally, if you are also using the `jekyll-redirect-from` plugin, pages coordinated this way will automatically have redirects created between pages.
100
+ So `/es/about` will automatically redirect to `/es/acerca-de` and `/acerca-de` can redirect to `/about`. If you use this approach, be sure to also employ a customized [redirect.html](https://github.com/untra/polyglot/blob/master/site/_layouts/redirect.html).
101
+
71
102
  #### Fallback Language Support
72
103
  Lets say you are building your website. You have an `/about/` page written in *english*, *german* and
73
104
  *swedish*. You are also supporting a *french* website, but you never designed a *french* version of your `/about/` page!
@@ -13,7 +13,7 @@ module Jekyll
13
13
  @lang_from_path = config.fetch('lang_from_path', false)
14
14
  @exclude_from_localization = config.fetch('exclude_from_localization', []).map do |e|
15
15
  if File.directory?(e) && e[-1] != '/'
16
- e + '/'
16
+ "#{e}/"
17
17
  else
18
18
  e
19
19
  end
@@ -28,7 +28,7 @@ module Jekyll
28
28
  @lang_vars = config.fetch('lang_vars', [])
29
29
  end
30
30
 
31
- alias_method :process_orig, :process
31
+ alias process_orig process
32
32
  def process
33
33
  prepare
34
34
  all_langs = (@languages + [@default_lang]).uniq
@@ -50,6 +50,7 @@ module Jekyll
50
50
  rescue Interrupt
51
51
  all_langs.each do |lang|
52
52
  next unless pids.key? lang
53
+
53
54
  puts "Killing #{pids[lang]} : #{lang}"
54
55
  kill('INT', pids[lang])
55
56
  end
@@ -61,7 +62,7 @@ module Jekyll
61
62
  end
62
63
  end
63
64
 
64
- alias_method :site_payload_orig, :site_payload
65
+ alias site_payload_orig site_payload
65
66
  def site_payload
66
67
  payload = site_payload_orig
67
68
  payload['site']['default_lang'] = default_lang
@@ -81,7 +82,8 @@ module Jekyll
81
82
  end
82
83
  if @active_lang == @default_lang
83
84
  then process_default_language
84
- else process_active_language
85
+ else
86
+ process_active_language
85
87
  end
86
88
  end
87
89
 
@@ -95,7 +97,7 @@ module Jekyll
95
97
  old_dest = @dest
96
98
  old_exclude = @exclude
97
99
  @file_langs = {}
98
- @dest = @dest + '/' + @active_lang
100
+ @dest = "#{@dest}/#{@active_lang}"
99
101
  @exclude += @exclude_from_localization
100
102
  process_orig
101
103
  @dest = old_dest
@@ -103,9 +105,10 @@ module Jekyll
103
105
  end
104
106
 
105
107
  def derive_lang_from_path(doc)
106
- if !@lang_from_path
108
+ unless @lang_from_path
107
109
  return nil
108
110
  end
111
+
109
112
  segments = doc.relative_path.split('/')
110
113
  if doc.relative_path[0] == '_' \
111
114
  && segments.length > 2 \
@@ -114,9 +117,9 @@ module Jekyll
114
117
  elsif segments.length > 1 \
115
118
  && segments[0] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/
116
119
  return segments[0]
117
- else
118
- return nil
119
120
  end
121
+
122
+ nil
120
123
  end
121
124
 
122
125
  # assigns natural permalinks to documents and prioritizes documents with
@@ -130,20 +133,39 @@ module Jekyll
130
133
  lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang
131
134
  lang_exclusive = doc.data['lang-exclusive'] || []
132
135
  url = doc.url.gsub(regex, '/')
133
- doc.data['permalink'] = url
136
+ page_id = doc.data['page_id'] || url
137
+ doc.data['permalink'] = url unless defined?(doc.data['permalink'])
138
+
139
+ # skip entirely if nothing to check
140
+ next if @file_langs.nil?
134
141
  # skip this document if it has already been processed
135
- next if @file_langs[url] == @active_lang
142
+ next if @file_langs[page_id] == @active_lang
136
143
  # skip this document if it has a fallback and it isn't assigned to the active language
137
- next if @file_langs[url] == @default_lang && lang != @active_lang
144
+ next if @file_langs[page_id] == @default_lang && lang != @active_lang
138
145
  # skip this document if it has lang-exclusive defined and the active_lang is not included
139
146
  next if !lang_exclusive.empty? && !lang_exclusive.include?(@active_lang)
140
147
 
141
- approved[url] = doc
142
- @file_langs[url] = lang
148
+ approved[page_id] = doc
149
+ @file_langs[page_id] = lang
143
150
  end
151
+ approved.values.each {|doc| assignPageRedirects(doc, docs) }
144
152
  approved.values
145
153
  end
146
154
 
155
+ def assignPageRedirects(doc, docs)
156
+ pageId = doc.data['page_id']
157
+ if !pageId.nil? && !pageId.empty?
158
+ lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang
159
+ langPrefix = lang === @default_lang ? '' : "#{lang}/"
160
+ redirectDocs = docs.select do |dd|
161
+ doclang = dd.data['lang'] || derive_lang_from_path(dd) || @default_lang
162
+ dd.data['page_id'] == pageId && doclang != lang && dd.data['permalink'] != doc.data['permalink']
163
+ end
164
+ redirects = redirectDocs.map { |dd| dd.data['permalink'] }
165
+ doc.data['redirect_from'] = redirects
166
+ end
167
+ end
168
+
147
169
  # performs any necesarry operations on the documents before rendering them
148
170
  def process_documents(docs)
149
171
  # return if @active_lang == @default_lang
@@ -168,11 +190,11 @@ module Jekyll
168
190
  # made by jekyll when parsing documents without explicitly set permalinks
169
191
  def document_url_regex
170
192
  regex = ''
171
- @languages.each do |lang|
193
+ (@languages || []).each do |lang|
172
194
  regex += "([\/\.]#{lang}[\/\.])|"
173
195
  end
174
196
  regex.chomp! '|'
175
- %r{#{regex}}
197
+ /#{regex}/
176
198
  end
177
199
 
178
200
  # a regex that matches relative urls in a html document
@@ -190,7 +212,7 @@ module Jekyll
190
212
  end
191
213
  end
192
214
  start = disabled ? 'ferh' : 'href'
193
- %r{#{start}=\"?#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
215
+ %r{#{start}="?#{@baseurl}/((?:#{regex}[^,'"\s/?.]+\.?)*(?:/[^\]\[)("'\s]*)?)"}
194
216
  end
195
217
 
196
218
  # a regex that matches absolute urls in a html document
@@ -208,11 +230,12 @@ module Jekyll
208
230
  end
209
231
  end
210
232
  start = disabled ? 'ferh' : 'href'
211
- %r{(?<!hreflang="#{@default_lang}" )#{start}=\"?#{url}#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
233
+ %r{(?<!hreflang="#{@default_lang}" )#{start}="?#{url}#{@baseurl}/((?:#{regex}[^,'"\s/?.]+\.?)*(?:/[^\]\[)("'\s]*)?)"}
212
234
  end
213
235
 
214
236
  def relativize_urls(doc, regex)
215
237
  return if doc.output.nil?
238
+
216
239
  modified_output = doc.output.dup
217
240
  modified_output.gsub!(regex, "href=\"#{@baseurl}/#{@active_lang}/" + '\1"')
218
241
  doc.output = modified_output
@@ -220,6 +243,7 @@ module Jekyll
220
243
 
221
244
  def relativize_absolute_urls(doc, regex, url)
222
245
  return if doc.output.nil?
246
+
223
247
  modified_output = doc.output.dup
224
248
  modified_output.gsub!(regex, "href=\"#{url}#{@baseurl}/#{@active_lang}/" + '\1"')
225
249
  doc.output = modified_output
@@ -227,6 +251,7 @@ module Jekyll
227
251
 
228
252
  def correct_nonrelativized_absolute_urls(doc, regex, url)
229
253
  return if doc.output.nil?
254
+
230
255
  modified_output = doc.output.dup
231
256
  modified_output.gsub!(regex, "href=\"#{url}#{@baseurl}/" + '\1"')
232
257
  doc.output = modified_output
@@ -234,6 +259,7 @@ module Jekyll
234
259
 
235
260
  def correct_nonrelativized_urls(doc, regex)
236
261
  return if doc.output.nil?
262
+
237
263
  modified_output = doc.output.dup
238
264
  modified_output.gsub!(regex, "href=\"#{@baseurl}/" + '\1"')
239
265
  doc.output = modified_output
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-polyglot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Volin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-08 00:00:00.000000000 Z
11
+ date: 2023-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll