locomotivecms_steam 1.4.1 → 1.5.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +31 -25
- data/Rakefile +2 -2
- data/config/locales/pt-BR.yml +43 -13
- data/lib/locomotive/steam.rb +9 -4
- data/lib/locomotive/steam/adapters/filesystem.rb +2 -7
- data/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +0 -2
- data/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +11 -0
- data/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +37 -0
- data/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb +18 -0
- data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +4 -3
- data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb +4 -3
- data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb +40 -0
- data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb +0 -1
- data/lib/locomotive/steam/adapters/memory.rb +1 -1
- data/lib/locomotive/steam/entities/content_entry.rb +15 -3
- data/lib/locomotive/steam/entities/content_type.rb +2 -1
- data/lib/locomotive/steam/entities/page.rb +21 -19
- data/lib/locomotive/steam/entities/section.rb +24 -0
- data/lib/locomotive/steam/entities/site.rb +3 -1
- data/lib/locomotive/steam/liquid.rb +2 -0
- data/lib/locomotive/steam/liquid/drops/page.rb +4 -2
- data/lib/locomotive/steam/liquid/drops/params.rb +51 -0
- data/lib/locomotive/steam/liquid/drops/section.rb +56 -0
- data/lib/locomotive/steam/liquid/drops/section_block.rb +47 -0
- data/lib/locomotive/steam/liquid/drops/section_content_proxy.rb +97 -0
- data/lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb +65 -0
- data/lib/locomotive/steam/liquid/drops/site.rb +2 -1
- data/lib/locomotive/steam/liquid/errors.rb +2 -0
- data/lib/locomotive/steam/liquid/filters/base.rb +3 -3
- data/lib/locomotive/steam/liquid/filters/date.rb +1 -1
- data/lib/locomotive/steam/liquid/filters/html.rb +7 -2
- data/lib/locomotive/steam/liquid/filters/json.rb +3 -1
- data/lib/locomotive/steam/liquid/tags/action.rb +2 -2
- data/lib/locomotive/steam/liquid/tags/alt_page_links.rb +63 -0
- data/lib/locomotive/steam/liquid/tags/concerns/i18n_page.rb +1 -1
- data/lib/locomotive/steam/liquid/tags/concerns/section.rb +117 -0
- data/lib/locomotive/steam/liquid/tags/global_section.rb +32 -0
- data/lib/locomotive/steam/liquid/tags/model_form.rb +1 -1
- data/lib/locomotive/steam/liquid/tags/section.rb +87 -0
- data/lib/locomotive/steam/liquid/tags/sections_dropzone.rb +56 -0
- data/lib/locomotive/steam/liquid/tags/snippet.rb +3 -2
- data/lib/locomotive/steam/liquid/template.rb +0 -5
- data/lib/locomotive/steam/middlewares.rb +1 -1
- data/lib/locomotive/steam/middlewares/auth.rb +29 -13
- data/lib/locomotive/steam/middlewares/concerns/helpers.rb +59 -0
- data/lib/locomotive/steam/middlewares/concerns/liquid_context.rb +94 -0
- data/lib/locomotive/steam/middlewares/encoded_link_redirection.rb +45 -0
- data/lib/locomotive/steam/middlewares/entry_submission.rb +1 -1
- data/lib/locomotive/steam/middlewares/favicon.rb +1 -1
- data/lib/locomotive/steam/middlewares/locale.rb +63 -15
- data/lib/locomotive/steam/middlewares/locale_redirection.rb +18 -6
- data/lib/locomotive/steam/middlewares/logging.rb +1 -1
- data/lib/locomotive/steam/middlewares/page.rb +35 -6
- data/lib/locomotive/steam/middlewares/path.rb +1 -1
- data/lib/locomotive/steam/middlewares/private_access.rb +1 -1
- data/lib/locomotive/steam/middlewares/redirection.rb +1 -1
- data/lib/locomotive/steam/middlewares/renderer.rb +2 -82
- data/lib/locomotive/steam/middlewares/robots.rb +1 -1
- data/lib/locomotive/steam/middlewares/section.rb +56 -0
- data/lib/locomotive/steam/middlewares/site.rb +1 -1
- data/lib/locomotive/steam/middlewares/sitemap.rb +58 -45
- data/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
- data/lib/locomotive/steam/middlewares/thread_safe.rb +85 -2
- data/lib/locomotive/steam/middlewares/timezone.rb +1 -1
- data/lib/locomotive/steam/middlewares/url_redirection.rb +1 -1
- data/lib/locomotive/steam/models/entity.rb +0 -2
- data/lib/locomotive/steam/models/mapper.rb +5 -3
- data/lib/locomotive/steam/models/scope.rb +8 -0
- data/lib/locomotive/steam/repositories.rb +4 -0
- data/lib/locomotive/steam/repositories/content_entry_repository.rb +6 -2
- data/lib/locomotive/steam/repositories/page_repository.rb +4 -1
- data/lib/locomotive/steam/repositories/section_repository.rb +14 -0
- data/lib/locomotive/steam/repositories/site_repository.rb +1 -1
- data/lib/locomotive/steam/repositories/snippet_repository.rb +0 -3
- data/lib/locomotive/steam/server.rb +3 -1
- data/lib/locomotive/steam/services.rb +17 -1
- data/lib/locomotive/steam/services/action_service.rb +10 -0
- data/lib/locomotive/steam/services/concerns/decorator.rb +0 -2
- data/lib/locomotive/steam/services/image_resizer_service.rb +1 -1
- data/lib/locomotive/steam/services/page_finder_service.rb +6 -0
- data/lib/locomotive/steam/services/section_finder_service.rb +17 -0
- data/lib/locomotive/steam/services/url_builder_service.rb +31 -17
- data/lib/locomotive/steam/services/url_finder_service.rb +87 -0
- data/lib/locomotive/steam/version.rb +2 -2
- data/locomotivecms_steam.gemspec +1 -1
- data/spec/fixtures/default/app/views/pages/tags/section.liquid.haml +11 -0
- data/spec/fixtures/default/app/views/sections/carousel.liquid +20 -0
- data/spec/fixtures/default/app/views/sections/footer.liquid +48 -0
- data/spec/fixtures/default/app/views/sections/header.liquid +54 -0
- data/spec/fixtures/default/config/deploy.yml +1 -1
- data/spec/fixtures/errors/section_bad_json_content.liquid +9 -0
- data/spec/fixtures/errors/section_bad_json_header.liquid +8 -0
- data/spec/fixtures/mongodb/locomotive_accounts.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_accounts.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_activities.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_activities.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_assets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_assets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_entries.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_entries.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_types.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_types.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_pages.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_pages.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_sections.bson +0 -0
- data/spec/fixtures/mongodb/{sessions.metadata.json → locomotive_sections.metadata.json} +1 -1
- data/spec/fixtures/mongodb/locomotive_sites.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_sites.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_snippets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_snippets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_theme_assets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_translations.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_translations.metadata.json +1 -1
- data/spec/integration/repositories/content_entry_repository_spec.rb +1 -1
- data/spec/integration/repositories/page_repository_spec.rb +1 -1
- data/spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
- data/spec/integration/server/auth_spec.rb +0 -4
- data/spec/integration/server/basic_spec.rb +8 -0
- data/spec/integration/server/contact_form_spec.rb +15 -2
- data/spec/integration/server/sitemap_spec.rb +5 -3
- data/spec/integration/services/content_entry_service_spec.rb +1 -1
- data/spec/support/helpers.rb +3 -3
- data/spec/unit/adapters/filesystem/sanitizers/section_spec.rb +65 -0
- data/spec/unit/adapters/filesystem/sanitizers/site_spec.rb +28 -2
- data/spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +8 -4
- data/spec/unit/adapters/filesystem/yaml_loaders/content_type_spec.rb +2 -2
- data/spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +1 -1
- data/spec/unit/adapters/filesystem/yaml_loaders/section_spec.rb +27 -0
- data/spec/unit/entities/section_spec.rb +29 -0
- data/spec/unit/liquid/drops/params_spec.rb +38 -0
- data/spec/unit/liquid/drops/section_content_proxy_spec.rb +88 -0
- data/spec/unit/liquid/drops/section_spec.rb +26 -0
- data/spec/unit/liquid/filters/html_spec.rb +198 -170
- data/spec/unit/liquid/filters/json_spec.rb +7 -0
- data/spec/unit/liquid/tags/action_spec.rb +12 -2
- data/spec/unit/liquid/tags/alt_page_links_spec.rb +58 -0
- data/spec/unit/liquid/tags/global_section_spec.rb +130 -0
- data/spec/unit/liquid/tags/link_to_spec.rb +1 -1
- data/spec/unit/liquid/tags/locale_switcher_spec.rb +1 -1
- data/spec/unit/liquid/tags/model_form_spec.rb +8 -0
- data/spec/unit/liquid/tags/path_to_spec.rb +26 -3
- data/spec/unit/liquid/tags/section_spec.rb +231 -0
- data/spec/unit/liquid/tags/sections_dropzone_spec.rb +107 -0
- data/spec/unit/middlewares/auth_spec.rb +9 -2
- data/spec/unit/middlewares/encoded_link_redirection_spec.rb +73 -0
- data/spec/unit/middlewares/entry_submission_spec.rb +1 -1
- data/spec/unit/middlewares/helpers_spec.rb +3 -64
- data/spec/unit/middlewares/locale_redirection_spec.rb +13 -2
- data/spec/unit/middlewares/locale_spec.rb +52 -10
- data/spec/unit/middlewares/page_spec.rb +57 -16
- data/spec/unit/middlewares/private_access_spec.rb +1 -1
- data/spec/unit/middlewares/redirection_spec.rb +1 -1
- data/spec/unit/middlewares/renderer_spec.rb +2 -1
- data/spec/unit/middlewares/section_spec.rb +70 -0
- data/spec/unit/middlewares/site_spec.rb +1 -1
- data/spec/unit/middlewares/sitemap_spec.rb +4 -42
- data/spec/unit/middlewares/url_redirection_spec.rb +1 -1
- data/spec/unit/repositories/content_entry_repository_spec.rb +13 -13
- data/spec/unit/repositories/section_repository_spec.rb +38 -0
- data/spec/unit/repositories/snippet_repository_spec.rb +0 -3
- data/spec/unit/services/action_service_spec.rb +19 -1
- data/spec/unit/services/asset_host_service_spec.rb +8 -0
- data/spec/unit/services/parent_finder_service_spec.rb +1 -2
- data/spec/unit/services/section_finder_service_spec.rb +28 -0
- data/spec/unit/services/url_builder_service_spec.rb +14 -4
- data/spec/unit/services/url_finder_service_spec.rb +100 -0
- data/spec/unit/services_spec.rb +34 -20
- metadata +72 -11
- data/lib/locomotive/steam/middlewares/helpers.rb +0 -140
- data/spec/fixtures/mongodb/sessions.bson +0 -0
@@ -8,11 +8,13 @@ module Locomotive::Steam
|
|
8
8
|
#
|
9
9
|
class LocaleRedirection < ThreadSafe
|
10
10
|
|
11
|
-
include Helpers
|
11
|
+
include Concerns::Helpers
|
12
12
|
|
13
13
|
def _call
|
14
|
-
if
|
15
|
-
redirect_to
|
14
|
+
if redirect_to_root_path_with_lang
|
15
|
+
redirect_to(path_with_locale, 302)
|
16
|
+
elsif url = redirect_url
|
17
|
+
redirect_to(url, redirect_type)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
@@ -21,13 +23,20 @@ module Locomotive::Steam
|
|
21
23
|
def redirect_url
|
22
24
|
if apply_redirection?
|
23
25
|
if site.prefix_default_locale
|
24
|
-
|
26
|
+
path_with_locale if locale_not_mentioned_in_path?
|
25
27
|
else
|
26
28
|
env['steam.path'] if default_locale? && locale_mentioned_in_path?
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
33
|
+
# only applied if redirect_url is not nil
|
34
|
+
def redirect_type
|
35
|
+
# We don't want a permanent redirection for the index page in case
|
36
|
+
# the user wants to change the current locale from the index page.
|
37
|
+
self.path == '/' && self.locales.size > 1 ? 302 : 301
|
38
|
+
end
|
39
|
+
|
31
40
|
def apply_redirection?
|
32
41
|
site.locales.size > 1 && request.get? && env['PATH_INFO'] != '/sitemap.xml'
|
33
42
|
end
|
@@ -44,12 +53,15 @@ module Locomotive::Steam
|
|
44
53
|
!locale_mentioned_in_path?
|
45
54
|
end
|
46
55
|
|
47
|
-
def
|
56
|
+
def path_with_locale
|
48
57
|
modify_path do |segments|
|
49
|
-
segments.insert(1,
|
58
|
+
segments.insert(1, locale)
|
50
59
|
end
|
51
60
|
end
|
52
61
|
|
62
|
+
def redirect_to_root_path_with_lang
|
63
|
+
locale_not_mentioned_in_path? && path.gsub(/^\//, '') == '' && !default_locale?
|
64
|
+
end
|
53
65
|
end
|
54
66
|
end
|
55
67
|
|
@@ -6,7 +6,7 @@ module Locomotive::Steam
|
|
6
6
|
#
|
7
7
|
class Page < ThreadSafe
|
8
8
|
|
9
|
-
include Helpers
|
9
|
+
include Concerns::Helpers
|
10
10
|
|
11
11
|
def _call
|
12
12
|
return env['steam.page'] if env['steam.page']
|
@@ -26,12 +26,12 @@ module Locomotive::Steam
|
|
26
26
|
protected
|
27
27
|
|
28
28
|
def fetch_page
|
29
|
-
page =
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end.first
|
29
|
+
page = site.routes.present? ? fetch_page_from_routes : nil
|
30
|
+
|
31
|
+
# if we don't find it from the site routes, try with the paths
|
32
|
+
page ||= fetch_page_from_paths
|
34
33
|
|
34
|
+
# make sure the page can be displayed, otherwise, display a nice 404 error page
|
35
35
|
if page && (page.published? || page.not_found? || live_editing?)
|
36
36
|
page
|
37
37
|
else
|
@@ -39,6 +39,35 @@ module Locomotive::Steam
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
def fetch_page_from_routes
|
43
|
+
site.routes.each do |definition|
|
44
|
+
route, handle = definition['route'], definition['page_handle']
|
45
|
+
|
46
|
+
_route = route.gsub(/:([a-z][a-z0-9_]+)/, '(?<\1>[^\/]+)').gsub(/^\//, '')
|
47
|
+
regexp = Regexp.new(/^#{_route}$/i)
|
48
|
+
|
49
|
+
if (matches = path.match(regexp))
|
50
|
+
log "Route found! #{route} (#{handle})"
|
51
|
+
|
52
|
+
# we want the named route parameters in the request params object
|
53
|
+
# because they will be needed in the liquid template.
|
54
|
+
self.merge_with_params(matches.named_captures)
|
55
|
+
|
56
|
+
return page_finder.by_handle(handle, false)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
nil # out of luck, find another way to get the page
|
61
|
+
end
|
62
|
+
|
63
|
+
def fetch_page_from_paths
|
64
|
+
page_finder.match(path).tap do |pages|
|
65
|
+
if pages.size > 1
|
66
|
+
self.log "Found multiple pages: #{pages.map(&:title).join(', ')}"
|
67
|
+
end
|
68
|
+
end.first
|
69
|
+
end
|
70
|
+
|
42
71
|
def page_finder
|
43
72
|
services.page_finder
|
44
73
|
end
|
@@ -3,7 +3,8 @@ module Locomotive::Steam
|
|
3
3
|
|
4
4
|
class Renderer < ThreadSafe
|
5
5
|
|
6
|
-
include Helpers
|
6
|
+
include Concerns::Helpers
|
7
|
+
include Concerns::LiquidContext
|
7
8
|
|
8
9
|
def _call
|
9
10
|
if page
|
@@ -45,87 +46,6 @@ module Locomotive::Steam
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
def liquid_context
|
49
|
-
::Liquid::Context.new(liquid_assigns, {}, liquid_registers, true)
|
50
|
-
end
|
51
|
-
|
52
|
-
def liquid_registers
|
53
|
-
{
|
54
|
-
request: request,
|
55
|
-
locale: locale,
|
56
|
-
site: site,
|
57
|
-
page: page,
|
58
|
-
services: services,
|
59
|
-
repositories: services.repositories,
|
60
|
-
logger: Locomotive::Common::Logger,
|
61
|
-
live_editing: !!env['steam.live_editing'],
|
62
|
-
session: request.session
|
63
|
-
}
|
64
|
-
end
|
65
|
-
|
66
|
-
def liquid_assigns
|
67
|
-
_default_liquid_assigns.merge(
|
68
|
-
_locale_liquid_assigns.merge(
|
69
|
-
_request_liquid_assigns.merge(
|
70
|
-
_http_actions_liquid_assigns.merge(
|
71
|
-
_steam_liquid_assigns))))
|
72
|
-
end
|
73
|
-
|
74
|
-
def _default_liquid_assigns
|
75
|
-
{
|
76
|
-
'current_page' => params[:page],
|
77
|
-
'params' => params.stringify_keys,
|
78
|
-
'now' => Time.zone.now,
|
79
|
-
'today' => Date.today,
|
80
|
-
'mode' => Locomotive::Steam.configuration.mode,
|
81
|
-
'wagon' => Locomotive::Steam.configuration.mode == :test,
|
82
|
-
'live_editing' => live_editing?
|
83
|
-
}
|
84
|
-
end
|
85
|
-
|
86
|
-
def _steam_liquid_assigns
|
87
|
-
{
|
88
|
-
'site' => site.to_liquid,
|
89
|
-
'page' => page.to_liquid,
|
90
|
-
'models' => Locomotive::Steam::Liquid::Drops::ContentTypes.new,
|
91
|
-
'contents' => Locomotive::Steam::Liquid::Drops::ContentTypes.new,
|
92
|
-
'session' => Locomotive::Steam::Liquid::Drops::SessionProxy.new,
|
93
|
-
}.merge(env['steam.liquid_assigns'])
|
94
|
-
end
|
95
|
-
|
96
|
-
def _locale_liquid_assigns
|
97
|
-
{
|
98
|
-
'locale' => locale.to_s,
|
99
|
-
'default_locale' => site.default_locale.to_s,
|
100
|
-
'locales' => site.locales.map(&:to_s)
|
101
|
-
}
|
102
|
-
end
|
103
|
-
|
104
|
-
def _request_liquid_assigns
|
105
|
-
{
|
106
|
-
'base_url' => request.base_url,
|
107
|
-
'fullpath' => request.fullpath,
|
108
|
-
'http_method' => request.request_method,
|
109
|
-
'ip_address' => request.ip,
|
110
|
-
'mounted_on' => mounted_on,
|
111
|
-
'path' => request.path,
|
112
|
-
'referer' => request.referer,
|
113
|
-
'url' => request.url,
|
114
|
-
'user_agent' => request.user_agent,
|
115
|
-
'host' => request.host_with_port
|
116
|
-
}
|
117
|
-
end
|
118
|
-
|
119
|
-
def _http_actions_liquid_assigns
|
120
|
-
{
|
121
|
-
'head?' => request.head?,
|
122
|
-
'get?' => request.get?,
|
123
|
-
'post?' => request.post?,
|
124
|
-
'put?' => request.put?,
|
125
|
-
'delete?' => request.delete?
|
126
|
-
}
|
127
|
-
end
|
128
|
-
|
129
49
|
end
|
130
50
|
|
131
51
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Locomotive::Steam
|
2
|
+
module Middlewares
|
3
|
+
class Section < ThreadSafe
|
4
|
+
|
5
|
+
include Concerns::Helpers
|
6
|
+
include Concerns::LiquidContext
|
7
|
+
|
8
|
+
def _call
|
9
|
+
if section_type = get_section_type
|
10
|
+
html = render(section_type)
|
11
|
+
render_response(html, 200)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def get_section_type
|
18
|
+
request.get_header('HTTP_LOCOMOTIVE_SECTION_TYPE')
|
19
|
+
end
|
20
|
+
|
21
|
+
def section_finder
|
22
|
+
services.section_finder
|
23
|
+
end
|
24
|
+
|
25
|
+
def render(section_type)
|
26
|
+
document = Liquid::Template.parse(liquid_source(section_type))
|
27
|
+
document.render(liquid_context)
|
28
|
+
end
|
29
|
+
|
30
|
+
def liquid_source(section_type)
|
31
|
+
"{% section '#{section_type}' %}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def liquid_registers
|
35
|
+
super.merge(_section_content: section_content)
|
36
|
+
end
|
37
|
+
|
38
|
+
def section_content
|
39
|
+
# looks like request.body is not always in the right state,
|
40
|
+
# the following statement makes sure of it.
|
41
|
+
request.body.rewind
|
42
|
+
|
43
|
+
if (data = request.body.read).present?
|
44
|
+
JSON.parse(data)['section_content']
|
45
|
+
else
|
46
|
+
{}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def live_editing?
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -3,7 +3,7 @@ module Locomotive::Steam
|
|
3
3
|
|
4
4
|
class Sitemap < ThreadSafe
|
5
5
|
|
6
|
-
include Helpers
|
6
|
+
include Concerns::Helpers
|
7
7
|
|
8
8
|
def _call
|
9
9
|
if env['PATH_INFO'] == '/sitemap.xml' && (page.nil? || page.not_found?)
|
@@ -14,78 +14,100 @@ module Locomotive::Steam
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def build_xml
|
17
|
-
<<-
|
17
|
+
<<-XML
|
18
18
|
<?xml version="1.0" encoding="UTF-8"?>
|
19
|
-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
20
|
-
<url>
|
21
|
-
<loc>#{base_url}</loc>
|
22
|
-
<priority>1.0</priority>
|
23
|
-
</url>
|
19
|
+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
24
20
|
#{build_pages_to_xml}
|
25
21
|
</urlset>
|
26
|
-
|
22
|
+
XML
|
27
23
|
end
|
28
24
|
|
29
25
|
def build_pages_to_xml
|
26
|
+
# we request the data based on the default locale
|
27
|
+
page_repository.locale = site.default_locale
|
28
|
+
|
30
29
|
page_repository.published.map do |page|
|
31
30
|
next if skip_page?(page)
|
32
31
|
|
33
|
-
|
32
|
+
_page = Locomotive::Steam::Decorators::I18nDecorator.new(page)
|
33
|
+
|
34
|
+
if page.templatized?
|
35
|
+
build_templatized_page_to_xml(_page)
|
36
|
+
else
|
37
|
+
build_page_to_xml(_page)
|
38
|
+
end
|
34
39
|
end.flatten.join.strip
|
35
40
|
end
|
36
41
|
|
37
|
-
def
|
38
|
-
|
42
|
+
def build_page_to_xml(page)
|
43
|
+
entry = { date: page.updated_at.to_date, links: [] }
|
39
44
|
|
40
|
-
site.locales.
|
41
|
-
|
45
|
+
site.locales.each_with_index do |locale, index|
|
46
|
+
page.__locale__ = locale
|
42
47
|
|
43
|
-
|
48
|
+
# if blank, means that the page is not translated, so skip it
|
49
|
+
next if page.title.blank?
|
44
50
|
|
45
|
-
if
|
46
|
-
|
51
|
+
if index == 0 # default locale
|
52
|
+
entry[:loc] = url_for(page, locale)
|
47
53
|
else
|
48
|
-
|
54
|
+
entry[:links] << { locale: locale, href: url_for(page, locale) }
|
49
55
|
end
|
50
56
|
end
|
57
|
+
|
58
|
+
entry_to_xml(entry)
|
51
59
|
end
|
52
60
|
|
53
|
-
def
|
61
|
+
def build_templatized_page_to_xml(page)
|
54
62
|
content_type = repositories.content_type.find(page.content_type_id)
|
55
63
|
|
56
|
-
|
64
|
+
repositories.content_entry.with(content_type).all({ _visible: true }).map do |content_entry|
|
65
|
+
_content_entry = Locomotive::Steam::Decorators::I18nDecorator.new(content_entry, locale)
|
66
|
+
entry = { date: content_entry.updated_at.to_date, links: [] }
|
57
67
|
|
58
|
-
|
59
|
-
|
68
|
+
site.locales.each_with_index do |locale, index|
|
69
|
+
page.__locale__ = locale
|
70
|
+
_content_entry.__locale__ = locale
|
60
71
|
|
61
|
-
|
72
|
+
# if blank, means that the page or the content entry is not translated, so skip it
|
73
|
+
next if _content_entry._label.blank? || page.title.blank?
|
62
74
|
|
63
|
-
|
75
|
+
page.content_entry = _content_entry
|
64
76
|
|
65
|
-
|
77
|
+
if index == 0 # default locale
|
78
|
+
entry[:loc] = url_for(page, locale)
|
79
|
+
else
|
80
|
+
entry[:links] << { locale: locale, href: url_for(page, locale) }
|
81
|
+
end
|
82
|
+
end
|
66
83
|
|
67
|
-
|
68
|
-
end
|
84
|
+
entry_to_xml(entry)
|
85
|
+
end.flatten.join.strip
|
69
86
|
end
|
70
87
|
|
71
|
-
def
|
72
|
-
|
73
|
-
|
74
|
-
<<-EOF
|
88
|
+
def entry_to_xml(entry)
|
89
|
+
<<-XML
|
75
90
|
<url>
|
76
|
-
<loc>#{base_url}#{
|
77
|
-
<lastmod>#{
|
78
|
-
|
91
|
+
<loc>#{base_url}#{entry[:loc]}</loc>
|
92
|
+
<lastmod>#{entry[:date].to_s('%Y-%m-%d')}</lastmod>
|
93
|
+
#{entry_links_to_xml(entry[:links])}
|
79
94
|
</url>
|
80
|
-
|
95
|
+
XML
|
96
|
+
end
|
97
|
+
|
98
|
+
def entry_links_to_xml(links)
|
99
|
+
links.map do |link|
|
100
|
+
<<-XML
|
101
|
+
<xhtml:link rel="alternate" hreflang="#{link[:locale]}" href="#{base_url}#{link[:href]}" />
|
102
|
+
XML
|
103
|
+
end.flatten.join.strip
|
81
104
|
end
|
82
105
|
|
83
106
|
def skip_page?(page)
|
84
|
-
page.index? ||
|
85
107
|
page.not_found? ||
|
86
108
|
page.layout? ||
|
87
109
|
page.redirect? ||
|
88
|
-
(!page.templatized? && !page.listed?)
|
110
|
+
(!page.templatized? && !page.index? && !page.listed?)
|
89
111
|
end
|
90
112
|
|
91
113
|
def repositories
|
@@ -100,15 +122,6 @@ module Locomotive::Steam
|
|
100
122
|
services.url_builder.url_for(page, locale)
|
101
123
|
end
|
102
124
|
|
103
|
-
def build_templatized_page_xml?(page, content_type, locale)
|
104
|
-
return true if content_type.localized? || default_locale == locale
|
105
|
-
|
106
|
-
# does the templatized page have the same source
|
107
|
-
# (liquid template) as in the default locale?
|
108
|
-
# If so, no need to add a xml entry for this page
|
109
|
-
!page.source.blank?
|
110
|
-
end
|
111
|
-
|
112
125
|
def base_url
|
113
126
|
"#{request.scheme}://#{request.host_with_port}"
|
114
127
|
end
|