Skip to content

Commit

Permalink
Adjust whitespace handling in front matter regexps, improve line numb…
Browse files Browse the repository at this point in the history
…ers in errors
  • Loading branch information
jaredcwhite committed May 4, 2024
1 parent ecbf29b commit 9b89961
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ module Loaders
# %}~~~
# ~~~~
class Ruby < Base
HEADER = %r!\A[~`#-]{3,}(?:ruby|<%|{%)\s*\n!
BLOCK = %r!#{HEADER.source}(.*?\n?)^((?:%>|%})?[~`#-]{3,}\s*$\n?)!m
HEADER = %r!\A[~`#-]{3,}(?:ruby|<%|{%)[ \t]*\n!
BLOCK = %r!#{HEADER.source}(.*?\n?)^((?:%>|%})?[~`#-]{3,}[ \t]*$\n?)!m

# Determines whether a given file has Ruby front matter
#
Expand All @@ -79,7 +79,7 @@ def read(file_contents, file_path:)
Result.new(
content: ruby_content.post_match,
front_matter: process_ruby_data(ruby_content[1], file_path, 2),
line_count: ruby_content[1].lines.size
line_count: ruby_content[1].lines.size - 1
)
elsif self.class.header?(file_path)
Result.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module Loaders
# ---
# ~~~
class YAML < Base
HEADER = %r!\A---\s*\n!
BLOCK = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
HEADER = %r!\A---[ \t]*\n!
BLOCK = %r!#{HEADER.source}(.*?\n?)^((---|\.\.\.)[ \t]*$\n?)!m

# Determines whether a given file has YAML front matter
#
Expand Down
10 changes: 4 additions & 6 deletions bridgetown-core/lib/bridgetown-core/model/builder_origin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ def read
@data
end

def front_matter_line_count
@data[:_front_matter_line_count_]
end
def front_matter_line_count = @data[:_front_matter_line_count_]

def exists?
false
end
def original_path = @data[:_original_path_] || relative_path

def exists? = false

def read_data_from_builder
builder = Kernel.const_get(url.host.gsub(".", "::"))
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-routes/lib/bridgetown-routes/code_blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def eval_route_file(file, file_slug, app) # rubocop:disable Lint/UnusedMethodArg
if ruby_content
code = ruby_content[1]
code_postmatch = ruby_content.post_match
front_matter_line_count = code.lines.count
front_matter_line_count = code.lines.count - 1
code.concat("\nrender_with {}") unless code.match?(%r!^\s*render_with\s|\(!)
end

Expand Down
1 change: 1 addition & 0 deletions bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def render_with(data: {}, &) # rubocop:todo Metrics/AbcSize, Metrics/MethodLengt
)
).read do
data[:_collection_] = bridgetown_site.collections.pages
data[:_original_path_] = path
data[:_relative_path_] = source_path
data[:_front_matter_line_count_] = response._front_matter_line_count
data[:_content_] = code
Expand Down
14 changes: 7 additions & 7 deletions bridgetown-routes/test/test_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def site
FileUtils.cp(index_file, index_file.sub("test_index.erb", "index.erb"))
get "/"
assert last_response.ok?
assert_equal "<h1>Dynamic Index</h1>", last_response.body
assert_equal "\n<h1>Dynamic Index</h1>", last_response.body
FileUtils.remove_file(index_file.sub("test_index.erb", "index.erb"))
end

Expand All @@ -38,32 +38,32 @@ def site

should "return HTML for the howdy route" do
get "/howdy?yo=joe&happy=pleased"
assert_equal "<h1>joe 42</h1>\n\n<p>I am pleasedpleased.</p>\n", last_response.body
assert_equal "\n<h1>joe 42</h1>\n\n<p>I am pleasedpleased.</p>\n", last_response.body
end

should "return HTML for a route in an arbitrary folder" do
get "/yello/my-friend"
assert_equal "<p>So arbitrary!</p>\n", last_response.body
assert_equal "\n<p>So arbitrary!</p>\n", last_response.body
end

should "return HTML for a route localized in english" do
get "/localized"
assert_equal "<h1>Localized for en - en</h1>\n", last_response.body
assert_equal "\n<h1>Localized for en - en</h1>\n", last_response.body
end

should "return HTML for a route localized in italian" do
get "/it/localized"
assert_equal "<h1>Localized for it - it</h1>\n", last_response.body
assert_equal "\n<h1>Localized for it - it</h1>\n", last_response.body
end

should "return HTML for nested index RESTful route" do
get "/nested"
assert_equal "<h1>Nested Index</h1>\n", last_response.body
assert_equal "\n<h1>Nested Index</h1>\n", last_response.body
end

should "return HTML for nested item RESTful route" do
get "/nested/123-abc"
assert_equal "<h1>Nested Page with Slug: 123-abc</h1>\n", last_response.body
assert_equal "\n<h1>Nested Page with Slug: 123-abc</h1>\n", last_response.body
end

should "return JSON for a base route (no template)" do
Expand Down

0 comments on commit 9b89961

Please sign in to comment.