bibtex.rb 1.1 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
module Jekyll
2
  class Scholar
Sylvester Keil's avatar
Sylvester Keil committed
3
    class BibTeXConverter < Converter
Sylvester Keil's avatar
Sylvester Keil committed
4
5
      include Scholar::Utilities

Sylvester Keil's avatar
Sylvester Keil committed
6
7
      safe true
      priority :highest
Sylvester Keil's avatar
Sylvester Keil committed
8

Sylvester Keil's avatar
Sylvester Keil committed
9
10
      attr_reader :config

11
12
      @pattern = (/bib(tex)?$/i).freeze
      @extension = '.html'.freeze
Sylvester Keil's avatar
Sylvester Keil committed
13
14
15
16
17

      class << self
        attr_reader :pattern, :extension
      end

Sylvester Keil's avatar
Sylvester Keil committed
18
      def initialize(config = {})
Sylvester Keil's avatar
Sylvester Keil committed
19
        super
20
        @config['scholar'] = Scholar.defaults.merge(@config['scholar'] || {})
Sylvester Keil's avatar
Sylvester Keil committed
21
        @markdown = Jekyll::Converters::Markdown.new(config)
Sylvester Keil's avatar
Sylvester Keil committed
22
      end
Sylvester Keil's avatar
Sylvester Keil committed
23

24
      def matches(extension)
Sylvester Keil's avatar
Sylvester Keil committed
25
26
27
        extension =~ BibTeXConverter.pattern
      end

28
      def output_ext(extension)
Sylvester Keil's avatar
Sylvester Keil committed
29
30
31
        BibTeXConverter.extension
      end

Sylvester Keil's avatar
Sylvester Keil committed
32
      def convert(content)
33
34
        content = BibTeX.parse(content, :strict => true, :include => [:meta_content],
                               :filter => @config['scholar']['bibtex_filters']).map do |b|
Sylvester Keil's avatar
Sylvester Keil committed
35
          if b.respond_to?(:to_citeproc)
Sylvester Keil's avatar
Sylvester Keil committed
36
            render_bibliography b
Sylvester Keil's avatar
Sylvester Keil committed
37
          else
Sylvester Keil's avatar
Sylvester Keil committed
38
            b.is_a?(BibTeX::MetaContent) ? b.to_s : ''
Sylvester Keil's avatar
Sylvester Keil committed
39
40
          end
        end
Sylvester Keil's avatar
Sylvester Keil committed
41

Sylvester Keil's avatar
Sylvester Keil committed
42
43
        @markdown.convert(content.join("\n"))
      end
Sylvester Keil's avatar
Sylvester Keil committed
44

Sylvester Keil's avatar
Sylvester Keil committed
45
46
    end
  end
Sylvester Keil's avatar
Sylvester Keil committed
47
end