details.rb 1.69 KB
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1 2 3 4 5 6 7 8 9
module Jekyll
  class Scholar

    class Details < Page
      
      attr_reader :config
      
      def initialize(site, base, dir, entry)
        @site, @base, @dir = site, base, dir
10

Sylvester Keil's avatar
Sylvester Keil committed
11
        @config = Scholar.defaults.merge(site.config['scholar'] || {})
12

Sylvester Keil's avatar
Sylvester Keil committed
13
        @name = filename_for(entry)
Sylvester Keil's avatar
Sylvester Keil committed
14 15 16

        process(@name)
        read_yaml(File.join(base, '_layouts'), config['details_layout'])
17

18
				liquidify(entry)
Sylvester Keil's avatar
Sylvester Keil committed
19
      end
20 21 22

			private
			
Sylvester Keil's avatar
Sylvester Keil committed
23 24 25 26 27 28 29 30
			def filename_for(entry)
				n = entry.key.dup
				
				n.gsub!(/[:\s]+/, '_')
				
				[n, 'html'].join('.')
			end
			
31 32 33 34 35 36 37 38 39 40 41
			def liquidify(entry)
				data['entry'] = {}

				data['entry']['key'] = entry.key
				data['entry']['type'] = entry.type
				
				entry.fields.each do |key, value|
					data['entry'][key.to_s] = value.to_s
        end
			end
			
Sylvester Keil's avatar
Sylvester Keil committed
42 43 44 45 46 47 48 49
    end

    class DetailsGenerator < Generator
      safe true

      attr_reader :config
      
      def generate(site)
50 51 52
        @config = Scholar.defaults.merge(site.config['scholar'] || {})

        if site.layouts.key?(File.basename(config['details_layout'], '.html'))
Sylvester Keil's avatar
Sylvester Keil committed
53
          bibliography[config['query']].each do |entry|
54
            details = Details.new(site, site.source, config['details_dir'], entry)
Sylvester Keil's avatar
Sylvester Keil committed
55 56 57 58 59 60 61 62
            details.render(site.layouts, site.site_payload)
            details.write(site.dest)
            
            site.pages << details
          end
          
        end
      end
63
			
Sylvester Keil's avatar
Sylvester Keil committed
64 65 66
      private
      
      def bibliography
67
        @bibliography ||= BibTeX.open(bibliography_path, :filter => :latex)
Sylvester Keil's avatar
Sylvester Keil committed
68 69
      end          
      
70 71
      def bibliography_path
        p = File.join(config['source'], config['bibliography'])
Sylvester Keil's avatar
Sylvester Keil committed
72 73 74 75 76 77 78 79
        p << '.bib' unless File.exists?(p)
        p
      end      
    end


  end
end