bibtex.rb 1.08 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
module Jekyll
2
  class Scholar
Sylvester Keil's avatar
Sylvester Keil committed
3
4
5
6
7
    class BibTeXConverter < Converter
      safe true
    
      priority :highest
    
8
9
      @pattern = (/bib(tex)?$/i).freeze
      @extension = '.html'.freeze
Sylvester Keil's avatar
Sylvester Keil committed
10
      
11
12
13
14
			class << self
				attr_reader :pattern, :extension
			end
			
Sylvester Keil's avatar
Sylvester Keil committed
15
16
      def initialize (config = {})
        super
17
        @config['scholar'] = Scholar.defaults.merge(@config['scholar'] || {})
Sylvester Keil's avatar
Sylvester Keil committed
18
19
20
        @markdown = MarkdownConverter.new config
      end
    
21
22
23
      def matches(extension)
				extension =~ BibTeXConverter.pattern
			end
Sylvester Keil's avatar
Sylvester Keil committed
24
    
25
26
27
      def output_ext(extension)
				BibTeXConverter.extension
			end
Sylvester Keil's avatar
Sylvester Keil committed
28
    
Sylvester Keil's avatar
Sylvester Keil committed
29
      def convert (content)
Sylvester Keil's avatar
Sylvester Keil committed
30
        content = BibTeX.parse(content, :strict => true, :include => [:meta_content], :filter => [:latex]).map do |b|
Sylvester Keil's avatar
Sylvester Keil committed
31
          if b.respond_to?(:to_citeproc)
Sylvester Keil's avatar
Sylvester Keil committed
32
33
            CiteProc.process b.to_citeproc, :style => @config['style'],
              :locale => @config['locale'], :format => 'html'
Sylvester Keil's avatar
Sylvester Keil committed
34
          else
Sylvester Keil's avatar
Sylvester Keil committed
35
              b.is_a?(BibTeX::MetaContent) ? b.to_s : ''
Sylvester Keil's avatar
Sylvester Keil committed
36
37
38
39
40
41
42
43
          end
        end
        @markdown.convert(content.join("\n"))
      end
    
    end
  end
end