utilities.rb 6.77 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
        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
      def bibliography
59 60 61 62 63 64
        unless @bibliography
          @bibliography = BibTeX.open(bibtex_path, bibtex_options)
          @bibliography.replace_strings if replace_strings?
        end

        @bibliography
65 66
      end

67
      def entries
68
        b = bibliography[query || config['query']]
69 70

        unless config['sort_by'] == 'none'
71
          b = b.sort_by { |e| e[config['sort_by']].to_s }
72 73
          b.reverse! if config['order'] =~ /^(desc|reverse)/i
        end
74

75 76
        b
      end
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      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

98 99 100 101
      def replace_strings?
        config['replace_strings']
      end

102 103 104 105
      def cited_only?
        !!@cited
      end

106 107 108 109
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
110

111 112
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
113

114 115 116 117
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
118

119
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
120
        return missing_reference unless entry
121

122
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
123
        reference = CiteProc.process entry.to_citeproc,
124
          :style => style, :locale => config['locale'], :format => 'html'
125

Sylvester Keil's avatar
Sylvester Keil committed
126 127 128 129
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

130 131 132 133
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
134 135 136 137 138 139 140 141 142
      def missing_reference
        config['missing_reference']
      end

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

      def bibliography_template
143 144 145 146 147 148 149 150 151 152 153 154
        return @bibliography_template if @bibliography_template

        tmp = config['bibliography_template'] || '{{reference}}'

        case
        when tmp.nil?
          tmp = '{{reference}}'
        when site.layouts.key?(tmp)
          tmp = site.layouts[tmp].content
        end

        @bibliography_template = Liquid::Template.parse(tmp)
Sylvester Keil's avatar
Sylvester Keil committed
155 156
      end

157
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
158 159
        return missing_reference unless entry

160
        bibliography_template.render({
Sylvester Keil's avatar
Sylvester Keil committed
161
          'entry' => liquidify(entry),
162 163 164 165 166 167
          'reference' => reference_tag(entry),
          'key' => entry.key,
          'type' => entry.type,
          'link' => repository_link_for(entry),
          'index' => index
        })
168 169
      end

Sylvester Keil's avatar
Sylvester Keil committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      def liquidify(entry)
        e = {}

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

        e['bibtex'] = entry.to_s

        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

186 187 188
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
189

190 191
      def details_file_for(entry)
        name = entry.key.to_s.dup
192

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

195 196
        [name, 'html'].join('.')
      end
197

198 199 200 201 202 203 204
      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
205
      def details_link_for(entry, base = base_url)
206
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
207
      end
208

Sylvester Keil's avatar
Sylvester Keil committed
209
      def base_url
210
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
211
      end
212

213 214 215
      def details_path
        config['details_dir']
      end
216

Sylvester Keil's avatar
Sylvester Keil committed
217
      def cite(key)
218 219 220
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
221
        if bibliography.key?(key)
222 223 224
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

228
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
229
        else
230
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
231
        end
232 233
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
234
      end
Sylvester Keil's avatar
Sylvester Keil committed
235

236
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
237
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
238
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
239
        else
240
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
241 242
        end
      end
243

Sylvester Keil's avatar
Sylvester Keil committed
244 245 246 247 248 249
      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
250

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

Sylvester Keil's avatar
Sylvester Keil committed
253 254 255 256 257 258
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
259

Sylvester Keil's avatar
Sylvester Keil committed
260 261 262
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
263

264 265 266 267
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
268
      def set_context_to(context)
269
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
270 271
        config.merge!(site.config['scholar'] || {})
      end
272
    end
273

274
  end
Hiren Patel's avatar
Hiren Patel committed
275
end