Sha256: 7e01667e0cbc2e34f0bcfb535201d588d9cb1cf2041f22d862071976044f2be1

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 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.1 lib/citeproc/js/assets.rb