utilities.rb 2.39 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
module Jekyll
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  class Scholar
    
    # Utility methods used by several Scholar plugins. The methods in this
    # module may depend on the presence of #config, #bibtex_file, and
    # #site readers
    module Utilities
      
      attr_reader :bibtex_file, :config, :site
      
      def bibtex_options
        @bibtex_options ||= { :filter => :latex }
      end
      
      def bibtex_path
        @bibtex_path ||= extend_path(bibtex_file)
      end
      
      def bibliography
        @bibliography ||= BibTeX.open(bibtex_path, bibtex_options)
      end          
      
      def entries
        b = bibliography[config['query']]

        unless config['sort_by'] == 'none'
          b.sort_by! { |e| e[config['sort_by']].to_s }
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
        
        b
      end
      
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
        
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
      
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
      
      def details_file_for(entry)
        name = entry.key.to_s.dup
        
        name.gsub!(/[:\s]+/, '_')
        
        [name, 'html'].join('.')
      end
      
Sylvester Keil's avatar
Sylvester Keil committed
56 57
      def details_link_for(entry, base = site.config['baseurl'] || '/')
        [base, details_path, details_file_for(entry)].compact.join('/')
58 59 60 61 62 63
      end
      
      def details_path
        config['details_dir']
      end
      
Sylvester Keil's avatar
Sylvester Keil committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      def content_tag(name, content_or_attributes, attributes = {})
        if content_or_attributes.is_a?(Hash)
          content, attributes = nil, content_or_attributes
        else
          content = content_or_attributes
        end
        
        attributes = attributes.map { |k,v| %Q(#{k}="#{v}") }
        
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
      
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
      
      def set_context_to(context)
        @site = context.registers[:site]
        config.merge!(site.config['scholar'] || {})
      end
      
89 90 91
    end
    
  end
Sylvester Keil's avatar
Sylvester Keil committed
92
end