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

4
    # Utility methods used by several Scholar plugins. The methods in this
5
    # module may depend on the presence of #config, #bibtex_files, and
6 7
    # #site readers
    module Utilities
8

9
      attr_reader :config, :site, :query,
10 11 12 13 14 15 16 17 18 19 20
        :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|
21 22
            @bibtex_files ||= []
            @bibtex_files << file
23 24 25 26 27 28 29 30 31 32 33 34 35
          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
36 37 38

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

          opts.on('-T', '--template TEMPLATE') do |template|
            @bibliography_template = template
          end
44 45
        end

46
        argv = arguments.split(/(\B-[cfqptTs]|\B--(?:cited|file|query|prefix|text|style|template|))/)
47 48 49

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

51 52 53 54 55 56 57 58 59
      def bibtex_files
        @bibtex_files ||= [config['bibliography']]
      end

      # :nodoc: backwards compatibility
      def bibtex_file
        bibtex_files[0]
      end

60
      def bibtex_options
61 62 63 64 65
        config['bibtex_options'] ||= {}
      end

      def bibtex_filters
        config['bibtex_filters'] ||= []
66
      end
67

68 69 70 71 72 73 74
      def bibtex_paths
        @bibtex_paths ||= bibtex_files.map { |file|
          extend_path file
        }
      end

      # :nodoc: backwards compatibility
75
      def bibtex_path
76
        bibtex_paths[0]
77
      end
78

79
      def bibliography
80
        unless @bibliography
81 82 83 84
          @bibliography = BibTeX.parse(
            bibtex_paths.reduce('') { |s, p| s << IO.read(p) },
            bibtex_options
          )
85
          @bibliography.replace_strings if replace_strings?
86
          @bibliography.join if join_strings?
87 88 89
        end

        @bibliography
90 91
      end

92
      def entries
93
        b = bibliography[query || config['query']]
94 95

        unless config['sort_by'] == 'none'
96
          b = b.sort_by { |e| e[config['sort_by']].to_s }
97 98
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
99

100 101
        b
      end
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      def repository?
        !config['repository'].nil? && !config['repository'].empty?
      end

      def repository
        @repository ||= load_repository
      end

      def load_repository
        return {} unless repository?

        Hash[Dir[File.join(repository_path, '**/*.{pdf,ps}')].map { |path|
          [File.basename(path).sub(/\.(pdf|ps)$/, ''), path]
        }]
      end

      def repository_path
        config['repository']
      end

123 124 125 126
      def replace_strings?
        config['replace_strings']
      end

127 128 129 130
      def join_strings?
        config['join_strings']
      end

131 132 133 134
      def cited_only?
        !!@cited
      end

135 136
      def extend_path(name)
        if name.nil? || name.empty?
137
          name = config['bibliography']
138
        end
139

140 141
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
142

143 144 145
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
146
      end
147

148
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
149
        return missing_reference unless entry
150

151
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
152
        reference = CiteProc.process entry.to_citeproc,
153
          :style => style, :locale => config['locale'], :format => 'html'
154

Sylvester Keil's avatar
Sylvester Keil committed
155 156 157 158
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

159 160 161 162
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
163 164 165 166 167 168 169 170 171
      def missing_reference
        config['missing_reference']
      end

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

      def bibliography_template
172 173 174 175 176
        @bibliography_template || config['bibliography_template']
      end

      def liquid_template
        return @liquid_template if @liquid_template
177

178
        tmp = bibliography_template
179 180

        case
181
        when tmp.nil?, tmp.empty?
182 183 184 185 186
          tmp = '{{reference}}'
        when site.layouts.key?(tmp)
          tmp = site.layouts[tmp].content
        end

187
        @liquid_template = Liquid::Template.parse(tmp)
Sylvester Keil's avatar
Sylvester Keil committed
188 189
      end

190
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
191 192
        return missing_reference unless entry

193
        liquid_template.render({
Sylvester Keil's avatar
Sylvester Keil committed
194
          'entry' => liquidify(entry),
195 196 197 198 199 200
          'reference' => reference_tag(entry),
          'key' => entry.key,
          'type' => entry.type,
          'link' => repository_link_for(entry),
          'index' => index
        })
201 202
      end

Sylvester Keil's avatar
Sylvester Keil committed
203 204 205 206 207 208
      def liquidify(entry)
        e = {}

        e['key'] = entry.key
        e['type'] = entry.type

209 210 211 212 213 214 215
        if entry.field?(:abstract)
          tmp = entry.dup
          tmp.delete :abstract
          e['bibtex'] = tmp.to_s
        else
          e['bibtex'] = entry.to_s
        end
Sylvester Keil's avatar
Sylvester Keil committed
216 217 218 219 220 221 222 223 224

        entry.fields.each do |key, value|
          value = value.convert(*bibtex_filters) unless bibtex_filters.empty?
          e[key.to_s] = value.to_s
        end

        e
      end

225 226 227
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
228

229 230
      def details_file_for(entry)
        name = entry.key.to_s.dup
231

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

234 235
        [name, 'html'].join('.')
      end
236

237 238 239 240 241 242 243
      def repository_link_for(entry, base = base_url)
        url = repository[entry.key]
        return unless url

        File.join(base, url)
      end

Sylvester Keil's avatar
Sylvester Keil committed
244
      def details_link_for(entry, base = base_url)
245
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
246
      end
247

Sylvester Keil's avatar
Sylvester Keil committed
248
      def base_url
249
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
250
      end
251

252 253 254
      def details_path
        config['details_dir']
      end
255

Sylvester Keil's avatar
Sylvester Keil committed
256
      def cite(key)
257 258 259
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
260
        if bibliography.key?(key)
261 262 263
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

267
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
268
        else
269
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
270
        end
271 272
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
273
      end
Sylvester Keil's avatar
Sylvester Keil committed
274

275
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
276
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
277
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
278
        else
279
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
280 281
        end
      end
282

Sylvester Keil's avatar
Sylvester Keil committed
283 284 285 286 287 288
      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
289

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

Sylvester Keil's avatar
Sylvester Keil committed
292 293 294 295 296 297
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
298

Sylvester Keil's avatar
Sylvester Keil committed
299 300 301
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
302

303 304 305 306
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
307
      def set_context_to(context)
308
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
309 310
        config.merge!(site.config['scholar'] || {})
      end
311
    end
312

313
  end
Hiren Patel's avatar
Hiren Patel committed
314
end