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

Ruby 3.1対応・互換性保持YAMLローダの導入 #85

Merged
merged 1 commit into from
Mar 7, 2022
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
26 changes: 23 additions & 3 deletions articles/lib/tasks/z01_pandoc2review.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 Kenshi Muto
# Copyright (c) 2020-2022 Kenshi Muto
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +20,7 @@

require 'fileutils'
require 'yaml'
require 'date'

def make_mdre(ch, p2r, path)
if File.exist?(ch) # re file
Expand All @@ -29,9 +30,28 @@ def make_mdre(ch, p2r, path)
end
end

def yaml_load_file_compatible(file)
if YAML.respond_to?(:safe_load_file)
YAML.safe_load_file(file, aliases: true, permitted_classes: [Date])
else
File.open(file, 'rt:bom|utf-8') do |f|
begin
# < Ruby 3.1
YAML.safe_load(f, filename: file, aliases: true, permitted_classes: [Date])
rescue ArgumentError
# < Ruby 2.7
YAML.safe_load(f, [Date])
rescue Psych::DisallowedClass
# < Ruby 2.5
YAML.safe_load(File.read(file), [Date])
end
end
end
end

desc 'run pandoc2review'
task :pandoc2review do
config = YAML.load_file('config.yml')
config = yaml_load_file_compatible('config.yml')
if config['contentdir'] == '_refiles'
path = '_refiles'
p2r = 'pandoc2review'
Expand All @@ -41,7 +61,7 @@ task :pandoc2review do
File.write("#{path}/THIS_FOLDER_IS_TEMPORARY", '')
end

catalog = YAML.load_file('catalog.yml')
catalog = yaml_load_file_compatible('catalog.yml')
%w(PREDEF CHAPS APPENDIX POSTDEF).each do |block|
if catalog[block].kind_of?(Array)
catalog[block].each do |ch|
Expand Down