utilities.rb 2.94 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
module Jekyll
2
  class Scholar
3

4
5
6
7
    # 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
8

9
      attr_reader :bibtex_file, :config, :site
10

11
12
13
      def bibtex_options
        @bibtex_options ||= { :filter => :latex }
      end
14

15
16
17
      def bibtex_path
        @bibtex_path ||= extend_path(bibtex_file)
      end
18

19
20
      def bibliography
        @bibliography ||= BibTeX.open(bibtex_path, bibtex_options)
21
22
      end

23
24
25
26
      def entries
        b = bibliography[config['query']]

        unless config['sort_by'] == 'none'
27
          b = b.sort_by { |e| e[config['sort_by']].to_s }
28
29
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
30

31
32
        b
      end
33

34
35
36
37
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
38

39
40
41
42
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
43

44
45
46
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
47

48
49
      def details_file_for(entry)
        name = entry.key.to_s.dup
50

51
        name.gsub!(/[:\s]+/, '_')
52

53
54
        [name, 'html'].join('.')
      end
55

Sylvester Keil's avatar
Sylvester Keil committed
56
      def details_link_for(entry, base = base_url)
57
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
58
      end
59

Sylvester Keil's avatar
Sylvester Keil committed
60
      def base_url
61
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
62
      end
63

64
65
66
      def details_path
        config['details_dir']
      end
67

Sylvester Keil's avatar
Sylvester Keil committed
68
69
70
71
72
73
      def cite(key)
        entry = bibliography[key]

        if bibliography.key?(key)
          citation = CiteProc.process entry.to_citeproc, :style => config['style'],
            :locale => config['locale'], :format => 'html', :mode => :citation
74

Sylvester Keil's avatar
Sylvester Keil committed
75
76
77
78
          link_to "##{entry.key}", citation.join
        else
          "(missing reference)"
        end
79
80
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
81
      end
Hiren Patel's avatar
Hiren Patel committed
82
      
83
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
84
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
85
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
86
87
        else
          "(missing reference)"
Hiren Patel's avatar
Hiren Patel committed
88
89
        end
      end
90

Sylvester Keil's avatar
Sylvester Keil committed
91
92
93
94
95
96
      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
97

Sylvester Keil's avatar
Sylvester Keil committed
98
        attributes = attributes.map { |k,v| %Q(#{k}="#{v}") }
99

Sylvester Keil's avatar
Sylvester Keil committed
100
101
102
103
104
105
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
106

Sylvester Keil's avatar
Sylvester Keil committed
107
108
109
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
110

Sylvester Keil's avatar
Sylvester Keil committed
111
112
113
114
      def set_context_to(context)
        @site = context.registers[:site]
        config.merge!(site.config['scholar'] || {})
      end
115

116
    end
117

118
  end
Hiren Patel's avatar
Hiren Patel committed
119
end