utilities.rb 7.48 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 86 87 88
          @bibliography.replace_strings if replace_strings?
        end

        @bibliography
89 90
      end

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

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

99 100
        b
      end
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      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

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

126 127 128 129
      def cited_only?
        !!@cited
      end

130 131
      def extend_path(name)
        if name.nil? || name.empty?
132
          name = config['bibliography']
133
        end
134

135 136
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
137

138 139 140
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
141
      end
142

143
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
144
        return missing_reference unless entry
145

146
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
147
        reference = CiteProc.process entry.to_citeproc,
148
          :style => style, :locale => config['locale'], :format => 'html'
149

Sylvester Keil's avatar
Sylvester Keil committed
150 151 152 153
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

154 155 156 157
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
158 159 160 161 162 163 164 165 166
      def missing_reference
        config['missing_reference']
      end

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

      def bibliography_template
167 168 169 170 171
        @bibliography_template || config['bibliography_template']
      end

      def liquid_template
        return @liquid_template if @liquid_template
172

173
        tmp = bibliography_template
174 175

        case
176
        when tmp.nil?, tmp.empty?
177 178 179 180 181
          tmp = '{{reference}}'
        when site.layouts.key?(tmp)
          tmp = site.layouts[tmp].content
        end

182
        @liquid_template = Liquid::Template.parse(tmp)
Sylvester Keil's avatar
Sylvester Keil committed
183 184
      end

185
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
186 187
        return missing_reference unless entry

188
        liquid_template.render({
Sylvester Keil's avatar
Sylvester Keil committed
189
          'entry' => liquidify(entry),
190 191 192 193 194 195
          'reference' => reference_tag(entry),
          'key' => entry.key,
          'type' => entry.type,
          'link' => repository_link_for(entry),
          'index' => index
        })
196 197
      end

Sylvester Keil's avatar
Sylvester Keil committed
198 199 200 201 202 203
      def liquidify(entry)
        e = {}

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

204 205 206 207 208 209 210
        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
211 212 213 214 215 216 217 218 219

        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

220 221 222
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
223

224 225
      def details_file_for(entry)
        name = entry.key.to_s.dup
226

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

229 230
        [name, 'html'].join('.')
      end
231

232 233 234 235 236 237 238
      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
239
      def details_link_for(entry, base = base_url)
240
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
241
      end
242

Sylvester Keil's avatar
Sylvester Keil committed
243
      def base_url
244
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
245
      end
246

247 248 249
      def details_path
        config['details_dir']
      end
250

Sylvester Keil's avatar
Sylvester Keil committed
251
      def cite(key)
252 253 254
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
255
        if bibliography.key?(key)
256 257 258
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

262
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
263
        else
264
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
265 266
        end
      end
Sylvester Keil's avatar
Sylvester Keil committed
267

268
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
269
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
270
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
271
        else
272
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
273 274
        end
      end
275

Sylvester Keil's avatar
Sylvester Keil committed
276 277 278 279 280 281
      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
282

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

Sylvester Keil's avatar
Sylvester Keil committed
285 286 287 288 289 290
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
291

Sylvester Keil's avatar
Sylvester Keil committed
292 293 294
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
295

296 297 298 299
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
300
      def set_context_to(context)
301
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
302 303
        config.merge!(site.config['scholar'] || {})
      end
304
    end
305

306
  end
Hiren Patel's avatar
Hiren Patel committed
307
end