From 9b8996165f7eb57a572db9085b5609298d1bcdae Mon Sep 17 00:00:00 2001 From: Jared White Date: Fri, 3 May 2024 23:15:18 -0700 Subject: [PATCH] Adjust whitespace handling in front matter regexps, improve line numbers in errors --- .../bridgetown-core/front_matter/loaders/ruby.rb | 6 +++--- .../bridgetown-core/front_matter/loaders/yaml.rb | 4 ++-- .../lib/bridgetown-core/model/builder_origin.rb | 10 ++++------ .../lib/bridgetown-routes/code_blocks.rb | 2 +- .../lib/roda/plugins/bridgetown_routes.rb | 1 + bridgetown-routes/test/test_routes.rb | 14 +++++++------- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/bridgetown-core/lib/bridgetown-core/front_matter/loaders/ruby.rb b/bridgetown-core/lib/bridgetown-core/front_matter/loaders/ruby.rb index ea07efbc6..3ab6993be 100644 --- a/bridgetown-core/lib/bridgetown-core/front_matter/loaders/ruby.rb +++ b/bridgetown-core/lib/bridgetown-core/front_matter/loaders/ruby.rb @@ -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 # @@ -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( diff --git a/bridgetown-core/lib/bridgetown-core/front_matter/loaders/yaml.rb b/bridgetown-core/lib/bridgetown-core/front_matter/loaders/yaml.rb index 11d639fe8..26dc6bcf3 100644 --- a/bridgetown-core/lib/bridgetown-core/front_matter/loaders/yaml.rb +++ b/bridgetown-core/lib/bridgetown-core/front_matter/loaders/yaml.rb @@ -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 # diff --git a/bridgetown-core/lib/bridgetown-core/model/builder_origin.rb b/bridgetown-core/lib/bridgetown-core/model/builder_origin.rb index 8d5be5dd7..acfe71bf7 100644 --- a/bridgetown-core/lib/bridgetown-core/model/builder_origin.rb +++ b/bridgetown-core/lib/bridgetown-core/model/builder_origin.rb @@ -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(".", "::")) diff --git a/bridgetown-routes/lib/bridgetown-routes/code_blocks.rb b/bridgetown-routes/lib/bridgetown-routes/code_blocks.rb index 67e4a0ff5..2e4e4a70b 100644 --- a/bridgetown-routes/lib/bridgetown-routes/code_blocks.rb +++ b/bridgetown-routes/lib/bridgetown-routes/code_blocks.rb @@ -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 diff --git a/bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb b/bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb index d3f6d3563..223a96adf 100644 --- a/bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb +++ b/bridgetown-routes/lib/roda/plugins/bridgetown_routes.rb @@ -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 diff --git a/bridgetown-routes/test/test_routes.rb b/bridgetown-routes/test/test_routes.rb index cfaee70c3..58ee7f207 100644 --- a/bridgetown-routes/test/test_routes.rb +++ b/bridgetown-routes/test/test_routes.rb @@ -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 "

Dynamic Index

", last_response.body + assert_equal "\n

Dynamic Index

", last_response.body FileUtils.remove_file(index_file.sub("test_index.erb", "index.erb")) end @@ -38,32 +38,32 @@ def site should "return HTML for the howdy route" do get "/howdy?yo=joe&happy=pleased" - assert_equal "

joe 42

\n\n

I am pleasedpleased.

\n", last_response.body + assert_equal "\n

joe 42

\n\n

I am pleasedpleased.

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

So arbitrary!

\n", last_response.body + assert_equal "\n

So arbitrary!

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

Localized for en - en

\n", last_response.body + assert_equal "\n

Localized for en - en

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

Localized for it - it

\n", last_response.body + assert_equal "\n

Localized for it - it

\n", last_response.body end should "return HTML for nested index RESTful route" do get "/nested" - assert_equal "

Nested Index

\n", last_response.body + assert_equal "\n

Nested Index

\n", last_response.body end should "return HTML for nested item RESTful route" do get "/nested/123-abc" - assert_equal "

Nested Page with Slug: 123-abc

\n", last_response.body + assert_equal "\n

Nested Page with Slug: 123-abc

\n", last_response.body end should "return JSON for a base route (no template)" do