bibliography.rb 3.01 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
      def render_groups(groups)
57
        def group_renderer(groupsOrItems,keys,order,tags)
58
59
60
61
62
          if keys.count == 0
            renderer(force = true)
            render_items(groupsOrItems)
          else
            groupsOrItems
63
64
65
66
67
68
69
70
              .sort do |e1,e2|
                if (order.first || group_order.last) =~ /^(desc|reverse)/i
                  group_compare(keys.first,e2[0],e1[0])
                else
                  group_compare(keys.first,e1[0],e2[0])
                end
              end
              .map do |e|
71
                bibhead = content_tag(tags.first || group_tags.last,
72
                                      group_name(keys.first, e[0]),
73
                                      :class => config['bibliography_class'])
74
                bibentries = group_renderer(e[1], keys.drop(1), order.drop(1), tags.drop(1))
75
76
77
78
79
                bibhead + "\n" + bibentries
              end
              .join("\n")
          end
        end
80
        group_renderer(groups,group_keys,group_order,group_tags)
81
82
83
      end
      
      def render_items(items)
Sylvester Keil's avatar
Sylvester Keil committed
84
        bibliography = items.each_with_index.map { |entry, index|
85
          reference = bibliography_tag(entry, index + 1)
86

87
          if generate_details?
88
89
            reference << link_to(details_link_for(entry),
              config['details_link'], :class => config['details_link_class'])
Sylvester Keil's avatar
Sylvester Keil committed
90
          end
91

92
          content_tag config['bibliography_item_tag'], reference, config['bibliography_item_attributes']
93
        }.join("\n")
Sylvester Keil's avatar
Sylvester Keil committed
94

95
96
97
        content_tag config['bibliography_list_tag'], bibliography,
          { :class => config['bibliography_class'] }.merge(config['bibliography_list_attributes'])

98
      end
99
      
100
    end
101

102
103
104
  end
end

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