Skip to content

Commit

Permalink
Make it work on Ruby 2.7 up to 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gettalong committed Jan 1, 2024
1 parent 4caa0dc commit f428b9b
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ EOF

s.required_ruby_version = '>= 2.6.0'

s.add_dependency('psych', '~> 5.1')
s.add_dependency('cmdparse', '~> 3.0', '>= 3.0.1')
s.add_dependency('systemu', '~> 2.5')
s.add_dependency('kramdown', '~> 2.3')
Expand Down
6 changes: 3 additions & 3 deletions lib/webgen/bundle_loader.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- encoding: utf-8 -*-

require 'rbconfig'
require 'yaml'
require 'webgen/error'
require 'webgen/utils'

module Webgen

Expand Down Expand Up @@ -59,7 +59,7 @@ def load_bundle_information #:nodoc:
if info_file.nil?
@infos[:bundles][bundle] = {}
else
infos = YAML.load(File.read(info_file))
infos = Utils.yaml_load(File.read(info_file))
[:extensions, :options].each do |type|
if data = infos.delete(type.to_s)
data.each do |name, hash|
Expand Down Expand Up @@ -134,7 +134,7 @@ def load_autoload_bundles

info_file = File.join(File.dirname(file), 'info.yaml')
next unless File.file?(info_file)
next unless (begin YAML.load(File.read(info_file))['autoload']; rescue Exception; false end)
next unless (begin Utils.yaml_load(File.read(info_file))['autoload']; rescue Exception; false end)

load(bundle_name)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/webgen/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'webgen/cli/commands/show'
require 'webgen/cli/commands/create'
require 'webgen/cli/commands/install'
require 'webgen/utils'

module Webgen

Expand Down Expand Up @@ -129,7 +130,7 @@ def initialize
opts.on("-o", "--option CONFIG_OPTION", String, "Specify a simple configuration option (key=value)") do |v|
k, v = v.split('=')
begin
@config_options[k.to_s] = YAML.load(v.to_s)
@config_options[k.to_s] = Utils.yaml_load(v.to_s)
rescue YAML::SyntaxError
raise ConfigurationOptionError.new("Couldn't parse value for '#{k}': #{$!}")
end
Expand Down
3 changes: 2 additions & 1 deletion lib/webgen/cli/commands/show_bundles.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-

require 'webgen/cli/utils'
require 'webgen/utils'

module Webgen
module CLI
Expand Down Expand Up @@ -32,7 +33,7 @@ def execute # :nodoc:
populate_hash = lambda do |file|
bundle_name = File.basename(File.dirname(file))
info_file = File.join(File.dirname(file), 'info.yaml')
bundles[bundle_name] ||= (File.file?(info_file) ? YAML.load(File.read(info_file)) : {})
bundles[bundle_name] ||= (File.file?(info_file) ? Utils.yaml_load(File.read(info_file)) : {})
bundles[bundle_name][:state] ||= :available
bundles[bundle_name]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/webgen/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'yaml'
require 'webgen/error'
require 'webgen/utils'

module Webgen

Expand Down Expand Up @@ -171,7 +172,7 @@ def set_values(values)
def load_from_file(filename)
data = if String === filename || filename.respond_to?(:read)
begin
YAML::load(String === filename ? File.read(filename) : filename.read) || {}
Utils.yaml_load(String === filename ? File.read(filename) : filename.read) || {}
rescue RuntimeError, ArgumentError, SyntaxError, YAML::SyntaxError => e
raise Error, "Problem parsing configuration data (it needs to contain a YAML hash): #{e.message}", e.backtrace
end
Expand Down
5 changes: 3 additions & 2 deletions lib/webgen/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'yaml'
require 'webgen/error'
require 'webgen/utils'

module Webgen

Expand Down Expand Up @@ -49,7 +50,7 @@ def parse_meta_info(mi_data, has_mi_start)
{}
else
begin
meta_info = YAML::load(mi_data.to_s)
meta_info = Utils.yaml_load(mi_data.to_s)
unless meta_info.kind_of?(Hash)
raise FormatError, "Invalid structure of meta information block: expected YAML hash but found #{meta_info.class}"
end
Expand Down Expand Up @@ -78,7 +79,7 @@ def parse_blocks(data, meta_info)
else
md = RE_BLOCKS_START_COMPLEX.match(options.to_s)
raise(FormatError, "Found invalid blocks starting line for block #{index}: #{options}") if content =~ /\A---/ || md.nil?
options = Hash[*md[1].to_s.scan(/(\w+):([^\s]*)/).map {|k,v| [k, (v == '' ? nil : YAML::load(v))]}.flatten]
options = Hash[*md[1].to_s.scan(/(\w+):([^\s]*)/).map {|k,v| [k, (v == '' ? nil : Utils.yaml_load(v))]}.flatten]
end

name = options.delete('name') || (index == 1 ? 'content' : 'block' + (index).to_s)
Expand Down
2 changes: 1 addition & 1 deletion lib/webgen/path_handler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def create_fragment_nodes_for_attributes(api_path, parent_node, klass)
# fragments.
def create_fragment_nodes_for_methods(api_path, klass_node, klass)
["Class", "Instance"].each do |type|
method_list = klass.send("#{type.downcase}_method_list")
method_list = type == 'Class' ? klass.class_method_list : klass.instance_methods
next if method_list.empty?
meth_url = "#{klass_node.alcn}##{type}-Methods"
path = Webgen::Path.new(meth_url,
Expand Down
3 changes: 2 additions & 1 deletion lib/webgen/path_handler/meta_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'yaml'
require 'webgen/path_handler/base'
require 'webgen/path_handler/page_utils'
require 'webgen/utils'

module Webgen
class PathHandler
Expand Down Expand Up @@ -44,7 +45,7 @@ def create_nodes(path, blocks)
# Add the data from the given page block to the hash.
def add_data(path, content, block_name)
entries = []
if content && (data = YAML::load(content))
if content && (data = Utils.yaml_load(content))
data.each do |(*keys), value|
value = Marshal.dump(value)
keys.each {|key| entries << [Webgen::Path.append(path.parent_path, key), value]}
Expand Down
3 changes: 2 additions & 1 deletion lib/webgen/path_handler/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'webgen/path'
require 'webgen/path_handler/base'
require 'webgen/path_handler/page_utils'
require 'webgen/utils'

module Webgen
class PathHandler
Expand Down Expand Up @@ -65,7 +66,7 @@ def create_nodes(path, blocks)
def read_entries(blocks)
blocks.each do |name, content|
begin
data = YAML::load(content)
data = Utils.yaml_load(content)
rescue RuntimeError, ArgumentError, SyntaxError, YAML::SyntaxError => e
raise RuntimeError, "Problem parsing block '#{name}' (it needs to contain a YAML hash): #{e.message}", e.backtrace
end
Expand Down
6 changes: 6 additions & 0 deletions lib/webgen/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rbconfig'
require 'webgen/core_ext'
require 'yaml'

module Webgen

Expand Down Expand Up @@ -34,6 +35,11 @@ def self.snake_case(str)
str
end

# Load the YAML string.
def self.yaml_load(str)
YAML.load(str, aliases: true, permitted_classes: [Symbol, Time])
end

end

end
3 changes: 2 additions & 1 deletion lib/webgen/utils/tag_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'yaml'
require 'strscan'
require 'webgen/error'
require 'webgen/utils'

module Webgen
module Utils
Expand Down Expand Up @@ -113,7 +114,7 @@ def replace_tags(content) #:yields: tag_name, params, body

# Parse the parameter string and return the result.
def parse_params(param_string, tag)
YAML::load("--- #{param_string}")
Utils.yaml_load("--- #{param_string}")
rescue ArgumentError, SyntaxError, YAML::SyntaxError => e
raise Error.new("Could not parse parameter string '#{param_string}' for tag '#{tag}': #{e.message}")
end
Expand Down
6 changes: 3 additions & 3 deletions test/webgen/test_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_all

def test_class_error_line
begin
eval("5 =")
eval("5 =", binding, "(eval)", 1)
rescue SyntaxError => e
assert_equal(1, Webgen::Error.error_line(e))
end
Expand All @@ -40,7 +40,7 @@ def test_class_error_line

def test_class_error_file
begin
eval("5 =")
eval("5 =", binding, "(eval)")
rescue SyntaxError => e
assert_equal('(eval)', Webgen::Error.error_file(e))
end
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_all
assert_match(/Error at location in <\/error:~5> while rendering <\/path>/, e.message)

begin
eval("5 =")
eval("5 =", binding, "(eval)", 1)
rescue SyntaxError
e = Webgen::RenderError.new($!, 'location', '/path', '/error')
assert_equal("/error", e.error_path)
Expand Down

0 comments on commit f428b9b

Please sign in to comment.