Skip to content

Commit

Permalink
replace YAMLLoaderへの置き換え
Browse files Browse the repository at this point in the history
use YAMLLoader to load config.yml
  • Loading branch information
takahashim committed Apr 8, 2016
2 parents 2bbedaa + aa78b31 commit 3066673
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 46 deletions.
8 changes: 3 additions & 5 deletions bin/review-compile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ bindir = Pathname.new(__FILE__).realpath.dirname
$LOAD_PATH.unshift((bindir + '../lib').realpath)

require 'review'
require 'review/compiler'
require 'review/book'
require 'review/i18n'
require 'fileutils'
require 'optparse'
require 'yaml'
Expand Down Expand Up @@ -137,11 +134,12 @@ def _main
end

begin
loader = ReVIEW::YAMLLoader.new
if config["yaml"]
config = config.merge(ReVIEW::MakerHelper.recursive_load_yaml(config["yaml"]))
config.deep_merge!(loader.load_file(config["yaml"]))
else
if File.exist?(DEFAULT_CONFIG_FILENAME)
config = config.merge(YAML.load_file(DEFAULT_CONFIG_FILENAME))
config.deep_merge!(loader.load_file(DEFAULT_CONFIG_FILENAME))
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/epubmaker/producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

require 'tmpdir'
require 'fileutils'
require 'review/makerhelper'
require 'review/yamlloader'
require 'securerandom'
require 'epubmaker/content'
require 'epubmaker/epubv2'
require 'epubmaker/epubv3'
require 'review/i18n'
require 'review/extentions/hash'

module EPUBMaker
# EPUBMaker produces EPUB file.
Expand All @@ -30,13 +31,15 @@ class Producer
# Take YAML +file+ and return parameter hash.
def Producer.load(file)
raise "Can't open #{file}." if file.nil? || !File.exist?(file)
return ReVIEW::MakerHelper.recursive_load_yaml(file)
loader = ReVIEW::YAMLLoader.new
loader.load_file(file)
end

# Take YAML +file+ and update parameter hash.
def load(file)
raise "Can't open #{file}." if file.nil? || !File.exist?(file)
merge_params(@params.merge(ReVIEW::MakerHelper.recursive_load_yaml(file)))
loader = ReVIEW::YAMLLoader.new
merge_params(@params.deep_merge(loader.load_file(file)))
end

# Construct producer object.
Expand Down
7 changes: 2 additions & 5 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
require 'rexml/document'
require 'rexml/streamlistener'
require 'epubmaker'
require 'review/htmltoc'
require 'review/converter'
require 'review/htmlbuilder'
require 'review/makerhelper'

module ReVIEW
class EPUBMaker
Expand All @@ -32,7 +28,8 @@ def log(s)
end

def load_yaml(yamlfile)
@params = ReVIEW::Configure.values.merge(ReVIEW::MakerHelper.recursive_load_yaml(yamlfile)) # FIXME:設定がRe:VIEW側とepubmaker/producer.rb側の2つに分かれて面倒
loader = YAMLLoader.new
@params = ReVIEW::Configure.values.deep_merge(loader.load_file(yamlfile))
@producer = Producer.new(@params)
@producer.load(yamlfile)
@params = @producer.params
Expand Down
26 changes: 0 additions & 26 deletions lib/review/makerhelper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,5 @@ def self.copy_images_to_dir(from_dir, to_dir, options = {})

image_files
end

def self.recursive_load_yaml(yamlfile)
__recursive_load_yaml({}, yamlfile, {})[0]
end

class << self
private
def __recursive_load_yaml(yaml, yamlfile, loaded_yaml={})
_yaml = YAML.load_file(yamlfile)
yaml = _yaml.merge(yaml)
if yaml['inherit']
inheritfile = yaml['inherit']

# Check loop
if loaded_yaml[inheritfile]
raise "Found cyclic YAML inheritance '#{inheritfile}' in #{yamlfile}."
else
loaded_yaml[inheritfile] = true
end

yaml.delete('inherit')
yaml, loaded_yaml = __recursive_load_yaml(yaml, inheritfile, loaded_yaml)
end
return yaml, loaded_yaml
end
end
end
end
4 changes: 2 additions & 2 deletions lib/review/pdfmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def parse_opts(args)
def execute(*args)
@config = ReVIEW::Configure.values
cmd_config, yamlfile = parse_opts(args)

@config.merge!(ReVIEW::MakerHelper.recursive_load_yaml(yamlfile))
loader = YAMLLoader.new
@config.deep_merge!(loader.load_file(yamlfile))
# YAML configs will be overridden by command line options.
@config.merge!(cmd_config)
I18n.setup(@config["language"])
Expand Down
6 changes: 3 additions & 3 deletions lib/review/yamlloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def load_file(yamlfile)
end

inherit_file = File.expand_path(yaml['inherit'], File.dirname(yamlfile))

# Check loop
if loaded_files[inherit_file]
raise "Found cyclic YAML inheritance '#{inherit_file}' in #{yamlfile}."
raise "Found circular YAML inheritance '#{inherit_file}' in #{yamlfile}."
end

loaded_files[inherit_file] = true
yaml.delete('inherit')
current_file = inherit_file
Expand Down
7 changes: 5 additions & 2 deletions test/test_yamlloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_load_file_inherit
k0: 2
k1:
name: "test"
array: [{name: "N", val: "V"}]
inherit: "test2.yml"
EOB
end
Expand All @@ -40,12 +41,14 @@ def test_load_file_inherit
k1:
name: "test2"
name2: "value2"
array: [{name: "shoudoverridden_name", val: "shouldoverridden_val"}]
k2: "3"
EOB
end
yaml = @loader.load_file(yaml_file)
assert_equal({"k0"=>2,
"k1" => {"name"=>"test", "name2"=>"value2"},
assert_equal({"k0" => 2,
"k1" => {"name"=>"test", "name2"=>"value2",
"array" => [{"name" => "N", "val" => "V"}]},
"k2" => "3"},
yaml)
end
Expand Down

0 comments on commit 3066673

Please sign in to comment.