Workaround for ruby <3

This commit is contained in:
Hugo Peixoto 2024-01-01 16:29:12 +00:00
parent d597878342
commit a59f80ddca
1 changed files with 10 additions and 1 deletions

View File

@ -3,13 +3,22 @@
require 'yaml'
require 'date'
def yaml_load(text)
# not sure if 3.0.0 is the right threshold, but it works
if (RUBY_VERSION.split(".").map(&:to_i) <=> [3,0,0]) < 0
YAML.load(text)
else
YAML.load(text, permitted_classes: [Symbol, Date, Time])
end
end
class YAMLFrontMatter
PATTERN = /\A(---\r?\n(.*?)\n?^---\s*$\n?)/m.freeze
class << self
def extract(content)
if content =~ PATTERN
[YAML.load(Regexp.last_match(2), permitted_classes: [Symbol, Date, Time]), content.sub(Regexp.last_match(1), "")]
[yaml_load(Regexp.last_match(2)), content.sub(Regexp.last_match(1), "")]
else
[{}, content]
end