bibliography.rb 1.48 KB
Newer Older
1
2
3
4
module Jekyll
  class Scholar

    class BibliographyTag < Liquid::Tag
5
      include Scholar::Utilities
6

7
8
      def initialize(tag_name, arguments, tokens)
        super
Sylvester Keil's avatar
Sylvester Keil committed
9

10
        @config = Scholar.defaults.dup
11
        @bibtex_file, @query = arguments.strip.split(/\s*filter:\s*/)
12
13
14
15
16
        
        if @bibtex_file == 'cited'
          @bibtex_file = nil
          @cited = true
        end
17
18
19
      end

      def render(context)
Sylvester Keil's avatar
Sylvester Keil committed
20
        set_context_to context
21

22
23
24
25
26
27
28
29
30
        references = entries

        if @cited
          references = cited_references.map do |key|
            references.detect { |e| e.key == key }
          end          
        end

        references.map! do |entry|
Sylvester Keil's avatar
Sylvester Keil committed
31
          reference = CiteProc.process entry.to_citeproc, :style => config['style'],
32
            :locale => config['locale'], :format => 'html'
33

Sylvester Keil's avatar
Sylvester Keil committed
34
          reference = content_tag :span, reference, :id => entry.key
35

36
          if generate_details?
37
38
            reference << link_to(details_link_for(entry),
              config['details_link'], :class => config['details_link_class'])
Sylvester Keil's avatar
Sylvester Keil committed
39
          end
40

Sylvester Keil's avatar
Sylvester Keil committed
41
          content_tag :li, reference
Sylvester Keil's avatar
Sylvester Keil committed
42
        end
Sylvester Keil's avatar
Sylvester Keil committed
43

44
        content_tag :ol, references.join("\n"), :class => config['bibliography_class']
45
      end
46
    end
47

48
    private
49

50
51
52
53
54
55
56
    def citeproc
      @citeproc ||= CiteProc::Processor.new do |p|
        p.style = config['style']
        p.format = 'html'
        p.locale = config['locale']
      end
    end
57
58
59
60
  end
end

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