From e35ec1a9f3bda30818be106c8e0b70770f35740b Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 25 Oct 2023 16:04:10 +0200 Subject: [PATCH] Use YAML to parse the front matter YAML is much better at understanding the boundaries of an embedded YAML document. --- lib/erb/formatter.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/erb/formatter.rb b/lib/erb/formatter.rb index b4d49c4..be01425 100644 --- a/lib/erb/formatter.rb +++ b/lib/erb/formatter.rb @@ -1,11 +1,11 @@ # frozen_string_literal: false require 'pp' -require "erb" -require "ripper" -require 'securerandom' +require 'erb' +require 'yaml' require 'strscan' require 'stringio' +require 'securerandom' require 'erb/formatter/version' require 'syntax_tree' @@ -104,10 +104,13 @@ def initialize(source, line_width: 80, filename: nil, debug: $DEBUG) end def remove_front_matter(source) - source.sub(/\A---\n[\s\S]*?\n---\n/) do |match| - @front_matter = match - match.gsub(/[^\n]/, ' ') - end + return source unless source.start_with?("---\n") + + first_body_line = YAML.parse(source).children.first.end_line + 1 + lines = source.lines + + @front_matter = lines[0...first_body_line].join + lines[first_body_line..].join end attr_accessor \