utilities.rb 5.47 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 38

          opts.on('-s', '--style STYLE') do |style|
            @style = style
          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
      def replace_strings?
        config['replace_strings']
      end

82 83 84 85
      def cited_only?
        !!@cited
      end

86 87 88 89
      def extend_path(name)
        if name.nil? || name.empty?
          name = config['bibliography']
        end
90

91 92
        # return as is if it is an absolute path
        return name if name.start_with?('/') && File.exists?(name)
93

94 95 96 97
        p = File.join(config['source'], name)
        p << '.bib' unless File.exists?(p)
        p
      end
98

99
      def reference_tag(entry)
Sylvester Keil's avatar
Sylvester Keil committed
100
        return missing_reference unless entry
101

102
        entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
103
        reference = CiteProc.process entry.to_citeproc,
104
          :style => style, :locale => config['locale'], :format => 'html'
105

Sylvester Keil's avatar
Sylvester Keil committed
106 107 108 109
        content_tag reference_tagname, reference,
          :id => [prefix, entry.key].compact.join('-')
      end

110 111 112 113
      def style
        @style || config['style']
      end

Sylvester Keil's avatar
Sylvester Keil committed
114 115 116 117 118 119 120 121 122 123 124 125
      def missing_reference
        config['missing_reference']
      end

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

      def bibliography_template
        config['bibliography_template'] || '%{reference}'
      end

126
      def bibliography_tag(entry, index)
Sylvester Keil's avatar
Sylvester Keil committed
127 128 129 130
        return missing_reference unless entry

        bibliography_template % {
          :reference => reference_tag(entry),
131
          :key => entry.key,
132
          :type => entry.type,
133
          :index => index
Sylvester Keil's avatar
Sylvester Keil committed
134
        }
135 136
      end

137 138 139
      def generate_details?
        site.layouts.key?(File.basename(config['details_layout'], '.html'))
      end
140

141 142
      def details_file_for(entry)
        name = entry.key.to_s.dup
143

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

146 147
        [name, 'html'].join('.')
      end
148

Sylvester Keil's avatar
Sylvester Keil committed
149
      def details_link_for(entry, base = base_url)
150
        File.join(base, details_path, details_file_for(entry))
Sylvester Keil's avatar
Sylvester Keil committed
151
      end
152

Sylvester Keil's avatar
Sylvester Keil committed
153
      def base_url
154
        @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
155
      end
156

157 158 159
      def details_path
        config['details_dir']
      end
160

Sylvester Keil's avatar
Sylvester Keil committed
161
      def cite(key)
162 163 164
        context['cited'] ||= []
        context['cited'] << key

Sylvester Keil's avatar
Sylvester Keil committed
165
        if bibliography.key?(key)
166 167 168
          entry = bibliography[key]
          entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?

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

172
          link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
Sylvester Keil's avatar
Sylvester Keil committed
173
        else
174
          missing_reference
Sylvester Keil's avatar
Sylvester Keil committed
175
        end
176 177
      rescue
        "(#{key})"
Sylvester Keil's avatar
Sylvester Keil committed
178
      end
Sylvester Keil's avatar
Sylvester Keil committed
179

180
      def cite_details(key, text)
Hiren Patel's avatar
Hiren Patel committed
181
        if bibliography.key?(key)
Sylvester Keil's avatar
Sylvester Keil committed
182
          link_to details_link_for(bibliography[key]), text || config['details_link']
Hiren Patel's avatar
Hiren Patel committed
183
        else
184
          missing_reference
Hiren Patel's avatar
Hiren Patel committed
185 186
        end
      end
187

Sylvester Keil's avatar
Sylvester Keil committed
188 189 190 191 192 193
      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
194

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

Sylvester Keil's avatar
Sylvester Keil committed
197 198 199 200 201 202
        if content.nil?
          "<#{[name, attributes].flatten.compact.join(' ')}/>"
        else
          "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
        end
      end
203

Sylvester Keil's avatar
Sylvester Keil committed
204 205 206
      def link_to(href, content, attributes = {})
        content_tag :a, content || href, attributes.merge(:href => href)
      end
207

208 209 210 211
      def cited_references
        context && context['cited'] || []
      end

Sylvester Keil's avatar
Sylvester Keil committed
212
      def set_context_to(context)
213
        @context, @site, = context, context.registers[:site]
Sylvester Keil's avatar
Sylvester Keil committed
214 215
        config.merge!(site.config['scholar'] || {})
      end
216
    end
217

218
  end
Hiren Patel's avatar
Hiren Patel committed
219
end