Skip to content

Commit

Permalink
Issue warning when a Rails-style extension is used (like .html.erb) (
Browse files Browse the repository at this point in the history
…bridgetownrb#523)

* Issue warning when a Rails-style extension is used (like `.html.erb`)

* Fix Roda serving bug when `index.html` is missing
  • Loading branch information
jaredcwhite authored Apr 22, 2022
1 parent af90f27 commit 7d5ada8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bridgetown-core/lib/bridgetown-core/rack/roda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Roda < ::Roda
request.root do
output_folder = Bridgetown::Current.preloaded_configuration.destination
File.read(File.join(output_folder, "index.html"))
rescue StandardError
response.status = 500
"<p>ERROR: cannot find <code>index.html</code> in the output folder.</p>"
end
end

Expand Down
18 changes: 18 additions & 0 deletions bridgetown-core/lib/bridgetown-core/resource/destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Destination
# @param resource [Bridgetown::Resource::Base]
def initialize(resource)
@resource = resource
warn_on_rails_style_extension
@output_ext = resource.transformer.final_ext
end

Expand Down Expand Up @@ -46,6 +47,23 @@ def write(output)
Bridgetown.logger.debug "Writing:", path
File.write(path, output, mode: "wb")
end

private

def warn_on_rails_style_extension
return unless resource.relative_path.fnmatch?("*.{html,json,js}.*", File::FNM_EXTGLOB)

Bridgetown.logger.warn("Uh oh!", "You're using a Rails-style filename extension in:")
Bridgetown.logger.warn("", resource.relative_path)
Bridgetown.logger.warn(
"", "Instead, you can use either the desired output file extension or set a permalink."
)
Bridgetown.logger.warn(
"For more info:",
"https://www.bridgetownrb.com/docs/template-engines/erb-and-beyond#extensions-and-permalinks"
)
Bridgetown.logger.warn("")
end
end
end
end
5 changes: 5 additions & 0 deletions bridgetown-core/test/source/src/contacts/rails-style.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Rails-style
---

Should issue a warning!
9 changes: 8 additions & 1 deletion bridgetown-core/test/test_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestERB < BridgetownUnitTest
def setup
@site = fixture_site
@site.process
@process_output = capture_output { @site.process }
@erb_page = @site.resources.find { |p| p.data[:title] == "I'm an ERB Page" }
end

Expand Down Expand Up @@ -50,4 +50,11 @@ def setup
assert_includes @erb_page.output, "A partial success? YES!!"
end
end

context "Rails-style extensions" do
should "issue a warning" do
assert_includes @process_output, "Uh oh! You're using a Rails-style filename extension in:"
assert_includes @process_output, "rails-style.html.erb"
end
end
end
4 changes: 2 additions & 2 deletions bridgetown-core/test/test_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def to_liquid
g["items"].is_a?(Array),
"The list of grouped items for '' is not an Array."
)
assert_equal 17, g["items"].size
assert_equal 18, g["items"].size
end
end
end
Expand Down Expand Up @@ -1169,7 +1169,7 @@ def to_liquid
g["items"].is_a?(Array),
"The list of grouped items for '' is not an Array."
)
assert_equal 17, g["items"].size
assert_equal 18, g["items"].size
end
end
end
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/test/test_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class TestSite < BridgetownUnitTest
page_using_erb.erb
page_using_serb.serb
properties.html
rails-style.html.erb
sitemap.xml
static_files.html
symlinked-file
Expand Down

0 comments on commit 7d5ada8

Please sign in to comment.