bibtex.rb 1.14 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
2
3
4
5
6
7
8
9
10
11
12
13
require 'bibtex'
require 'citeproc'

module Jekyll
  module Scholar
    class BibTeXConverter < Converter
      safe true
    
      priority :highest
    
      DEFAULTS = Hash[*%w{
        citation_style  apa
        citation_locale en
Sylvester Keil's avatar
Sylvester Keil committed
14
        citation_sort_by none
Sylvester Keil's avatar
Sylvester Keil committed
15
16
      }].freeze
      
Sylvester Keil's avatar
Sylvester Keil committed
17
      PATTERN = (/bib(tex)?$/i).freeze
Sylvester Keil's avatar
Sylvester Keil committed
18
19
20
21
22
23
24
25
26
27
28
29
      EXTENSION = '.html'.freeze
      
      def initialize (config = {})
        super
        @config['scholar'] = DEFAULTS.merge(@config['scholar'] || {})
        @markdown = MarkdownConverter.new config
      end
    
      def matches (extension); extension =~ PATTERN; end
    
      def output_ext (extension); EXTENSION; end
    
Sylvester Keil's avatar
Sylvester Keil committed
30
      def convert (content)
Sylvester Keil's avatar
Sylvester Keil committed
31
        content = BibTeX.parse(content, :include => [:meta_content], :filter => [:latex]).map do |b|
Sylvester Keil's avatar
Sylvester Keil committed
32
33
34
          if b.respond_to?(:to_citeproc)
            CiteProc.process b.to_citeproc, :style => @config['citation_style'],
              :locale => @config['citation_locale'], :format => 'html'
Sylvester Keil's avatar
Sylvester Keil committed
35
          else
Sylvester Keil's avatar
Sylvester Keil committed
36
              b.is_a?(BibTeX::MetaContent) ? b.to_s : ''
Sylvester Keil's avatar
Sylvester Keil committed
37
38
39
40
41
42
43
44
          end
        end
        @markdown.convert(content.join("\n"))
      end
    
    end
  end
end