bibtex.rb 1.11 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
    class BibTeXConverter < Converter
      safe true
      priority :highest
Sylvester Keil's avatar
Sylvester Keil committed
6 7

			attr_reader :config
Sylvester Keil's avatar
Sylvester Keil committed
8
    
9 10
      @pattern = (/bib(tex)?$/i).freeze
      @extension = '.html'.freeze
Sylvester Keil's avatar
Sylvester Keil committed
11
      
12 13 14 15
			class << self
				attr_reader :pattern, :extension
			end
			
Sylvester Keil's avatar
Sylvester Keil committed
16
      def initialize(config = {})
Sylvester Keil's avatar
Sylvester Keil committed
17
        super
18
        @config['scholar'] = Scholar.defaults.merge(@config['scholar'] || {})
Sylvester Keil's avatar
Sylvester Keil committed
19
        @markdown = Jekyll::Converters::Markdown.new(config)
Sylvester Keil's avatar
Sylvester Keil committed
20 21
      end
    
22 23 24
      def matches(extension)
				extension =~ BibTeXConverter.pattern
			end
Sylvester Keil's avatar
Sylvester Keil committed
25
    
26 27 28
      def output_ext(extension)
				BibTeXConverter.extension
			end
Sylvester Keil's avatar
Sylvester Keil committed
29
    
Sylvester Keil's avatar
Sylvester Keil committed
30 31
      def convert(content)
				content = BibTeX.parse(content, :strict => true, :include => [:meta_content], :filter => [:latex]).map do |b|
Sylvester Keil's avatar
Sylvester Keil committed
32
          if b.respond_to?(:to_citeproc)
Sylvester Keil's avatar
Sylvester Keil committed
33 34
            CiteProc.process b.to_citeproc, :style => config['style'],
              :locale => config['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
          end
        end
Sylvester Keil's avatar
Sylvester Keil committed
39

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