bibliography.rb 2.67 KB
Newer Older
1
2
3
4
module Jekyll
  class Scholar

    class BibliographyTag < Liquid::Tag
5
      include Scholar::Utilities
6

7
8
      def initialize(tag_name, arguments, tokens)
        super
Sylvester Keil's avatar
Sylvester Keil committed
9

10
        @config = Scholar.defaults.dup
11
12

        optparse(arguments)
13
14
15
      end

      def render(context)
Sylvester Keil's avatar
Sylvester Keil committed
16
        set_context_to context
17

18
19
20
21
22
23
24
25
26
27
        # Add bibtex files to dependency tree
        if context.registers[:page] and context.registers[:page].has_key? "path"
          bibtex_paths.each do |bibtex_path|
            site.regenerator.add_dependency(
              site.in_source_dir(context.registers[:page]["path"]),
              bibtex_path
            )
          end
        end

Sylvester Keil's avatar
Sylvester Keil committed
28
29
30
31
32
33
34
35
36
37
38
39
        items = entries

        if cited_only?
          items = if skip_sort?
            cited_references.uniq.map do |key|
              items.detect { |e| e.key == key }
            end
          else
            entries.select do |e|
              cited_references.include? e.key
            end
          end
40
41
42

          # See #90
          cited_keys.clear
Sylvester Keil's avatar
Sylvester Keil committed
43
        end
44

45
46
47
48
49
50
51
52
53
54
        if group?
          groups = group(items)
          bibliography = render_groups(groups)
        else
          items = items[offset..max] if limit_entries?
          bibliography = render_items(items)
        end

        bibliography
      end
Sylvester Keil's avatar
Sylvester Keil committed
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      def render_groups(groups)
        def group_renderer(groupsOrItems,keys,tags)
          if keys.count == 0
            renderer(force = true)
            render_items(groupsOrItems)
          else
            groupsOrItems
              .map do |keyvalue,value|
                bibhead = content_tag(tags.first,
                                      group_name(keys.first, keyvalue),
                                      :class => config['bibliography_class'])
                bibentries = group_renderer(value, keys.drop(1),
                                      tags.length > 1 ? tags.drop(1) : tags)
                bibhead + "\n" + bibentries
              end
              .join("\n")
          end
        end
        group_renderer(groups,group_keys,group_tags)
      end
      
      def render_items(items)
Sylvester Keil's avatar
Sylvester Keil committed
78
        bibliography = items.each_with_index.map { |entry, index|
79
          reference = bibliography_tag(entry, index + 1)
80

81
          if generate_details?
82
83
            reference << link_to(details_link_for(entry),
              config['details_link'], :class => config['details_link_class'])
Sylvester Keil's avatar
Sylvester Keil committed
84
          end
85

86
          content_tag config['bibliography_item_tag'], reference
87
        }.join("\n")
Sylvester Keil's avatar
Sylvester Keil committed
88

89
        content_tag config['bibliography_list_tag'], bibliography, :class => config['bibliography_class']
90
      end
91
      
92
    end
93

94
95
96
  end
end

97
Liquid::Template.register_tag('bibliography', Jekyll::Scholar::BibliographyTag)