jekyll-polyglot 1.3.0 → 1.4.1

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
- SHA1:
3
- metadata.gz: 1df6aaf664d7ec8f17ca2da2ae38232e4fe95fac
4
- data.tar.gz: 223f820553ee68db46b6ef433d9c8298af737198
2
+ SHA256:
3
+ metadata.gz: 44004d45f57d6cd3d029fb2dd11aa4636a003648e88cbb8c0af96707379143ea
4
+ data.tar.gz: 944467976c70ec60171b5c8e9717d13ac844be691ab315d7937aa827eb9d42de
5
5
  SHA512:
6
- metadata.gz: 617ed0583476e45cfd8ec9c41c745e2e02eaf8a52b760a767c5081c51d0d184254d6546ad585659b18c43dbdfa6a826758566f5ae7767b4472e924091494b933
7
- data.tar.gz: 0ff23c46c41468bddd13a7b91622622aa47bb758f9ca07ed25763e8f4f852b8c7524823ed0dc5ba34d5b4089ce2dfa504f6fc8a70f832e0a352e155db81edd8c
6
+ metadata.gz: 381ec1f760288a9945c0a353b3b1cdc5ba7bcbc14a6f6b261e23d28ae6007b25770814d2b14deb8daec986271211f9b822429135449de3974b6c71041807c344
7
+ data.tar.gz: 8a39f63b3b72670d561299308940a76f24008d73ca84411272776767beee24c2d8a1b5dce02f99d5a490635ec8ddfaa11b0ce390a203073cc283bf13c73e378a
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 - 2017 Samuel Volin
1
+ Copyright (c) 2015 - 2020 Samuel Volin
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
data/README.md CHANGED
@@ -27,15 +27,20 @@ In your `_config.yml` file, add the following preferences
27
27
  ```YAML
28
28
  languages: ["en", "sv", "de", "fr"]
29
29
  default_lang: "en"
30
- exclude_from_localization: ["javascript", "images", "css"]
30
+ exclude_from_localization: ["javascript", "images", "css", "public"]
31
31
  parallel_localization: true
32
32
  ```
33
33
  These configuration preferences indicate
34
34
  - what i18n languages you wish to support
35
35
  - what is your default "fallback" language for your content
36
- - what root level folders are you excluding from localization
36
+ - what root level files/folders are excluded from localization, based
37
+ on if their paths start with any of the excluded regexp substrings. (this is different than the jekyll `exclude: [README.md, CNAME]` ; you should `exclude` files and directories in your repo you dont want in your built site at all, and `exclude_from_localization` files and directories in your built site, but not in your sublanguage sites.)
37
38
  - whether to run language processing in parallel or serial
38
39
 
40
+ The optional `lang_from_path: true` option enables getting page
41
+ language from the first or second path segment, e.g `de/first-one.md`, or
42
+ `_posts/zh_Hans_HK/use-second-segment.md` , if the lang frontmatter isn't defined.
43
+
39
44
  ## How To Use It
40
45
  When adding new posts and pages, add to the YAML front matter:
41
46
  ```
@@ -89,8 +94,7 @@ becomes
89
94
  ```
90
95
  Notice the link `<a href="/fr/menu/">...` directs to the french website.
91
96
 
92
- Even if you are falling back to `default_lang` page, relative links built on the *french* site will
93
- still link to *french* pages.
97
+ Even if you are falling back to `default_lang` page, relative links built on the *french* site will still link to *french* pages.
94
98
 
95
99
  #### Relativized Absolute Urls
96
100
  If you defined a site `url` in your `_config.yaml`, polyglot will automatically relativize absolute links pointing to your website directory:
@@ -107,7 +111,17 @@ becomes
107
111
  ```
108
112
 
109
113
  #### Disabling Url Relativizing
110
- If you don't want a url to be relativized, you can add a space explicitly into the href to prevents a url from being relativized by polyglot.
114
+ _New in 1.4.0_
115
+ If you dont want a href attribute to be relativized (such as for making [a language switcher](https://github.com/untra/polyglot/blob/master/site/_includes/sidebar.html#L40)), you can use the `{% static_href %}href="..."{% endstatic_href %}` block tag.
116
+
117
+ ```html
118
+ <a {% static_href %}href="/about"{% endstatic_href %}>click this static link</a>
119
+ ```
120
+
121
+ that will generate `<a href="/about">click this static link</a>` which is what you would normally use to create a url unmangled by invisible language relativization.
122
+
123
+ _Previous (no longer) Recommendation:_
124
+ If you don't want aa url to be relativized, you can add a space explicitly into the href to prevent a url from being relativized by polyglot.
111
125
 
112
126
  For example, the following urls will be relativized:
113
127
 
@@ -123,7 +137,20 @@ href=" http://mywebsite.com/about"
123
137
  href=" /about"
124
138
  ```
125
139
 
126
- combine with a [html minifier](https://github.com/digitalsparky/jekyll-minifier) for a polished and production ready website.
140
+ combine with a [html minifier](https://github.com/digitalsparky/jekyll-minifier) for a polished and production ready website. -->
141
+
142
+ #### Exclusive site language generation
143
+ _New in 1.4.0_
144
+
145
+ If you want to control which languages a document can be generated for, you can specify `lang-exclusive: [ ]` frontmatter.
146
+ If you include this frontmatter in your post, it will only generate for the specified site languages.
147
+
148
+ For Example, the following frontmatter will only generate in the `en` and `fr` site language builds:
149
+ ```
150
+ ---
151
+ lang-exclusive: ['en', 'fr']
152
+ ---
153
+ ```
127
154
 
128
155
  #### Localized site.data
129
156
 
@@ -144,24 +171,47 @@ This plugin stands out from other I18n Jekyll plugins.
144
171
  - provides the liquid tag `{{ site.default_lang }}` to get the default_lang I18n string.
145
172
  - provides the liquid tag `{{ site.active_lang }}` to get the I18n language string the website was built for. Alternative names for `active_lang` can be configured via `config.lang_vars`.
146
173
  - provides the liquid tag `{{ I18n_Headers https://yourwebsite.com/ }}` to append SEO bonuses to your website.
174
+ - provides the liquid tag `{{ Unrelativized_Link href="/hello" }}` to make urls that do not get influenced by url correction regexs.
147
175
  - provides `site.data` localization for efficient rich text replacement.
148
176
  - a creator that will answer all of your questions and issues.
149
177
 
150
178
  ## SEO Recipes
151
179
  Jekyll-polyglot has a few spectacular [Search Engine Optimization techniques](https://untra.github.io/polyglot/seo) to ensure your Jekyll blog gets the most out of it's multilingual audience. Check them out!
152
180
 
153
- ## Examples
154
- Check out the example project website [here](https://untra.github.io/polyglot)
155
- (Jekyll resources are on the project's [site](https://github.com/untra/polyglot/tree/master/site) directory)
156
-
157
181
  ### Other Websites Built with Polyglot
158
182
  let us know if you make a multilingual blog you want to share:
159
- * [LogRhythm Corporate Website](http://logrhythm.com)
183
+ * [Polyglot project website](https://polyglot.untra.io)
184
+ * [LogRhythm Corporate Website](https://logrhythm.com)
160
185
  * [All Over Earth](https://allover.earth/)
186
+ * [Hanare Cafe in Toshijima, Japan](https://hanarecafe.com)
187
+ * [F-Droid](https://f-droid.org)
188
+ * [Ubuntu MATE](https://ubuntu-mate.org)
161
189
 
162
190
  ## Compatibility
163
- Currently supports Jekyll 3.0 .
164
- Windows users will need to disable parallel_localization on their machines by setting `parallel_localization: false` in the `_config.yml`
191
+ Currently supports Jekyll 3.0 , and Jekyll 4.0
192
+
193
+ * Windows users will need to disable parallel_localization on their machines by setting `parallel_localization: false` in the `_config.yml`
194
+ * In Jekyll 4.0 , SCSS source maps will generate improperly due to how Polyglot operates. The workaround is to disable the CSS sourcemaps. Adding the following to your `config.yml` will disable sourcemap generation:
195
+ ```yaml
196
+ sass:
197
+ sourcemap: never
198
+ ```
199
+
200
+ ## Contributions
201
+ Please! I need all the support I can get! 🙏
202
+
203
+ But for real I would appreciate any contributions and support. This started as an open-source side-project and has gotten bigger than I'd ever imagine!
204
+ If you have something you'd like to contribute to jekyll-polyglot, please open a PR!
205
+
206
+
207
+ ## Roadmap
208
+ * [ ] - **site language**: portuguese `pt_BR` `pt_PT`
209
+ * [ ] - **site language**: arabic `ar`
210
+ * [ ] - **site language**: japanese `ja`
211
+ * [x] - **site language**: russian `ru`
212
+ * [ ] - **site language**: korean `ko`
213
+ * [ ] - **site language**: hebrew `he`
214
+ * [ ] - get whitelisted as an official github-pages jekyll plugin
165
215
 
166
216
  ## Copyright
167
- Copyright (c) Samuel Volin 2017. License: MIT
217
+ Copyright (c) Samuel Volin 2021. License: MIT
@@ -1,3 +1,2 @@
1
- require_relative 'hooks/assets-toggle'
2
1
  require_relative 'hooks/coordinate'
3
2
  require_relative 'hooks/process'
@@ -1,7 +1,8 @@
1
1
  module Jekyll
2
2
  module Polyglot
3
3
  module Liquid
4
- require_relative 'liquid/tags/i18n-headers'
4
+ require_relative 'liquid/tags/i18n_headers'
5
+ require_relative 'liquid/tags/static_href'
5
6
  end
6
7
  end
7
8
  end
@@ -14,11 +14,12 @@ module Jekyll
14
14
  permalink = context.registers[:page]['permalink']
15
15
  site_url = @url.empty? ? site.config['url'] : @url
16
16
  i18n = "<meta http-equiv=\"Content-Language\" content=\"#{site.active_lang}\">\n"
17
- i18n += "<link rel=\"alternate\" i18n=\"#{site.default_lang}\" "\
18
- "href=\" #{site_url}#{permalink}\"/>\n"
17
+ i18n += "<link rel=\"alternate\" hreflang=\"#{site.default_lang}\" "\
18
+ "href=\"#{site_url}#{permalink}\"/>\n"
19
19
  site.languages.each do |lang|
20
20
  next if lang == site.default_lang
21
- i18n += "<link rel=\"alternate\" i18n=\"#{lang}\" "\
21
+
22
+ i18n += "<link rel=\"alternate\" hreflang=\"#{lang}\" "\
22
23
  "href=\"#{site_url}/#{lang}#{permalink}\"/>\n"
23
24
  end
24
25
  i18n
@@ -29,3 +30,4 @@ module Jekyll
29
30
  end
30
31
 
31
32
  Liquid::Template.register_tag('I18n_Headers', Jekyll::Polyglot::Liquid::I18nHeadersTag)
33
+ Liquid::Template.register_tag('i18n_headers', Jekyll::Polyglot::Liquid::I18nHeadersTag)
@@ -0,0 +1,27 @@
1
+ module Jekyll
2
+ module Polyglot
3
+ module Liquid
4
+ class StaticHrefTag < :: Liquid::Block
5
+ def initialize(tag_name, params, tokens)
6
+ super
7
+ end
8
+
9
+ def render(context)
10
+ text = super
11
+ href_attrs = text.strip.split '='
12
+ valid = (href_attrs.length == 2 && href_attrs[0] == 'href') && href_attrs[1].start_with?('"') && href_attrs[1].end_with?('"')
13
+ unless valid
14
+ raise Liquid::SyntaxError, "static_href parameters must include match href=\"...\" attribute param, eg. href=\"http://example.com\, href=\"/about\", href=\"/\" , instead got:\n#{text}"
15
+ end
16
+
17
+ href_value = href_attrs[1]
18
+ # href writes out as ferh="..." explicitly wrong, to be cauaght by seperate processor for nonrelativized links
19
+ "ferh=#{href_value}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Liquid::Template.register_tag('Static_Href', Jekyll::Polyglot::Liquid::StaticHrefTag)
27
+ Liquid::Template.register_tag('static_href', Jekyll::Polyglot::Liquid::StaticHrefTag)
@@ -8,12 +8,19 @@ module Jekyll
8
8
  @file_langs = {}
9
9
  fetch_languages
10
10
  @parallel_localization = config.fetch('parallel_localization', true)
11
- @exclude_from_localization = config.fetch('exclude_from_localization', [])
11
+ @lang_from_path = config.fetch('lang_from_path', false)
12
+ @exclude_from_localization = config.fetch('exclude_from_localization', []).map do |e|
13
+ if File.directory?(e) && e[-1] != '/'
14
+ e + '/'
15
+ else
16
+ e
17
+ end
18
+ end
12
19
  end
13
20
 
14
21
  def fetch_languages
15
22
  @default_lang = config.fetch('default_lang', 'en')
16
- @languages = config.fetch('languages', ['en'])
23
+ @languages = config.fetch('languages', ['en']).uniq
17
24
  @keep_files += (@languages - [@default_lang])
18
25
  @active_lang = @default_lang
19
26
  @lang_vars = config.fetch('lang_vars', [])
@@ -88,17 +95,42 @@ module Jekyll
88
95
  @exclude = old_exclude
89
96
  end
90
97
 
98
+ def derive_lang_from_path(doc)
99
+ if !@lang_from_path
100
+ return nil
101
+ end
102
+ segments = doc.relative_path.split('/')
103
+ if doc.relative_path[0] == '_' \
104
+ && segments.length > 2 \
105
+ && segments[1] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/
106
+ return segments[1]
107
+ elsif segments.length > 1 \
108
+ && segments[0] =~ /^[a-z]{2,3}(:?[_-](:?[A-Za-z]{2}){1,2}){0,2}$/
109
+ return segments[0]
110
+ else
111
+ return nil
112
+ end
113
+ end
114
+
91
115
  # assigns natural permalinks to documents and prioritizes documents with
92
- # active_lang languages over others
116
+ # active_lang languages over others. If lang is not set in front matter,
117
+ # then this tries to derive from the path, if the lang_from_path is set.
118
+ # otherwise it will assign the document to the default_lang
93
119
  def coordinate_documents(docs)
94
120
  regex = document_url_regex
95
121
  approved = {}
96
122
  docs.each do |doc|
97
- lang = doc.data['lang'] || @default_lang
123
+ lang = doc.data['lang'] || derive_lang_from_path(doc) || @default_lang
124
+ lang_exclusive = doc.data['lang-exclusive'] || []
98
125
  url = doc.url.gsub(regex, '/')
99
126
  doc.data['permalink'] = url
127
+ # skip this document if it has already been processed
100
128
  next if @file_langs[url] == @active_lang
129
+ # skip this document if it has a fallback and it isn't assigned to the active language
101
130
  next if @file_langs[url] == @default_lang && lang != @active_lang
131
+ # skip this document if it has lang-exclusive defined and the active_lang is not included
132
+ next if !lang_exclusive.empty? && !lang_exclusive.include?(@active_lang)
133
+
102
134
  approved[url] = doc
103
135
  @file_langs[url] = lang
104
136
  end
@@ -107,14 +139,19 @@ module Jekyll
107
139
 
108
140
  # performs any necesarry operations on the documents before rendering them
109
141
  def process_documents(docs)
110
- return if @active_lang == @default_lang
142
+ # return if @active_lang == @default_lang
143
+
111
144
  url = config.fetch('url', false)
112
- rel_regex = relative_url_regex
113
- abs_regex = absolute_url_regex(url)
145
+ rel_regex = relative_url_regex(false)
146
+ abs_regex = absolute_url_regex(url, false)
147
+ non_rel_regex = relative_url_regex(true)
148
+ non_abs_regex = absolute_url_regex(url, true)
114
149
  docs.each do |doc|
115
- relativize_urls(doc, rel_regex)
150
+ unless @active_lang == @default_lang then relativize_urls(doc, rel_regex) end
151
+ correct_nonrelativized_urls(doc, non_rel_regex)
116
152
  if url
117
- then relativize_absolute_urls(doc, abs_regex, url)
153
+ unless @active_lang == @default_lang then relativize_absolute_urls(doc, abs_regex, url) end
154
+ correct_nonrelativized_absolute_urls(doc, non_abs_regex, url)
118
155
  end
119
156
  end
120
157
  end
@@ -132,30 +169,63 @@ module Jekyll
132
169
  end
133
170
 
134
171
  # a regex that matches relative urls in a html document
135
- # matches href="baseurl/foo/bar-baz" and others like it
136
- # avoids matching excluded files
137
- def relative_url_regex
172
+ # matches href="baseurl/foo/bar-baz" href="/foo/bar-baz" and others like it
173
+ # avoids matching excluded files. prepare makes sure
174
+ # that all @exclude dirs have a trailing slash.
175
+ def relative_url_regex(disabled = false)
138
176
  regex = ''
139
- (@exclude + @languages).each do |x|
140
- regex += "(?!#{x}\/)"
177
+ unless disabled
178
+ @exclude.each do |x|
179
+ regex += "(?!#{x})"
180
+ end
181
+ @languages.each do |x|
182
+ regex += "(?!#{x}\/)"
183
+ end
141
184
  end
142
- %r{href=\"?#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.#]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
185
+ start = disabled ? 'ferh' : 'href'
186
+ %r{#{start}=\"?#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.#]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
143
187
  end
144
188
 
145
- def absolute_url_regex(url)
189
+ # a regex that matches absolute urls in a html document
190
+ # matches href="http://baseurl/foo/bar-baz" and others like it
191
+ # avoids matching excluded files. prepare makes sure
192
+ # that all @exclude dirs have a trailing slash.
193
+ def absolute_url_regex(url, disabled = false)
146
194
  regex = ''
147
- (@exclude + @languages).each do |x|
148
- regex += "(?!#{x}\/)"
195
+ unless disabled
196
+ @exclude.each do |x|
197
+ regex += "(?!#{x})"
198
+ end
199
+ @languages.each do |x|
200
+ regex += "(?!#{x}\/)"
201
+ end
149
202
  end
150
- %r{href=\"?#{url}#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.#]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
203
+ start = disabled ? 'ferh' : 'href'
204
+ %r{(?<!hreflang="#{@default_lang}" )#{start}=\"?#{url}#{@baseurl}\/((?:#{regex}[^,'\"\s\/?\.#]+\.?)*(?:\/[^\]\[\)\(\"\'\s]*)?)\"}
151
205
  end
152
206
 
153
207
  def relativize_urls(doc, regex)
208
+ return if doc.output.nil?
209
+
154
210
  doc.output.gsub!(regex, "href=\"#{@baseurl}/#{@active_lang}/" + '\1"')
155
211
  end
156
212
 
157
213
  def relativize_absolute_urls(doc, regex, url)
214
+ return if doc.output.nil?
215
+
158
216
  doc.output.gsub!(regex, "href=\"#{url}#{@baseurl}/#{@active_lang}/" + '\1"')
159
217
  end
218
+
219
+ def correct_nonrelativized_absolute_urls(doc, regex, url)
220
+ return if doc.output.nil?
221
+
222
+ doc.output.gsub!(regex, "href=\"#{url}#{@baseurl}/" + '\1"')
223
+ end
224
+
225
+ def correct_nonrelativized_urls(doc, regex)
226
+ return if doc.output.nil?
227
+
228
+ doc.output.gsub!(regex, "href=\"#{@baseurl}/" + '\1"')
229
+ end
160
230
  end
161
231
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Polyglot
3
- VERSION = '1.3.0'.freeze
3
+ VERSION = '1.3.3'.freeze
4
4
  end
5
5
  end
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.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Volin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-23 00:00:00.000000000 Z
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -39,12 +39,13 @@ files:
39
39
  - lib/jekyll/polyglot/hooks/coordinate.rb
40
40
  - lib/jekyll/polyglot/hooks/process.rb
41
41
  - lib/jekyll/polyglot/liquid.rb
42
- - lib/jekyll/polyglot/liquid/tags/i18n-headers.rb
42
+ - lib/jekyll/polyglot/liquid/tags/i18n_headers.rb
43
+ - lib/jekyll/polyglot/liquid/tags/static_href.rb
43
44
  - lib/jekyll/polyglot/patches.rb
44
45
  - lib/jekyll/polyglot/patches/jekyll/site.rb
45
46
  - lib/jekyll/polyglot/patches/jekyll/static_file.rb
46
47
  - lib/jekyll/polyglot/version.rb
47
- homepage: http://untra.github.io/polyglot
48
+ homepage: https://polyglot.untra.io/
48
49
  licenses:
49
50
  - MIT
50
51
  metadata: {}
@@ -56,15 +57,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
57
  requirements:
57
58
  - - ">="
58
59
  - !ruby/object:Gem::Version
59
- version: '0'
60
+ version: 2.4.0
60
61
  required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  requirements:
62
63
  - - ">="
63
64
  - !ruby/object:Gem::Version
64
- version: '0'
65
+ version: 2.7.0
65
66
  requirements: []
66
67
  rubyforge_project:
67
- rubygems_version: 2.5.1
68
+ rubygems_version: 2.7.6
68
69
  signing_key:
69
70
  specification_version: 4
70
71
  summary: I18n plugin for Jekyll Blogs