fix various bugs + custom error when rendering a HAML template

did committed May 22, 2015
commit 90f183eef956dbf786153cc8897d23b9d0e88657
Showing 10 changed files with 58 additions and 18 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +1 -0
@@ @@ -3,6 +3,7 @@ require 'locomotive/common'
require_relative 'steam/configuration'
require_relative_all 'steam/decorators'
require_relative 'steam/liquid'
+ require_relative 'steam/errors'
require_relative 'steam/models'
require_relative_all 'steam/entities'
locomotive/steam/decorators/template_decorator.rb b/lib/locomotive/steam/decorators/template_decorator.rb +9 -1
@@ @@ -25,12 +25,20 @@ module Locomotive
end
if template_path.ends_with?('.haml')
- Haml::Engine.new(source).render
+ render_haml(source, template_path)
else
source
end
end
+ def render_haml(source, template_path)
+ begin
+ Haml::Engine.new(source).render
+ rescue Haml::SyntaxError => e
+ raise Steam::RenderError.new(e.message, template_path, source, e.line, e.backtrace)
+ end
+ end
+
end
end
locomotive/steam/errors.rb b/lib/locomotive/steam/errors.rb +32 -0
@@ @@ -0,0 +1,32 @@
+ module Locomotive::Steam
+
+ class RenderError < ::StandardError
+
+ LINES_RANGE = 10
+
+ attr_reader :file, :source, :line, :original_backtrace
+
+ def initialize(message, file, source, line, original_backtrace)
+ @file, @source, @line, @original_backtrace = file, source, line, original_backtrace
+ super(message)
+ end
+
+ def code_lines
+ return [] if source.blank? || line.nil?
+
+ lines = source.split("\n")
+
+ start = line - (LINES_RANGE / 2)
+ start = 0 if start < 0
+ finish = line + (LINES_RANGE / 2)
+
+ (start..finish).map { |i| [i, lines[i]] }
+ end
+
+ def backtrace
+ original_backtrace
+ end
+
+ end
+
+ end
locomotive/steam/liquid/drops/site.rb b/lib/locomotive/steam/liquid/drops/site.rb +2 -1
@@ @@ -22,7 +22,8 @@ module Locomotive
def scoped_pages
conditions = @context['with_scope'] || {}
- conditions['slug.ne'] = '404'
+ conditions['slug.ne'] = '404'
+ conditions[:published] = true
repository.all(conditions)
end
locomotive/steam/liquid/tags/extends.rb b/lib/locomotive/steam/liquid/tags/extends.rb +1 -1
@@ @@ -10,7 +10,7 @@ module Locomotive
parent = options[:parent_finder].find(options[:page], @template_name)
# no need to go further if the parent does not exist
- raise PageNotFound.new("Page with fullpath '#{@template_name}' was not found") if parent.nil?
+ raise PageNotFound.new("Extending a missing page. Page/Layout with fullpath '#{@template_name}' was not found") if parent.nil?
ActiveSupport::Notifications.instrument("steam.parse.extends", page: options[:page], parent: parent)
locomotive/steam/liquid/tags/seo.rb b/lib/locomotive/steam/liquid/tags/seo.rb +2 -2
@@ @@ -17,7 +17,7 @@ module Locomotive
def render_title(context)
title = self.value_for(:seo_title, context)
- title = context.registers[:site].name if title.blank?
+ title = context['site'].name if title.blank?
%{
<title>#{title}</title>
@@ @@ -38,7 +38,7 @@ module Locomotive
def value_for(attribute, context)
object = self.metadata_object(context)
- value = object.try(attribute.to_sym).blank? ? context.registers[:site].send(attribute.to_sym) : object.send(attribute.to_sym)
+ value = object.try(attribute.to_sym).blank? ? context['site'].send(attribute.to_sym) : object.send(attribute.to_sym)
self.sanitized_string(value)
end
locomotive/steam/services/parent_finder_service.rb b/lib/locomotive/steam/services/parent_finder_service.rb +4 -6
@@ @@ -8,12 +8,10 @@ module Locomotive
def find(page, fullpath)
return nil if fullpath.blank?
- decorate do
- if fullpath.strip == 'parent'
- repository.parent_of(page)
- else
- repository.by_fullpath(fullpath)
- end
+ if fullpath.strip == 'parent'
+ decorate { repository.parent_of(page) }
+ else
+ super(fullpath)
end
end
spec/fixtures/default/app/views/pages/tags/nav.liquid.haml +2 -2
@@ @@ -1,6 +1,6 @@
---
title: Page to test the nav tag
listed: false
- published: false
+ published: true
---
- {% nav site %}
\ No newline at end of file
+ {% nav site %}
spec/integration/server/sitemap_spec.rb +1 -1
@@ @@ -18,7 +18,7 @@ describe Locomotive::Steam::Server do
it 'checks if it looks valid' do
expect(Nokogiri::XML(subject).errors.empty?).to eq true
- expect(subject.scan(/<url>/).size).to eq 45
+ expect(subject.scan(/<url>/).size).to eq 46
expect(subject).to match("<loc>http://example.org/songs/song-number-2/band</loc>")
expect(subject).to match((<<-EOF
<url>
spec/unit/liquid/tags/seo_spec.rb +4 -4
@@ @@ -5,9 +5,9 @@ describe Locomotive::Steam::Liquid::Tags::SEO do
let(:page) { nil }
let(:content_entry) { nil }
- let(:site) { instance_double('Site', name: 'Acme', seo_title: 'Acme (SEO)', meta_description: 'A short site description', meta_keywords: 'test only cat dog') }
- let(:assigns) { { 'page' => page, 'content_entry' => content_entry } }
- let(:context) { ::Liquid::Context.new(assigns, {}, { site: site }) }
+ let(:site) { liquid_instance_double('Site', name: 'Acme', seo_title: 'Acme (SEO)', meta_description: 'A short site description', meta_keywords: 'test only cat dog') }
+ let(:assigns) { { 'site' => site, 'page' => page, 'content_entry' => content_entry } }
+ let(:context) { ::Liquid::Context.new(assigns, {}, {}) }
subject { render_template(source, context).strip }
@@ @@ -30,7 +30,7 @@ describe Locomotive::Steam::Liquid::Tags::SEO do
describe 'no seo_title site property' do
- let(:site) { instance_double('Site', name: 'Acme', seo_title: nil, meta_description: 'A short site description', meta_keywords: 'test only cat dog') }
+ let(:site) { liquid_instance_double('Site', name: 'Acme', seo_title: nil, meta_description: 'A short site description', meta_keywords: 'test only cat dog') }
it { is_expected.to eq '<title>Acme</title>' }
end