bibliography.rb 2.62 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
      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|
64
                bibhead = content_tag(tags.first || group_tags.last,
65 66
                                      group_name(keys.first, keyvalue),
                                      :class => config['bibliography_class'])
67
                bibentries = group_renderer(value, keys.drop(1), tags.drop(1))
68 69 70 71 72 73 74 75 76
                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
77
        bibliography = items.each_with_index.map { |entry, index|
78
          reference = bibliography_tag(entry, index + 1)
79

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

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

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

93 94 95
  end
end

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