utilities.rb 6.92 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
      def liquidify(entry)
        e = {}

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

176 177 178 179 180 181 182
        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
183 184 185 186 187 188 189 190 191

        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

192 193 194
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
195

196 197
      def details_file_for(entry)
        name = entry.key.to_s.dup
198

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

201 202
        [name, 'html'].join('.')
      end
203

204 205 206 207 208 209 210
      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
211
      def details_link_for(entry, base = base_url)
212
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
213
      end
214

Sylvester Keil's avatar
Sylvester Keil committed
215
      def base_url
216
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
217
      end
218

219 220 221
      def details_path
        config['details_dir']
      end
222

Sylvester Keil's avatar
Sylvester Keil committed
223
      def cite(key)
224 225 226
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
227
        if bibliography.key?(key)
228 229 230
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

234
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
235
        else
236
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
237
        end
238 239
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
240
      end
Sylvester Keil's avatar
Sylvester Keil committed
241

242
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
243
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
244
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
245
        else
246
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
247 248
        end
      end
249

Sylvester Keil's avatar
Sylvester Keil committed
250 251 252 253 254 255
      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
256

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

Sylvester Keil's avatar
Sylvester Keil committed
259 260 261 262 263 264
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
265

Sylvester Keil's avatar
Sylvester Keil committed
266 267 268
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
269

270 271 272 273
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
274
      def set_context_to(context)
275
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
276 277
        config.merge!(site.config['scholar'] || {})
      end
278
    end
279

280
  end
Hiren Patel's avatar
Hiren Patel committed
281
end