cite.rb 1.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module Jekyll
  class Scholar

    class CiteTag < Liquid::Tag
  
      attr_reader :key, :pages, :config
    
      def initialize(tag_name, arguments, tokens)
        super
        
        @config = Scholar.defaults.dup
        @key = arguments.strip.split(/\s+/)[0]
      end

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

        e = bibliography[key]
Sylvester Keil's avatar
Sylvester Keil committed
19

20
        if e
Sylvester Keil's avatar
Sylvester Keil committed
21
          c = CiteProc.process e.to_citeproc, :style => config['style'],
22
            :locale => config['locale'], :format => 'html', :mode => :citation
Sylvester Keil's avatar
Sylvester Keil committed
23
24
          
          "<a href='##{e.key}'>#{c}</a>"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        else
          "(missing reference)"
        end
      end
      
      private
      
      def bibliography
        @bibliography ||= BibTeX.open(extend_path(config['bibliography']), :filter => :latex)
      end          
      
      def extend_path(name)
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
    end
    
  end
end

Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)