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.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +31 -25
  4. data/Rakefile +2 -2
  5. data/config/locales/pt-BR.yml +43 -13
  6. data/lib/locomotive/steam.rb +9 -4
  7. data/lib/locomotive/steam/adapters/filesystem.rb +2 -7
  8. data/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +0 -2
  9. data/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +11 -0
  10. data/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +37 -0
  11. data/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb +18 -0
  12. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +4 -3
  13. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb +4 -3
  14. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb +40 -0
  15. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb +0 -1
  16. data/lib/locomotive/steam/adapters/memory.rb +1 -1
  17. data/lib/locomotive/steam/entities/content_entry.rb +15 -3
  18. data/lib/locomotive/steam/entities/content_type.rb +2 -1
  19. data/lib/locomotive/steam/entities/page.rb +21 -19
  20. data/lib/locomotive/steam/entities/section.rb +24 -0
  21. data/lib/locomotive/steam/entities/site.rb +3 -1
  22. data/lib/locomotive/steam/liquid.rb +2 -0
  23. data/lib/locomotive/steam/liquid/drops/page.rb +4 -2
  24. data/lib/locomotive/steam/liquid/drops/params.rb +51 -0
  25. data/lib/locomotive/steam/liquid/drops/section.rb +56 -0
  26. data/lib/locomotive/steam/liquid/drops/section_block.rb +47 -0
  27. data/lib/locomotive/steam/liquid/drops/section_content_proxy.rb +97 -0
  28. data/lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb +65 -0
  29. data/lib/locomotive/steam/liquid/drops/site.rb +2 -1
  30. data/lib/locomotive/steam/liquid/errors.rb +2 -0
  31. data/lib/locomotive/steam/liquid/filters/base.rb +3 -3
  32. data/lib/locomotive/steam/liquid/filters/date.rb +1 -1
  33. data/lib/locomotive/steam/liquid/filters/html.rb +7 -2
  34. data/lib/locomotive/steam/liquid/filters/json.rb +3 -1
  35. data/lib/locomotive/steam/liquid/tags/action.rb +2 -2
  36. data/lib/locomotive/steam/liquid/tags/alt_page_links.rb +63 -0
  37. data/lib/locomotive/steam/liquid/tags/concerns/i18n_page.rb +1 -1
  38. data/lib/locomotive/steam/liquid/tags/concerns/section.rb +117 -0
  39. data/lib/locomotive/steam/liquid/tags/global_section.rb +32 -0
  40. data/lib/locomotive/steam/liquid/tags/model_form.rb +1 -1
  41. data/lib/locomotive/steam/liquid/tags/section.rb +87 -0
  42. data/lib/locomotive/steam/liquid/tags/sections_dropzone.rb +56 -0
  43. data/lib/locomotive/steam/liquid/tags/snippet.rb +3 -2
  44. data/lib/locomotive/steam/liquid/template.rb +0 -5
  45. data/lib/locomotive/steam/middlewares.rb +1 -1
  46. data/lib/locomotive/steam/middlewares/auth.rb +29 -13
  47. data/lib/locomotive/steam/middlewares/concerns/helpers.rb +59 -0
  48. data/lib/locomotive/steam/middlewares/concerns/liquid_context.rb +94 -0
  49. data/lib/locomotive/steam/middlewares/encoded_link_redirection.rb +45 -0
  50. data/lib/locomotive/steam/middlewares/entry_submission.rb +1 -1
  51. data/lib/locomotive/steam/middlewares/favicon.rb +1 -1
  52. data/lib/locomotive/steam/middlewares/locale.rb +63 -15
  53. data/lib/locomotive/steam/middlewares/locale_redirection.rb +18 -6
  54. data/lib/locomotive/steam/middlewares/logging.rb +1 -1
  55. data/lib/locomotive/steam/middlewares/page.rb +35 -6
  56. data/lib/locomotive/steam/middlewares/path.rb +1 -1
  57. data/lib/locomotive/steam/middlewares/private_access.rb +1 -1
  58. data/lib/locomotive/steam/middlewares/redirection.rb +1 -1
  59. data/lib/locomotive/steam/middlewares/renderer.rb +2 -82
  60. data/lib/locomotive/steam/middlewares/robots.rb +1 -1
  61. data/lib/locomotive/steam/middlewares/section.rb +56 -0
  62. data/lib/locomotive/steam/middlewares/site.rb +1 -1
  63. data/lib/locomotive/steam/middlewares/sitemap.rb +58 -45
  64. data/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
  65. data/lib/locomotive/steam/middlewares/thread_safe.rb +85 -2
  66. data/lib/locomotive/steam/middlewares/timezone.rb +1 -1
  67. data/lib/locomotive/steam/middlewares/url_redirection.rb +1 -1
  68. data/lib/locomotive/steam/models/entity.rb +0 -2
  69. data/lib/locomotive/steam/models/mapper.rb +5 -3
  70. data/lib/locomotive/steam/models/scope.rb +8 -0
  71. data/lib/locomotive/steam/repositories.rb +4 -0
  72. data/lib/locomotive/steam/repositories/content_entry_repository.rb +6 -2
  73. data/lib/locomotive/steam/repositories/page_repository.rb +4 -1
  74. data/lib/locomotive/steam/repositories/section_repository.rb +14 -0
  75. data/lib/locomotive/steam/repositories/site_repository.rb +1 -1
  76. data/lib/locomotive/steam/repositories/snippet_repository.rb +0 -3
  77. data/lib/locomotive/steam/server.rb +3 -1
  78. data/lib/locomotive/steam/services.rb +17 -1
  79. data/lib/locomotive/steam/services/action_service.rb +10 -0
  80. data/lib/locomotive/steam/services/concerns/decorator.rb +0 -2
  81. data/lib/locomotive/steam/services/image_resizer_service.rb +1 -1
  82. data/lib/locomotive/steam/services/page_finder_service.rb +6 -0
  83. data/lib/locomotive/steam/services/section_finder_service.rb +17 -0
  84. data/lib/locomotive/steam/services/url_builder_service.rb +31 -17
  85. data/lib/locomotive/steam/services/url_finder_service.rb +87 -0
  86. data/lib/locomotive/steam/version.rb +2 -2
  87. data/locomotivecms_steam.gemspec +1 -1
  88. data/spec/fixtures/default/app/views/pages/tags/section.liquid.haml +11 -0
  89. data/spec/fixtures/default/app/views/sections/carousel.liquid +20 -0
  90. data/spec/fixtures/default/app/views/sections/footer.liquid +48 -0
  91. data/spec/fixtures/default/app/views/sections/header.liquid +54 -0
  92. data/spec/fixtures/default/config/deploy.yml +1 -1
  93. data/spec/fixtures/errors/section_bad_json_content.liquid +9 -0
  94. data/spec/fixtures/errors/section_bad_json_header.liquid +8 -0
  95. data/spec/fixtures/mongodb/locomotive_accounts.bson +0 -0
  96. data/spec/fixtures/mongodb/locomotive_accounts.metadata.json +1 -1
  97. data/spec/fixtures/mongodb/locomotive_activities.bson +0 -0
  98. data/spec/fixtures/mongodb/locomotive_activities.metadata.json +1 -1
  99. data/spec/fixtures/mongodb/locomotive_content_assets.bson +0 -0
  100. data/spec/fixtures/mongodb/locomotive_content_assets.metadata.json +1 -1
  101. data/spec/fixtures/mongodb/locomotive_content_entries.bson +0 -0
  102. data/spec/fixtures/mongodb/locomotive_content_entries.metadata.json +1 -1
  103. data/spec/fixtures/mongodb/locomotive_content_types.bson +0 -0
  104. data/spec/fixtures/mongodb/locomotive_content_types.metadata.json +1 -1
  105. data/spec/fixtures/mongodb/locomotive_pages.bson +0 -0
  106. data/spec/fixtures/mongodb/locomotive_pages.metadata.json +1 -1
  107. data/spec/fixtures/mongodb/locomotive_sections.bson +0 -0
  108. data/spec/fixtures/mongodb/{sessions.metadata.json → locomotive_sections.metadata.json} +1 -1
  109. data/spec/fixtures/mongodb/locomotive_sites.bson +0 -0
  110. data/spec/fixtures/mongodb/locomotive_sites.metadata.json +1 -1
  111. data/spec/fixtures/mongodb/locomotive_snippets.bson +0 -0
  112. data/spec/fixtures/mongodb/locomotive_snippets.metadata.json +1 -1
  113. data/spec/fixtures/mongodb/locomotive_theme_assets.bson +0 -0
  114. data/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json +1 -1
  115. data/spec/fixtures/mongodb/locomotive_translations.bson +0 -0
  116. data/spec/fixtures/mongodb/locomotive_translations.metadata.json +1 -1
  117. data/spec/integration/repositories/content_entry_repository_spec.rb +1 -1
  118. data/spec/integration/repositories/page_repository_spec.rb +1 -1
  119. data/spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
  120. data/spec/integration/server/auth_spec.rb +0 -4
  121. data/spec/integration/server/basic_spec.rb +8 -0
  122. data/spec/integration/server/contact_form_spec.rb +15 -2
  123. data/spec/integration/server/sitemap_spec.rb +5 -3
  124. data/spec/integration/services/content_entry_service_spec.rb +1 -1
  125. data/spec/support/helpers.rb +3 -3
  126. data/spec/unit/adapters/filesystem/sanitizers/section_spec.rb +65 -0
  127. data/spec/unit/adapters/filesystem/sanitizers/site_spec.rb +28 -2
  128. data/spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +8 -4
  129. data/spec/unit/adapters/filesystem/yaml_loaders/content_type_spec.rb +2 -2
  130. data/spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +1 -1
  131. data/spec/unit/adapters/filesystem/yaml_loaders/section_spec.rb +27 -0
  132. data/spec/unit/entities/section_spec.rb +29 -0
  133. data/spec/unit/liquid/drops/params_spec.rb +38 -0
  134. data/spec/unit/liquid/drops/section_content_proxy_spec.rb +88 -0
  135. data/spec/unit/liquid/drops/section_spec.rb +26 -0
  136. data/spec/unit/liquid/filters/html_spec.rb +198 -170
  137. data/spec/unit/liquid/filters/json_spec.rb +7 -0
  138. data/spec/unit/liquid/tags/action_spec.rb +12 -2
  139. data/spec/unit/liquid/tags/alt_page_links_spec.rb +58 -0
  140. data/spec/unit/liquid/tags/global_section_spec.rb +130 -0
  141. data/spec/unit/liquid/tags/link_to_spec.rb +1 -1
  142. data/spec/unit/liquid/tags/locale_switcher_spec.rb +1 -1
  143. data/spec/unit/liquid/tags/model_form_spec.rb +8 -0
  144. data/spec/unit/liquid/tags/path_to_spec.rb +26 -3
  145. data/spec/unit/liquid/tags/section_spec.rb +231 -0
  146. data/spec/unit/liquid/tags/sections_dropzone_spec.rb +107 -0
  147. data/spec/unit/middlewares/auth_spec.rb +9 -2
  148. data/spec/unit/middlewares/encoded_link_redirection_spec.rb +73 -0
  149. data/spec/unit/middlewares/entry_submission_spec.rb +1 -1
  150. data/spec/unit/middlewares/helpers_spec.rb +3 -64
  151. data/spec/unit/middlewares/locale_redirection_spec.rb +13 -2
  152. data/spec/unit/middlewares/locale_spec.rb +52 -10
  153. data/spec/unit/middlewares/page_spec.rb +57 -16
  154. data/spec/unit/middlewares/private_access_spec.rb +1 -1
  155. data/spec/unit/middlewares/redirection_spec.rb +1 -1
  156. data/spec/unit/middlewares/renderer_spec.rb +2 -1
  157. data/spec/unit/middlewares/section_spec.rb +70 -0
  158. data/spec/unit/middlewares/site_spec.rb +1 -1
  159. data/spec/unit/middlewares/sitemap_spec.rb +4 -42
  160. data/spec/unit/middlewares/url_redirection_spec.rb +1 -1
  161. data/spec/unit/repositories/content_entry_repository_spec.rb +13 -13
  162. data/spec/unit/repositories/section_repository_spec.rb +38 -0
  163. data/spec/unit/repositories/snippet_repository_spec.rb +0 -3
  164. data/spec/unit/services/action_service_spec.rb +19 -1
  165. data/spec/unit/services/asset_host_service_spec.rb +8 -0
  166. data/spec/unit/services/parent_finder_service_spec.rb +1 -2
  167. data/spec/unit/services/section_finder_service_spec.rb +28 -0
  168. data/spec/unit/services/url_builder_service_spec.rb +14 -4
  169. data/spec/unit/services/url_finder_service_spec.rb +100 -0
  170. data/spec/unit/services_spec.rb +34 -20
  171. metadata +72 -11
  172. data/lib/locomotive/steam/middlewares/helpers.rb +0 -140
  173. data/spec/fixtures/mongodb/sessions.bson +0 -0
@@ -0,0 +1,65 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Drops
5
+
6
+ # Required to allow the sync between the Locomotive editor
7
+ # and the string/text inputs of a section and section block
8
+ class SectionEditorSettingData < ::Liquid::Drop
9
+
10
+ def initialize(section)
11
+ @section = section
12
+ end
13
+
14
+ def before_method(meth)
15
+ block = nil
16
+ prefix = "section-#{@context['section'].id}"
17
+ matches = (@context['forloop.name'] || '').match(SECTIONS_BLOCK_FORLOOP_REGEXP)
18
+
19
+ # are we inside a block?
20
+ if matches && variable_name = matches[:name]
21
+ block = @context[variable_name]
22
+ prefix += "-block.#{block.id}"
23
+ end
24
+
25
+ # only string and text inputs can synced
26
+ if is_text?(meth.to_s, block)
27
+ %( data-locomotive-editor-setting="#{prefix}.#{meth}")
28
+ else
29
+ ''
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def is_text?(id, block)
36
+ settings = block ? block_settings(block['type']) : section_settings
37
+
38
+ # can happen if the developer forgets to assign a type to
39
+ # the default blocks
40
+ return false if settings.blank?
41
+
42
+ text_inputs(settings).include?(id)
43
+ end
44
+
45
+ def text_inputs(settings)
46
+ settings.map do |input|
47
+ %w(text textarea).include?(input['type']) ? input['id'] : nil
48
+ end.compact
49
+ end
50
+
51
+ def block_settings(type)
52
+ @section.definition['blocks'].find do |block|
53
+ block['type'] == type
54
+ end&.fetch('settings', nil)
55
+ end
56
+
57
+ def section_settings
58
+ @section.definition['settings']
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+ end
@@ -4,7 +4,8 @@ module Locomotive
4
4
  module Drops
5
5
  class Site < I18nBase
6
6
 
7
- delegate :name, :handle, :domains, :seo_title, :meta_keywords, :meta_description, :asset_host, to: :@_source
7
+ delegate :name, :handle, :domains, :seo_title, :meta_keywords,
8
+ :meta_description, :asset_host, :sections_content, to: :@_source
8
9
 
9
10
  def index
10
11
  @index ||= repository.root.to_liquid
@@ -5,6 +5,8 @@ module Locomotive
5
5
 
6
6
  class SnippetNotFound < ::Liquid::Error; end
7
7
 
8
+ class SectionNotFound < ::Liquid::Error; end
9
+
8
10
  class PageNotTranslated < ::Liquid::Error; end
9
11
  end
10
12
  end
@@ -35,14 +35,14 @@ module Locomotive
35
35
  input.respond_to?(:url) ? input.url : input
36
36
  end
37
37
 
38
- def css_js_asset_url(input, extension, folder)
38
+ def any_asset_url(input, extension = nil, folder = nil)
39
39
  return '' if input.nil?
40
40
 
41
41
  if input =~ /^https?:/
42
42
  input
43
43
  else
44
- uri = input.starts_with?('/') ? URI(input) : URI(asset_url("#{folder}/#{input}"))
45
- uri.path = "#{uri.path}#{extension}" unless uri.path.ends_with?(extension)
44
+ uri = input.starts_with?('/') ? URI(input) : URI(asset_url(folder.blank? ? input : "#{folder}/#{input}"))
45
+ uri.path = "#{uri.path}#{extension}" unless extension.blank? || uri.path.ends_with?(extension)
46
46
  uri.to_s
47
47
  end
48
48
  end
@@ -17,7 +17,7 @@ module Locomotive
17
17
  end
18
18
  end
19
19
 
20
- def parse_date(input, format)
20
+ def parse_date(input, format = nil)
21
21
  return '' if input.blank?
22
22
 
23
23
  format ||= I18n.t('date.formats.default')
@@ -18,10 +18,15 @@ module Locomotive
18
18
  %{<link rel="#{rel}" type="#{type}" title="#{title}" href="#{input}" />}
19
19
  end
20
20
 
21
+ # Write the url of any theme file
22
+ def theme_asset_url(input)
23
+ any_asset_url(input)
24
+ end
25
+
21
26
  # Write the url of a theme stylesheet
22
27
  # input: name of the css file
23
28
  def stylesheet_url(input)
24
- css_js_asset_url(input, '.css', 'stylesheets')
29
+ any_asset_url(input, '.css', 'stylesheets')
25
30
  end
26
31
 
27
32
  # Write the link tag of a theme stylesheet
@@ -37,7 +42,7 @@ module Locomotive
37
42
  # Write the url to javascript resource
38
43
  # input: name of the javascript file
39
44
  def javascript_url(input)
40
- css_js_asset_url(input, '.js', 'javascripts')
45
+ any_asset_url(input, '.js', 'javascripts')
41
46
  end
42
47
 
43
48
  # Write the link to javascript resource
@@ -9,7 +9,9 @@ module Locomotive
9
9
  fields = fields.split(',').map(&:strip)
10
10
  end
11
11
 
12
- if input.respond_to?(:each)
12
+ if input.is_a?(Hash)
13
+ object_to_json(input, fields)
14
+ elsif input.respond_to?(:each)
13
15
  '[' + input.map do |object|
14
16
  fields.size == 1 ? object[fields.first].to_json : object_to_json(object, fields)
15
17
  end.join(',') + ']'
@@ -54,9 +54,9 @@ module Locomotive
54
54
  end
55
55
 
56
56
  def safe_params(context)
57
- return {} if context['params'].blank?
57
+ return {} if context.registers[:params].blank?
58
58
 
59
- context['params'].dup.tap do |params|
59
+ context.registers[:params].dup.tap do |params|
60
60
  # Tempfile can't be converted in Duktape for obvious reasons
61
61
  replace_tempfile(params)
62
62
  end
@@ -0,0 +1,63 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Tags
5
+
6
+ class AltPageLinks < ::Liquid::Tag
7
+
8
+ include Concerns::I18nPage
9
+
10
+ def render(context)
11
+ set_vars_from_context(context)
12
+
13
+ if @site.locales.size == 1
14
+ ''
15
+ else
16
+ (
17
+ [%(<link rel="alternate" hreflang="x-default" href="#{url_for(@site.default_locale, true)}" />)] +
18
+ @site.locales.map do |locale|
19
+ %(<link rel="alternate" hreflang="#{locale}" href="#{url_for(locale)}" />)
20
+ end
21
+ ).join("\n")
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ # Examples:
28
+ # - http://www.example.com/fr (even if the default locale is fr)
29
+ # - http://www.example.com/ (as the default url (x-default) for the index page)
30
+ #
31
+ # Note: the index page has a different behaviour because rendering "/" depends
32
+ # on the language returned by the browser (so might be different based on the user session).
33
+ def url_for(locale, default = false)
34
+ change_page_locale(locale, @page) do
35
+ fullpath = services.url_builder.url_for(@page.send(:_source), locale, @page.index? && !default ? true : nil)
36
+
37
+ if @page.index? && default
38
+ fullpath.gsub!(/\/#{locale}$/, '/')
39
+ end
40
+
41
+ @base_url + fullpath
42
+ end
43
+ end
44
+
45
+ def services
46
+ @context.registers[:services]
47
+ end
48
+
49
+ def set_vars_from_context(context)
50
+ @context = context
51
+ @site = context.registers[:site]
52
+ @page = context['page']
53
+ @base_url = context['base_url']
54
+ end
55
+
56
+
57
+ end
58
+
59
+ ::Liquid::Template.register_tag('alt_page_links'.freeze, AltPageLinks)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -10,7 +10,7 @@ module Locomotive
10
10
  page = drop.send(:_source)
11
11
 
12
12
  page.__with_locale__(locale) do
13
- if page.templatized?
13
+ if page.templatized? && page.content_entry
14
14
  page.content_entry.__with_locale__(locale) { yield }
15
15
  else
16
16
  yield
@@ -0,0 +1,117 @@
1
+ module Locomotive::Steam::Liquid::Tags::Concerns
2
+
3
+ module Section
4
+
5
+ private
6
+
7
+ def render_section(context, template, section, content)
8
+ context.stack do
9
+ # build a drop from the content and add it to the new context
10
+ context['section'] = Locomotive::Steam::Liquid::Drops::Section.new(
11
+ section,
12
+ content
13
+ )
14
+
15
+ # assign an id if specified in the context
16
+ context['section'].id ||= context['section_id'] if context['section_id'].present?
17
+
18
+ begin
19
+ _render(context, template)
20
+ rescue Locomotive::Steam::ParsingRenderingError => e
21
+ e.file = section.name + ' [Section]'
22
+ raise e
23
+ end
24
+ end
25
+ end
26
+
27
+ def _render(context, template)
28
+ if context.registers[:live_editing]
29
+ editor_settings_lookup(template.root)
30
+ end
31
+
32
+ context.stack do
33
+ html = template.render(context)
34
+ section = context['section']
35
+
36
+ tag_id = %(id="locomotive-section-#{section.id}")
37
+ tag_class = %(class="#{['locomotive-section', section.css_class].compact.join(' ')}")
38
+ tag_data = %(data-locomotive-section-type="#{section.type}")
39
+
40
+ %(<div #{tag_id} #{tag_class} #{tag_data}>#{html}</div>)
41
+ end
42
+ end
43
+
44
+ # in order to enable string/text synchronization with the editor:
45
+ # - find variables like {{ section.settings.<id> }} or {{ block.settings.<id> }}
46
+ # - once found, get the closest tag
47
+ # - add custom data attributes to it
48
+ def editor_settings_lookup(root)
49
+ previous_node = nil
50
+ new_nodelist = []
51
+
52
+ return if root.nodelist.blank?
53
+
54
+ root.nodelist.each_with_index do |node, index|
55
+ if node.is_a?(::Liquid::Variable) && previous_node.is_a?(::Liquid::Token)
56
+ matches = node.raw.match(Locomotive::Steam::SECTIONS_SETTINGS_VARIABLE_REGEXP)
57
+
58
+ # is a section setting variable?
59
+ if matches && matches[:id] && wrapped_around_tag?(index, root.nodelist)
60
+ # open the closest HTML tag
61
+ previous_node.gsub!(/>(\s*)\z/, '\1')
62
+
63
+ # here we go, add a liquid variable!
64
+ new_nodelist.push(::Liquid::Variable.new(
65
+ "section.editor_setting_data.#{matches[:id]}",
66
+ node.instance_variable_get(:@options))
67
+ )
68
+
69
+ # close the tag
70
+ new_nodelist.push(::Liquid::Token.new('>', previous_node.line_number))
71
+ end
72
+ elsif node.respond_to?(:nodelist)
73
+ editor_settings_lookup(node)
74
+ end
75
+
76
+ new_nodelist.push(node)
77
+
78
+ previous_node = node
79
+ end
80
+
81
+ (root.instance_variable_get(:@body) || root).instance_variable_set(:@nodelist, new_nodelist)
82
+ end
83
+
84
+ def wrapped_around_tag?(index, nodelist)
85
+ return false if index + 1 >= nodelist.size
86
+
87
+ previous_node = nodelist[index - 1]
88
+ next_node = nodelist[index + 1]
89
+
90
+ return false unless next_node.is_a?(::Liquid::Token)
91
+
92
+ (previous_node =~ /\>\s*\z/).present? && (next_node =~ /\A\s*\</).present?
93
+ end
94
+
95
+ def notify_on_parsing(type, source: :page, is_dropzone: false, key: nil, id: nil, label: nil, placement: nil)
96
+ ActiveSupport::Notifications.instrument('steam.parse.section', {
97
+ attributes: {
98
+ type: type,
99
+ source: source,
100
+ id: id,
101
+ key: key,
102
+ is_dropzone: is_dropzone,
103
+ label: label,
104
+ placement: placement
105
+ },
106
+ page: options[:page],
107
+ block: current_inherited_block_name
108
+ })
109
+ end
110
+
111
+ def current_inherited_block_name
112
+ options[:inherited_blocks].try(:[], :nested).try(:last).try(:name)
113
+ end
114
+
115
+ end
116
+
117
+ end
@@ -0,0 +1,32 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Tags
5
+ class GlobalSection < Locomotive::Steam::Liquid::Tags::Section
6
+
7
+ def parse(tokens)
8
+ notify_on_parsing(evaluate_section_name,
9
+ source: :site,
10
+ id: "site-#{evaluate_section_name}",
11
+ key: evaluate_section_name,
12
+ placement: @section_options[:placement]&.to_sym
13
+ )
14
+ end
15
+
16
+ private
17
+
18
+ def find_section_content(context)
19
+ context['site']&.sections_content&.fetch(@section_type, nil)
20
+ end
21
+
22
+ def set_section_dom_id(context)
23
+ context['section_id'] = "site-#{@section_type}"
24
+ end
25
+
26
+ end
27
+
28
+ ::Liquid::Template.register_tag('global_section'.freeze, GlobalSection)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -79,7 +79,7 @@ module Locomotive
79
79
  if url.blank?
80
80
  if options[:json]
81
81
  url = current_context['path'].blank? ? '/' : current_context['path']
82
- url + 'index.json'
82
+ url + (url.ends_with?('/') ? 'index.json' : '.json')
83
83
  else
84
84
  nil
85
85
  end
@@ -0,0 +1,87 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Tags
5
+ class Section < ::Liquid::Include
6
+
7
+ include Concerns::Section
8
+
9
+ def initialize(tag_name, markup, options)
10
+ if markup =~ /(#{::Liquid::QuotedString}|#{::Liquid::VariableSignature}+)\s*,*(.*)?/o
11
+ @section_type, _options = $1, $2
12
+ raw_options = parse_options_from_string(_options)
13
+ @section_options = interpolate_options(raw_options, {})
14
+ super
15
+ else
16
+ raise ::Liquid::SyntaxError.new("Syntax Error in 'section' - Valid syntax: section section_type, id: '<string>', placement: 'top|bottom' (id and placement are optional)")
17
+ end
18
+ end
19
+
20
+ def parse(tokens)
21
+ notify_on_parsing(evaluate_section_name,
22
+ id: "page-#{@section_options[:id] || evaluate_section_name}",
23
+ key: (@section_options[:id] || evaluate_section_name).to_s,
24
+ label: @section_options[:label],
25
+ placement: @section_options[:placement]&.to_sym
26
+ )
27
+ end
28
+
29
+ def render(context)
30
+ # @options doesn't include the page key if cache is on
31
+ @options[:page] = context.registers[:page]
32
+
33
+ # get the type/slug of the section
34
+ # @section_options = interpolate_options(@raw_section_options, context)
35
+ @section_type = evaluate_section_name(context)
36
+ @template_name = "sections-#{@section_type}"
37
+
38
+ section = find_section(context)
39
+ template = load_cached_partial(context)
40
+
41
+ # if the tag is called by the Section middleware, use the content
42
+ # from the request.
43
+ content = context.registers[:_section_content]
44
+
45
+ # if no content from the middleware, go get it from the page
46
+ content ||= find_section_content(context)
47
+
48
+ context.stack do
49
+ set_section_dom_id(context)
50
+ render_section(context, template, section, content)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def set_section_dom_id(context)
57
+ context['section_id'] = "page-#{@section_options[:id] || @section_type}"
58
+ end
59
+
60
+ def read_template_from_file_system(context)
61
+ section = find_section(context)
62
+ raise SectionNotFound.new("Section with slug '#{@section_type}' was not found") if section.nil?
63
+ section.liquid_source
64
+ end
65
+
66
+ def find_section(context)
67
+ context.registers[:services].section_finder.find(@section_type)
68
+ end
69
+
70
+ def find_section_content(context)
71
+ section_id = @section_options[:id].presence || @section_type
72
+ context['page']&.sections_content&.fetch(section_id, nil)
73
+ end
74
+
75
+ def evaluate_section_name(context = nil)
76
+ context.try(:evaluate, @template_name) ||
77
+ (!@template_name.is_a?(String) && @template_name.send(:state).first) ||
78
+ @template_name
79
+ end
80
+
81
+ end
82
+
83
+ ::Liquid::Template.register_tag('section'.freeze, Section)
84
+ end
85
+ end
86
+ end
87
+ end