bibliography.rb 1.26 KB
Newer Older
1 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
module Jekyll
  class Scholar

    class BibliographyTag < Liquid::Tag
  
      attr_reader :file, :config
    
      def initialize(tag_name, arguments, tokens)
        super
        
        @config = Scholar.defaults.dup
        @file = arguments.strip
      end

      def render(context)
        config.merge!(context.registers[:site].config['scholar'] || {})

        entries.map { |e|
          CiteProc.process e.to_citeproc, :style => config['style'],
            :locale => config['locale'], :format => 'html'
        }.join("\n")
      end
      
      private
      
      def bibliography
        @bibliography ||= BibTeX.open(extend_path(file), :filter => :latex)
      end          
      
      def entries
        b = bibliography['@*']

        unless config['sort_by'] == 'none'
          b.sort_by! { |e| e[config['sort_by']] }
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
        
        b
      end
      
      def extend_path(name)
Sylvester Keil's avatar
Sylvester Keil committed
42 43 44 45
        if name.nil? || name.empty?
          name = config['bibliography']
        end
        
46 47 48 49 50 51 52 53 54 55
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
    end
    
  end
end

Liquid::Template.register_tag('bibliography', Jekyll::Scholar::BibliographyTag)