utilities.rb 5.29 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 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
      attr_reader :bibtex_file, :config, :site, :query,
        :context, :prefix, :key, :text

      def optparse(arguments)
        return if arguments.nil? || arguments.empty?

        parser = OptionParser.new do |opts|
          opts.on('-c', '--cited') do |cited|
            @cited = true
          end

          opts.on('-f', '--file FILE') do |file|
            @bibtex_file = file
          end

          opts.on('-q', '--query QUERY') do |query|
            @query = query
          end

          opts.on('-p', '--prefix PREFIX') do |prefix|
            @prefix = prefix
          end

          opts.on('-t', '--text TEXT') do |text|
            @text = text
          end
35 36 37 38

          opts.on('-s', '--style STYLE') do |style|
            @style = style
          end 
39 40
        end

41
        argv = arguments.split(/(\B-[cfqpts]|\B--(?:cited|file|query|prefix|text|style|))/)
42 43 44

        parser.parse argv.map(&:strip).reject(&:empty?)
      end
45

46
      def bibtex_options
47 48 49 50 51
        config['bibtex_options'] ||= {}
      end

      def bibtex_filters
        config['bibtex_filters'] ||= []
52
      end
53

54 55 56
      def bibtex_path
        @bibtex_path ||= extend_path(bibtex_file)
      end
57

58 59
      def bibliography
        @bibliography ||= BibTeX.open(bibtex_path, bibtex_options)
60 61
      end

62
      def entries
63
        b = bibliography[query || config['query']]
64 65

        unless config['sort_by'] == 'none'
66
          b = b.sort_by { |e| e[config['sort_by']].to_s }
67 68
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
69

70 71
        b
      end
72

73 74 75 76
      def cited_only?
        !!@cited
      end

77 78 79 80
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
81

82 83
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
84

85 86 87 88
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
89

90
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
91
        return missing_reference unless entry
92

93
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
94
        reference = CiteProc.process entry.to_citeproc,
95
          :style => style, :locale => config['locale'], :format => 'html'
96

Sylvester Keil's avatar
Sylvester Keil committed
97 98 99 100
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

101 102 103 104
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
105 106 107 108 109 110 111 112 113 114 115 116
      def missing_reference
        config['missing_reference']
      end

      def reference_tagname
        config['reference_tagname'] || :span
      end

      def bibliography_template
        config['bibliography_template'] || '%{reference}'
      end

117
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
118 119 120 121
        return missing_reference unless entry

        bibliography_template % {
          :reference => reference_tag(entry),
122
          :key => entry.key,
123
          :type => entry.type,
124
          :index => index
Sylvester Keil's avatar
Sylvester Keil committed
125
        }
126 127
      end

128 129 130
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
131

132 133
      def details_file_for(entry)
        name = entry.key.to_s.dup
134

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

137 138
        [name, 'html'].join('.')
      end
139

Sylvester Keil's avatar
Sylvester Keil committed
140
      def details_link_for(entry, base = base_url)
141
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
142
      end
143

Sylvester Keil's avatar
Sylvester Keil committed
144
      def base_url
145
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
146
      end
147

148 149 150
      def details_path
        config['details_dir']
      end
151

Sylvester Keil's avatar
Sylvester Keil committed
152
      def cite(key)
153 154 155
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
156
        if bibliography.key?(key)
157 158 159
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

Sylvester Keil's avatar
Sylvester Keil committed
160 161
          citation = CiteProc.process entry.to_citeproc, :style => config['style'],
            :locale => config['locale'], :format => 'html', :mode => :citation
162

163
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
164
        else
165
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
166
        end
167 168
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
169
      end
Sylvester Keil's avatar
Sylvester Keil committed
170

171
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
172
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
173
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
174
        else
175
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
176 177
        end
      end
178

Sylvester Keil's avatar
Sylvester Keil committed
179 180 181 182 183 184
      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
185

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

Sylvester Keil's avatar
Sylvester Keil committed
188 189 190 191 192 193
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
194

Sylvester Keil's avatar
Sylvester Keil committed
195 196 197
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
198

199 200 201 202
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
203
      def set_context_to(context)
204
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
205 206
        config.merge!(site.config['scholar'] || {})
      end
207
    end
208

209
  end
Hiren Patel's avatar
Hiren Patel committed
210
end