Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use YAML to parse the front matter #31

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 \
Expand Down
Loading