raindeer 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cli/static.rb +25 -3
- data/lib/matrix/effects/color_effect.rb +1 -1
- data/lib/matrix/matrix.rb +1 -1
- data/lib/raindeer/boot.rb +3 -0
- data/lib/router/router.rb +27 -3
- data/lib/translations/translations.rb +60 -0
- data/lib/version.rb +1 -1
- metadata +14 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2c8a226e4c8d8c753078a9a36de33e78f0934b0b5535480b4ca5253a52804842
|
|
4
|
+
data.tar.gz: 29807e27625d5090b2a9d35fc291364f925d4944cd3545b219ae088bb16b80b6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 43186b8da6090f9bdd665fc7f0e6c72cf22cb2d38f42125b71805418f8c98bc5a9375094e7e6d95e21b80741def3a9f5a10bd51c453093da17328bc36cce64fa
|
|
7
|
+
data.tar.gz: 17b096a285ebb9ea9099a33328f12d649a552ff1233ea1195b58e83dcd61b142003f73dd44e8cfa881905ec9fa5cba80375d9c9e6e5f412755158ad15b655d23
|
data/lib/cli/static.rb
CHANGED
|
@@ -6,8 +6,10 @@ require 'ruby-progressbar'
|
|
|
6
6
|
|
|
7
7
|
require 'antlers' # Adds antlers support to dirload.
|
|
8
8
|
require 'dirload'
|
|
9
|
+
require 'lowdata' # Data classes can be loaded without a database.
|
|
9
10
|
|
|
10
11
|
require_relative '../pages/pages'
|
|
12
|
+
require_relative '../support/config_loader'
|
|
11
13
|
|
|
12
14
|
module Rain
|
|
13
15
|
module CLI
|
|
@@ -22,6 +24,7 @@ module Rain
|
|
|
22
24
|
|
|
23
25
|
public_path = File.expand_path('public', application_path)
|
|
24
26
|
build_assets(application_path:, public_path:)
|
|
27
|
+
load_translations(application_path:)
|
|
25
28
|
|
|
26
29
|
FileUtils.rm_rf(build_path)
|
|
27
30
|
FileUtils.mkdir_p(build_path)
|
|
@@ -35,6 +38,7 @@ module Rain
|
|
|
35
38
|
folder_path = File.join(build_path, request_result.path)
|
|
36
39
|
|
|
37
40
|
puts "#{folder_path} => #{request_result.status}"
|
|
41
|
+
next unless request_result.html
|
|
38
42
|
|
|
39
43
|
FileUtils.mkdir_p(folder_path)
|
|
40
44
|
File.write(File.expand_path('index.html', folder_path), request_result.html)
|
|
@@ -48,7 +52,6 @@ module Rain
|
|
|
48
52
|
config_path = File.expand_path('config/config.yaml', application_path)
|
|
49
53
|
return unless File.exist?(config_path)
|
|
50
54
|
|
|
51
|
-
require_relative '../support/config_loader'
|
|
52
55
|
bundles = Rain::ConfigLoader.load(config_path).assets
|
|
53
56
|
return unless bundles
|
|
54
57
|
|
|
@@ -56,6 +59,15 @@ module Rain
|
|
|
56
59
|
Rain::Assets.new(web_root: public_path, bundles:).build
|
|
57
60
|
end
|
|
58
61
|
|
|
62
|
+
# Static builds have no Accept-Language header, so pages render in the "source_locale" config option ("en" by default).
|
|
63
|
+
def load_translations(application_path:)
|
|
64
|
+
config_path = File.expand_path('config/config.yaml', application_path)
|
|
65
|
+
source_locale = File.exist?(config_path) ? Rain::ConfigLoader.load(config_path).source_locale : nil
|
|
66
|
+
|
|
67
|
+
require_relative '../translations/translations'
|
|
68
|
+
Rain::Translations.load(File.expand_path('config/translations.yaml', application_path), source_locale:)
|
|
69
|
+
end
|
|
70
|
+
|
|
59
71
|
def load_user_application(app_path:)
|
|
60
72
|
metadata = dirload(app_path)
|
|
61
73
|
|
|
@@ -80,12 +92,22 @@ module Rain
|
|
|
80
92
|
paths.map do |path|
|
|
81
93
|
request = Protocol::HTTP::Request.new('http', nil, 'GET', path)
|
|
82
94
|
response = Low::Events::RequestEvent.take(request:).response
|
|
83
|
-
|
|
95
|
+
RequestResult.new(path:, status: response.status, html: response.read)
|
|
96
|
+
rescue Observers::ObserverError => e
|
|
97
|
+
raise unless database_error?(e)
|
|
98
|
+
|
|
99
|
+
# Static sites don't have a database so paths that need one are skipped.
|
|
100
|
+
RequestResult.new(path:, status: 'Skipped', html: nil)
|
|
101
|
+
ensure
|
|
84
102
|
progress_bar.increment
|
|
85
|
-
result
|
|
86
103
|
end
|
|
87
104
|
end
|
|
88
105
|
|
|
106
|
+
def database_error?(error)
|
|
107
|
+
error = error.cause until error.nil? || error.is_a?(LowData::ConfigError)
|
|
108
|
+
error&.message == LowData::ConfigError::MISSING_DATABASE
|
|
109
|
+
end
|
|
110
|
+
|
|
89
111
|
def request_paths(metadata:)
|
|
90
112
|
file_paths = metadata.file_types.values_at('md', 'rd', 'markdown', 'raindown').flat_map { it }.compact
|
|
91
113
|
paths = file_paths.map { |file_path| Rain::Pages.url_path(file_path:) }.compact
|
|
@@ -7,7 +7,7 @@ module Rain
|
|
|
7
7
|
def render(output:, next_output:, **)
|
|
8
8
|
color = next_output ? @config.cell_color : @config.lead_color
|
|
9
9
|
|
|
10
|
-
return Paint[output, '#006ab0'] if @config.leet_keys.invert.key?(output)
|
|
10
|
+
return Paint[output, '#006ab0'] if @config.leet && @config.leet_keys.invert.key?(output)
|
|
11
11
|
|
|
12
12
|
output ? Paint[output, color] : Paint[' ']
|
|
13
13
|
end
|
data/lib/matrix/matrix.rb
CHANGED
|
@@ -18,7 +18,7 @@ module Rain
|
|
|
18
18
|
def initialize(event_pool:, config: ConfigLoader.load('matrix.yaml'))
|
|
19
19
|
@event_pool = event_pool
|
|
20
20
|
@config = config
|
|
21
|
-
@effects = [LeetEffect.new(config:), ColorEffect.new(config:)]
|
|
21
|
+
@effects = [(LeetEffect.new(config:) if config.leet), ColorEffect.new(config:)].compact
|
|
22
22
|
|
|
23
23
|
@screen_size = nil
|
|
24
24
|
|
data/lib/raindeer/boot.rb
CHANGED
|
@@ -27,6 +27,9 @@ module Rain
|
|
|
27
27
|
config_path = File.expand_path('config/config.yaml', Dir.pwd)
|
|
28
28
|
config = ConfigLoader.load(config_path, env)
|
|
29
29
|
|
|
30
|
+
require_relative '../translations/translations'
|
|
31
|
+
Translations.load(source_locale: config.source_locale)
|
|
32
|
+
|
|
30
33
|
Providers.define('rain.router') do
|
|
31
34
|
require_relative '../router/router'
|
|
32
35
|
Router.new
|
data/lib/router/router.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative 'events/wildcard_event'
|
|
|
7
7
|
require_relative 'http'
|
|
8
8
|
require_relative 'route'
|
|
9
9
|
require_relative 'trie'
|
|
10
|
+
require_relative '../translations/translations'
|
|
10
11
|
|
|
11
12
|
module Rain
|
|
12
13
|
# Defines routes and routes requests to a matching route event.
|
|
@@ -57,21 +58,44 @@ module Rain
|
|
|
57
58
|
end
|
|
58
59
|
|
|
59
60
|
def request(event: RequestEvent)
|
|
61
|
+
accept_language = event.request.headers['accept-language']&.join(',')
|
|
62
|
+
locale = Rain::Translations.negotiate(accept_language:)
|
|
63
|
+
|
|
64
|
+
response_event = Antlers::Translations.with_locale(locale) do
|
|
65
|
+
respond(request: event.request)
|
|
66
|
+
end
|
|
67
|
+
language_headers(response: response_event.response, locale:) if response_event.is_a?(ResponseEvent)
|
|
68
|
+
|
|
69
|
+
response_event
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def language_headers(response:, locale:)
|
|
75
|
+
return unless response
|
|
76
|
+
return if Antlers::Translations.locales.empty?
|
|
77
|
+
|
|
78
|
+
headers = response.headers
|
|
79
|
+
headers.add('vary', 'accept-language')
|
|
80
|
+
headers.add('content-language', locale) unless headers.include?('content-language')
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def respond(request:)
|
|
60
84
|
response_event = nil
|
|
61
85
|
|
|
62
86
|
# The last route event will render a response event which we want to return to the request event.
|
|
63
|
-
@trie.match(request:
|
|
87
|
+
@trie.match(request:).each do |route_event|
|
|
64
88
|
response_event = route_event.take
|
|
65
89
|
end
|
|
66
90
|
return response_event if response_event
|
|
67
91
|
|
|
68
92
|
if @routes['/*']
|
|
69
|
-
route = Route.new(path:
|
|
93
|
+
route = Route.new(path: request.path, verbs: @routes['/*'].verbs)
|
|
70
94
|
wildcard_event = WildcardEvent.take(key: '/*', action: :render, route:)
|
|
71
95
|
return wildcard_event if wildcard_event
|
|
72
96
|
end
|
|
73
97
|
|
|
74
|
-
StatusEvent.take(status: Status[404], request:
|
|
98
|
+
StatusEvent.take(status: Status[404], request:)
|
|
75
99
|
end
|
|
76
100
|
end
|
|
77
101
|
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'antlers/translations'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
|
|
6
|
+
module Rain
|
|
7
|
+
# Loads config/translations.yaml into Antlers and picks a request's locale from its Accept-Language header.
|
|
8
|
+
# The "source_locale" config option is the language templates are written in, used when no other language is requested.
|
|
9
|
+
module Translations
|
|
10
|
+
DEFAULT_SOURCE_LOCALE = 'en'
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def load(file_path = File.expand_path('config/translations.yaml', Dir.pwd), source_locale: nil)
|
|
15
|
+
@source_locale = (source_locale || DEFAULT_SOURCE_LOCALE).to_s
|
|
16
|
+
return unless File.exist?(file_path)
|
|
17
|
+
|
|
18
|
+
Antlers::Translations.load(YAML.safe_load_file(file_path))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def source_locale
|
|
22
|
+
@source_locale || DEFAULT_SOURCE_LOCALE
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The templates' own language can be requested too, so "en,es;q=0.5" renders English templates as written.
|
|
26
|
+
def negotiate(accept_language:, locales: Antlers::Translations.locales)
|
|
27
|
+
locales = [source_locale, *locales.reject { it.casecmp?(source_locale) }]
|
|
28
|
+
|
|
29
|
+
requested_locale(accept_language:, locales:) || source_locale
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The first language in "es-ES,es;q=0.9,en;q=0.8" that has a locale, preferring higher "q" values.
|
|
33
|
+
def requested_locale(accept_language:, locales:)
|
|
34
|
+
return nil if accept_language.nil?
|
|
35
|
+
|
|
36
|
+
languages(accept_language:).each do |language|
|
|
37
|
+
tag = language.downcase
|
|
38
|
+
locale = locales.find { it.downcase == tag } || locales.find { it.downcase == tag.split('-').first }
|
|
39
|
+
return locale if locale
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def languages(accept_language:)
|
|
46
|
+
languages = accept_language.split(',').map { |part| language(part:) }
|
|
47
|
+
languages = languages.reject { |language, q| language.empty? || language == '*' || q <= 0 }
|
|
48
|
+
|
|
49
|
+
languages.sort_by.with_index { |(_, q), index| [-q, index] }.map(&:first)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# "es;q=0.9" => ['es', 0.9]
|
|
53
|
+
def language(part:)
|
|
54
|
+
language, *params = part.split(';').map(&:strip)
|
|
55
|
+
q = params.find { it.start_with?('q=') }&.delete_prefix('q=')
|
|
56
|
+
|
|
57
|
+
[language.to_s, q ? q.to_f : 1.0]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: raindeer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
@@ -71,70 +71,70 @@ dependencies:
|
|
|
71
71
|
requirements:
|
|
72
72
|
- - "~>"
|
|
73
73
|
- !ruby/object:Gem::Version
|
|
74
|
-
version: 0.7
|
|
74
|
+
version: '0.7'
|
|
75
75
|
type: :runtime
|
|
76
76
|
prerelease: false
|
|
77
77
|
version_requirements: !ruby/object:Gem::Requirement
|
|
78
78
|
requirements:
|
|
79
79
|
- - "~>"
|
|
80
80
|
- !ruby/object:Gem::Version
|
|
81
|
-
version: 0.7
|
|
81
|
+
version: '0.7'
|
|
82
82
|
- !ruby/object:Gem::Dependency
|
|
83
83
|
name: lowdata
|
|
84
84
|
requirement: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
|
86
86
|
- - "~>"
|
|
87
87
|
- !ruby/object:Gem::Version
|
|
88
|
-
version: '0.
|
|
88
|
+
version: '0.3'
|
|
89
89
|
type: :runtime
|
|
90
90
|
prerelease: false
|
|
91
91
|
version_requirements: !ruby/object:Gem::Requirement
|
|
92
92
|
requirements:
|
|
93
93
|
- - "~>"
|
|
94
94
|
- !ruby/object:Gem::Version
|
|
95
|
-
version: '0.
|
|
95
|
+
version: '0.3'
|
|
96
96
|
- !ruby/object:Gem::Dependency
|
|
97
97
|
name: lowevent
|
|
98
98
|
requirement: !ruby/object:Gem::Requirement
|
|
99
99
|
requirements:
|
|
100
100
|
- - "~>"
|
|
101
101
|
- !ruby/object:Gem::Version
|
|
102
|
-
version: 0.8
|
|
102
|
+
version: '0.8'
|
|
103
103
|
type: :runtime
|
|
104
104
|
prerelease: false
|
|
105
105
|
version_requirements: !ruby/object:Gem::Requirement
|
|
106
106
|
requirements:
|
|
107
107
|
- - "~>"
|
|
108
108
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: 0.8
|
|
109
|
+
version: '0.8'
|
|
110
110
|
- !ruby/object:Gem::Dependency
|
|
111
111
|
name: lowloop
|
|
112
112
|
requirement: !ruby/object:Gem::Requirement
|
|
113
113
|
requirements:
|
|
114
114
|
- - "~>"
|
|
115
115
|
- !ruby/object:Gem::Version
|
|
116
|
-
version: 0.8
|
|
116
|
+
version: '0.8'
|
|
117
117
|
type: :runtime
|
|
118
118
|
prerelease: false
|
|
119
119
|
version_requirements: !ruby/object:Gem::Requirement
|
|
120
120
|
requirements:
|
|
121
121
|
- - "~>"
|
|
122
122
|
- !ruby/object:Gem::Version
|
|
123
|
-
version: 0.8
|
|
123
|
+
version: '0.8'
|
|
124
124
|
- !ruby/object:Gem::Dependency
|
|
125
125
|
name: lownode
|
|
126
126
|
requirement: !ruby/object:Gem::Requirement
|
|
127
127
|
requirements:
|
|
128
128
|
- - "~>"
|
|
129
129
|
- !ruby/object:Gem::Version
|
|
130
|
-
version: 0.5
|
|
130
|
+
version: '0.5'
|
|
131
131
|
type: :runtime
|
|
132
132
|
prerelease: false
|
|
133
133
|
version_requirements: !ruby/object:Gem::Requirement
|
|
134
134
|
requirements:
|
|
135
135
|
- - "~>"
|
|
136
136
|
- !ruby/object:Gem::Version
|
|
137
|
-
version: 0.5
|
|
137
|
+
version: '0.5'
|
|
138
138
|
- !ruby/object:Gem::Dependency
|
|
139
139
|
name: lowtype
|
|
140
140
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -155,14 +155,14 @@ dependencies:
|
|
|
155
155
|
requirements:
|
|
156
156
|
- - "~>"
|
|
157
157
|
- !ruby/object:Gem::Version
|
|
158
|
-
version: '1.
|
|
158
|
+
version: '1.3'
|
|
159
159
|
type: :runtime
|
|
160
160
|
prerelease: false
|
|
161
161
|
version_requirements: !ruby/object:Gem::Requirement
|
|
162
162
|
requirements:
|
|
163
163
|
- - "~>"
|
|
164
164
|
- !ruby/object:Gem::Version
|
|
165
|
-
version: '1.
|
|
165
|
+
version: '1.3'
|
|
166
166
|
- !ruby/object:Gem::Dependency
|
|
167
167
|
name: expressions
|
|
168
168
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -291,6 +291,7 @@ files:
|
|
|
291
291
|
- lib/system/status_404_node.rbx
|
|
292
292
|
- lib/system/status_node.rbx
|
|
293
293
|
- lib/system/system_routes.rb
|
|
294
|
+
- lib/translations/translations.rb
|
|
294
295
|
- lib/version.rb
|
|
295
296
|
homepage: https://github.com/raindeer-rb/raindeer
|
|
296
297
|
licenses: []
|