lib/csl/loader.rb in csl-1.2.0 vs lib/csl/loader.rb in csl-1.2.1

- old
+ new

@@ -16,49 +16,21 @@ # Style.load('chicago-author.csl') -> style # Locale.load('en') -> locale # Locale.load('http://example.com/de.xml') -> locale # # Resolves the passed-in path/name or string and loads the asset data. - # The data will be passed on to the #parse method of the base class. + # The data will be passed on to the #parse! method of the base class. # Typically, this will return a new instance of the class. # # @note - # The base class is expected to define a #parse method. + # The base class is expected to define a #parse! method. # # @raise ParseError # # @return [Style, Locale] the parsed CSL resource def load(input) - case - when input.respond_to?(:read) - data = input.read - when input.to_s =~ /^\s*</ - data = input.to_s - else - - input = input.to_s - - case - when File.exists?(input) - location = input - when File.exists?(extend_name(input)) - location = extend_name(input) - when File.exists?(extend_path(input)) - location = extend_path(input) - else - location = input - end - - Kernel.open(location, 'r:UTF-8') do |io| - data = io.read - end - end - - parse(data) - - rescue => e - raise ParseError, "failed to load #{input.inspect}: #{e.message}" + parse! extract_data_from(input) end def list Dir["#{root}/#{prefix}*#{extension}"].map do |path| File.basename(path, extension).sub(/^#{prefix}/, '') @@ -83,9 +55,40 @@ unless name.start_with?(prefix.to_s) name = [prefix, name].join end name + end + + private + + def extract_data_from(input) + case + when input.respond_to?(:read) + input.read + when input.to_s =~ /^\s*</ + input.to_s + else + + input = input.to_s + + case + when File.exists?(input) + location = input + when File.exists?(extend_name(input)) + location = extend_name(input) + when File.exists?(extend_path(input)) + location = extend_path(input) + else + location = input + end + + Kernel.open(location, 'r:UTF-8') do |io| + io.read + end + end + rescue => e + raise ParseError, "failed to extract CSL data from #{input.inspect}: #{e.message}" end end end