Class: Rubyscholar::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyscholar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, nameToHighlight = nil, pdfLinks = {}, altmetricDOIs = [], minCitationCount = 1) ⇒ Formatter

Returns a new instance of Formatter.



75
76
77
78
79
80
81
# File 'lib/rubyscholar.rb', line 75

def initialize(parser, nameToHighlight = nil, pdfLinks = {}, altmetricDOIs = [], minCitationCount = 1)
  @parser          = parser
  @nameToHighlight = nameToHighlight
  @pdfLinks        = pdfLinks
  @altmetricDOIs   = altmetricDOIs
  @minCitations    = minCitationCount
end

Instance Attribute Details

#altmetricDOIsObject

Returns the value of attribute altmetricDOIs.



73
74
75
# File 'lib/rubyscholar.rb', line 73

def altmetricDOIs
  @altmetricDOIs
end

#nameToHighlightObject

Returns the value of attribute nameToHighlight.



73
74
75
# File 'lib/rubyscholar.rb', line 73

def nameToHighlight
  @nameToHighlight
end

#parserObject

Returns the value of attribute parser.



73
74
75
# File 'lib/rubyscholar.rb', line 73

def parser
  @parser
end

Returns the value of attribute pdfLinks.



73
74
75
# File 'lib/rubyscholar.rb', line 73

def pdfLinks
  @pdfLinks
end

Instance Method Details

#to_htmlObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rubyscholar.rb', line 83

def to_html
  ##@doc = Nokogiri::HTML::DocumentFragment.parse ""
  builder = Nokogiri::HTML::Builder.new do |doc|
    doc.html {
      doc.body {
        @parser.parsedPapers.each_with_index { |paper, index|
          doc.div( :class => "publication") {
            doc.p {
              doc.text ((@parser.parsedPapers).length - index).to_s + '. '

              doc.b    paper[:title] + '.'
              doc.text ' (' + paper[:year] + '). '

              if paper[:authors].include?(@nameToHighlight)
                doc.text( paper[:authors].sub(Regexp.new(@nameToHighlight + '.*'), '') )
                doc.span( :class => "label label-info") { doc.text @nameToHighlight }
                doc.text( paper[:authors].sub(Regexp.new('.*' + @nameToHighlight), '') )
              else
                doc.text( paper[:authors])
              end

              doc.br
              doc.em   paper[:journalName]
              doc.text ' '
              doc.text paper[:journalDetails]
              unless paper[ :doi].empty?
                doc.text(' ')
                doc.a( :href => URI.join("http://dx.doi.org/", paper[ :doi]))  {
                  doc.text "[DOI]"
                }
              end
              if @pdfLinks.keys.include?(paper[:title])
                doc.text(' ')
                doc.a( :href => @pdfLinks[paper[:title]])  {
                  doc.text "[PDF]"
                }
              end
              if paper[ :citationCount].to_i > @minCitations
                doc.text(' ')
                doc.a( :href => paper[ :citingPapers]) {
                  doc.text("[Cited #{paper[ :citationCount]}x]")
                }
              end
              if altmetricDOIs.include?( paper[ :doi])
                doc.text(' ')
                doc.span( :class                => 'altmetric-embed',
                          :'data-badge-popover' => 'bottom',
                          :'data-doi'           => paper[ :doi]        )
              end
            }
          }
        }
      }
    }
  end
  return builder.to_html
end