utilities.rb 7.11 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

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

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

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

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

50
      def bibtex_options
51 52 53 54 55
        config['bibtex_options'] ||= {}
      end

      def bibtex_filters
        config['bibtex_filters'] ||= []
56
      end
57

58 59 60
      def bibtex_path
        @bibtex_path ||= extend_path(bibtex_file)
      end
61

62
      def bibliography
63 64 65 66 67 68
        unless @bibliography
          @bibliography = BibTeX.open(bibtex_path, bibtex_options)
          @bibliography.replace_strings if replace_strings?
        end

        @bibliography
69 70
      end

71
      def entries
72
        b = bibliography[query || config['query']]
73 74

        unless config['sort_by'] == 'none'
75
          b = b.sort_by { |e| e[config['sort_by']].to_s }
76 77
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
78

79 80
        b
      end
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      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

102 103 104 105
      def replace_strings?
        config['replace_strings']
      end

106 107 108 109
      def cited_only?
        !!@cited
      end

110 111 112 113
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
114

115 116
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
117

118 119 120 121
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
122

123
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
124
        return missing_reference unless entry
125

126
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
127
        reference = CiteProc.process entry.to_citeproc,
128
          :style => style, :locale => config['locale'], :format => 'html'
129

Sylvester Keil's avatar
Sylvester Keil committed
130 131 132 133
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

134 135 136 137
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
138 139 140 141 142 143 144 145 146
      def missing_reference
        config['missing_reference']
      end

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

      def bibliography_template
147 148 149 150 151
        @bibliography_template || config['bibliography_template']
      end

      def liquid_template
        return @liquid_template if @liquid_template
152

153
        tmp = bibliography_template
154 155

        case
156
        when tmp.nil?, tmp.empty?
157 158 159 160 161
          tmp = '{{reference}}'
        when site.layouts.key?(tmp)
          tmp = site.layouts[tmp].content
        end

162
        @liquid_template = Liquid::Template.parse(tmp)
Sylvester Keil's avatar
Sylvester Keil committed
163 164
      end

165
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
166 167
        return missing_reference unless entry

168
        liquid_template.render({
Sylvester Keil's avatar
Sylvester Keil committed
169
          'entry' => liquidify(entry),
170 171 172 173 174 175
          'reference' => reference_tag(entry),
          'key' => entry.key,
          'type' => entry.type,
          'link' => repository_link_for(entry),
          'index' => index
        })
176 177
      end

Sylvester Keil's avatar
Sylvester Keil committed
178 179 180 181 182 183
      def liquidify(entry)
        e = {}

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

184 185 186 187 188 189 190
        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
191 192 193 194 195 196 197 198 199

        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

200 201 202
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
203

204 205
      def details_file_for(entry)
        name = entry.key.to_s.dup
206

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

209 210
        [name, 'html'].join('.')
      end
211

212 213 214 215 216 217 218
      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
219
      def details_link_for(entry, base = base_url)
220
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
221
      end
222

Sylvester Keil's avatar
Sylvester Keil committed
223
      def base_url
224
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
225
      end
226

227 228 229
      def details_path
        config['details_dir']
      end
230

Sylvester Keil's avatar
Sylvester Keil committed
231
      def cite(key)
232 233 234
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
235
        if bibliography.key?(key)
236 237 238
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

242
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
243
        else
244
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
245
        end
246 247
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
248
      end
Sylvester Keil's avatar
Sylvester Keil committed
249

250
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
251
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
252
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
253
        else
254
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
255 256
        end
      end
257

Sylvester Keil's avatar
Sylvester Keil committed
258 259 260 261 262 263
      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
264

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

Sylvester Keil's avatar
Sylvester Keil committed
267 268 269 270 271 272
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
273

Sylvester Keil's avatar
Sylvester Keil committed
274 275 276
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
277

278 279 280 281
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
282
      def set_context_to(context)
283
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
284 285
        config.merge!(site.config['scholar'] || {})
      end
286
    end
287

288
  end
Hiren Patel's avatar
Hiren Patel committed
289
end