module CiteProc module Asset def self.included(base) base.extend(ClassMethods) end attr_reader :asset, :location def open? !asset.nil? end def open(input) case when input.respond_to?(:read) @location = nil @asset = input.read when input.to_s =~ /^\s* e puts e.backtrace.join("\n") raise ArgumentError, "failed to open asset #{input.inspect}: #{e.message}" end def name File.basename(location, self.class.extension).sub(Regexp.new("^#{self.class.prefix}"), '') end alias to_s asset def inspect "#" end module ClassMethods attr_accessor :root, :extension, :prefix def open(path_or_name) new.open(path_or_name) end def extend_path(input) File.join(root.to_s, extend_name(input)) end def extend_name(input) name = File.extname(input).empty? ? [input, extension].compact.join : input.to_s.dup name = name.start_with?(prefix.to_s) ? name : [prefix, name].join name end end end class Style include Asset @root = '/usr/local/share/citation-style-language/styles'.freeze @extension = '.csl'.freeze end class Locale include Asset @root = '/usr/local/share/citation-style-language/locales'.freeze @extension = '.xml'.freeze @prefix = 'locales-'.freeze def language name.split('-')[0] end def region name.split('-')[1] end end end