bibliography_bytype.rb 4.11 KB
Newer Older
1 2 3
module Jekyll
  class Scholar

4
    class BibliographyTagByType < Liquid::Tag
5
      include Scholar::Utilities
rmrf's avatar
rmrf committed
6

7 8 9 10
      def initialize(tag_name, arguments, tokens)
        super

        @config = Scholar.defaults.dup
rmrf's avatar
rmrf committed
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        optparse(arguments)
      end

      def initialize_type_labels()
        @type_labels = Hash[{
                              "@article" => "Journal Articles",
                              "@inproceedings" => "Conference and Workshop Papers",
                              "@incollection" => "Book Chapters",
                              "@techreport" => "Technical Reports",
                              "@book" => "Books"
                            }]
      end


      def initialize_prefix_defaults() 
        @prefix_defaults = Hash[{
                                  :article => "J",
                                  :inproceedings => "C",
                                  :incollection=> "BC",
                                  :techreport => "TR",
                                  :book => "B"
                                }]
      end

      def set_type_counts(tc)
        @type_counts = tc
      end

      def render_index(item, ref)
        si = '[' + @prefix_defaults[item.type].to_s + @type_counts.to_s + ']'
        @type_counts = @type_counts - 1
        
        idx_html = content_tag "div class=\"csl-index\"", si
        return idx_html + ref
      end

      def render_ref_img(item)
        css_points = Hash[{
                         :article => "csl-point-journal-icon",
                         :inproceedings => "csl-point-conference-icon",
                         :incollection=> "csl-point-bookchapter-icon",
                         :techreport => "csl-point-techreport-icon",
                         :book => "csl-point-book-icon"
                       }]

        s = css_points[item.type]
        return s
      end

      def render_header(y)
        ys = content_tag "h2 class=\"csl-year-header\"", y
        ys = content_tag "div class=\"csl-year-icon\"", ys
64 65 66 67 68
      end

      def render(context)
        set_context_to context

69 70
        # Only select items that are public.
        items = entries.select { |e| e.public == 'yes' }
rmrf's avatar
rmrf committed
71

72 73 74
        initialize_prefix_defaults()
        initialize_type_labels()
        set_type_counts(items.size())
75

76 77 78 79 80 81 82 83 84
        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
85
          end
86
        end
87

88
        items = items[offset..max] if limit_entries?
rmrf's avatar
rmrf committed
89 90

        bibliography = render_header(@type_labels[query])
91 92
        bibliography << items.each_with_index.map { |entry, index|
          reference = render_index(entry, bibliography_tag(entry, nil))
rmrf's avatar
rmrf committed
93

94 95 96
          if generate_details?
            reference << link_to(details_link_for(entry),
              config['details_link'], :class => config['details_link_class'])
97 98
          end

99 100 101 102 103 104 105 106 107
          if entry.field?(:award)
            # TODO: Awkward -- Find position to insert it. Before the last </div>
            ts = content_tag "div class=\"csl-award\"", entry.award.to_s
            refPos = reference.rindex('</div>')
            if refPos.nil?
              puts "NILL"
            else 
              reference.insert(reference.rindex('</div>'), ts.to_s)
            end 
108
          end
rmrf's avatar
rmrf committed
109 110 111 112 113 114 115 116
          # Render links if repository specified
          if repository?
            if not repository_link_for(entry).nil?
              puts "link is not null"
              puts repository_link_for(entry)
              reference << "<a class=\"pure-button\" href=\"" + repository_link_for(entry) + "\">PDF</a>"
            end
          end
117

118 119 120 121
          content_tag config['bibliography_item_tag'], reference
          content_tag "li class=\"" + render_ref_img(entry) + "\"", reference
        }.join("\n")

rmrf's avatar
rmrf committed
122 123

        content_tag config['bibliography_list_tag'], bibliography, :class => config['bibliography_class']
124
        
125 126
      end
    end
127

128 129 130
  end
end

131
Liquid::Template.register_tag('bibliography_bytype', Jekyll::Scholar::BibliographyTagByType)