Sha256: d41d00ebe6cd94c6224766eed820a1ba091bc87a5350d3117834c1d94a54cf31

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'uri'

module CiteProc
  module JS
    module Asset
      
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      attr_accessor :asset
      
      alias to_s asset

      def inspect
        to_s.inspect
      end
      
      module ClassMethods
        
        attr_accessor :root, :extension, :prefix
        
        def load(asset)
          instance = new
          case
          when File.exists?(asset)
            instance.asset = read(asset)
          when File.exists?(File.join(root.to_s, extend_name(asset)))
            instance.asset = read(File.join(root.to_s, extend_name(asset)))
          else
            instance.asset = asset
          end
          instance
        end
        
        private

        def read(name)
          io = open(name, 'r:UTF-8')
          io.read
        ensure
          io.close
        end

        def extend_name(file)
          file = File.extname(file).empty? ? [file, extension].compact.join : file
          file = file.start_with?(prefix.to_s) ? file : [prefix,file].join
          file
        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-'
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citeproc-js-0.0.2 lib/citeproc/js/assets.rb