Class: BibSonomy::CSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name, api_key) ⇒ CSL

Create a new BibSonomy instance.

Parameters:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bibsonomy/csl.rb', line 95

def initialize(user_name, api_key)
  super()
  @bibsonomy = BibSonomy::API.new(user_name, api_key, 'csl')
  # setting some defaults
  @style = 'apa.csl'
  @pdf_dir = nil
  @css_class = 'publications'
  @year_headings = true
  @public_doc_postfix = '_oa.pdf'
  @group = 'public'
  @altmetric_badge_type = nil

  # optional parts to be rendered (or not)
  @doi_link = true
  @url_link = true
  @bibtex_link = true
  @bibsonomy_link = true
  @opt_sep = ' | '
end

Instance Attribute Details

#altmetric_badge_typeString

Returns the badge type for embedded Altmetric badges. If set to ‘nil`, no badge is included. (default: `nil`).

Returns:

  • (String)

    the badge type for embedded Altmetric badges. If set to ‘nil`, no badge is included. (default: `nil`)



79
80
81
# File 'lib/bibsonomy/csl.rb', line 79

def altmetric_badge_type
  @altmetric_badge_type
end

Returns whether links to BibSonomy shall be rendered. (default: ‘true`).

Returns:

  • (Boolean)

    whether links to BibSonomy shall be rendered. (default: ‘true`)



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

def bibsonomy_link
  @bibsonomy_link
end

Returns whether links to the BibTeX data of a post (in BibSonomy) shall be rendered. (default: ‘true`).

Returns:

  • (Boolean)

    whether links to the BibTeX data of a post (in BibSonomy) shall be rendered. (default: ‘true`)



70
71
72
# File 'lib/bibsonomy/csl.rb', line 70

def bibtex_link
  @bibtex_link
end

#css_classString

Returns the CSS class used to render the surrounding ‘<ul>` list (default: ’publications’).

Returns:

  • (String)

    the CSS class used to render the surrounding ‘<ul>` list (default: ’publications’)



61
62
63
# File 'lib/bibsonomy/csl.rb', line 61

def css_class
  @css_class
end

Returns whether links for DOIs shall be rendered. (default: ‘true`).

Returns:

  • (Boolean)

    whether links for DOIs shall be rendered. (default: ‘true`)



64
65
66
# File 'lib/bibsonomy/csl.rb', line 64

def doi_link
  @doi_link
end

#groupString

Returns which posts shall be included, based on the groups they are viewable for.

Returns:

  • (String)

    which posts shall be included, based on the groups they are viewable for



88
89
90
# File 'lib/bibsonomy/csl.rb', line 88

def group
  @group
end

#opt_sepString

Returns the separator between options. (default: ‘ | ’).

Returns:

  • (String)

    the separator between options. (default: ‘ | ’)



76
77
78
# File 'lib/bibsonomy/csl.rb', line 76

def opt_sep
  @opt_sep
end

#pdf_dirString

Returns the output directory for downloaded PDF files. If set to ‘nil`, no documents are downloaded. (default: `nil`).

Returns:

  • (String)

    the output directory for downloaded PDF files. If set to ‘nil`, no documents are downloaded. (default: `nil`)



52
53
54
# File 'lib/bibsonomy/csl.rb', line 52

def pdf_dir
  @pdf_dir
end

#public_doc_postfixString

Returns When a post has several documents and the filename of one of them ends with ‘public_doc_postfix`, only this document is downloaded and linked, all other are ignored. (default: ’_oa.pdf’).

Returns:

  • (String)

    When a post has several documents and the filename of one of them ends with ‘public_doc_postfix`, only this document is downloaded and linked, all other are ignored. (default: ’_oa.pdf’)



85
86
87
# File 'lib/bibsonomy/csl.rb', line 85

def public_doc_postfix
  @public_doc_postfix
end

#styleString

Returns the / CSL style used for rendering. (default: ‘apa.csl`).

Returns:

  • (String)

    the / CSL style used for rendering. (default: ‘apa.csl`)



55
56
57
# File 'lib/bibsonomy/csl.rb', line 55

def style
  @style
end

Returns whether URLs of posts shall be rendered. (default: ‘true`).

Returns:

  • (Boolean)

    whether URLs of posts shall be rendered. (default: ‘true`)



67
68
69
# File 'lib/bibsonomy/csl.rb', line 67

def url_link
  @url_link
end

#year_headingsBoolean

Returns whether year headings shall be rendered. (default: ‘true`).

Returns:

  • (Boolean)

    whether year headings shall be rendered. (default: ‘true`)



58
59
60
# File 'lib/bibsonomy/csl.rb', line 58

def year_headings
  @year_headings
end

Instance Method Details

#render(grouping, name, tags, count) ⇒ String

Download ‘count` posts for the given `user` and `tag(s)` and render them with / CSL.

Parameters:

  • grouping (String)

    the type of the name (either “user” or “group”)

  • name (String)

    the name of the group or user

  • tags (Array<String>)

    the tags that all posts must contain (can be empty)

  • count (Integer)

    number of posts to download

Returns:

  • (String)

    the rendered posts as HTML



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/bibsonomy/csl.rb', line 123

def render(grouping, name, tags, count)
  # get posts from BibSonomy
  posts = JSON.parse(@bibsonomy.get_posts(grouping, name, 'publication', tags, 0, count))

  # render them with citeproc
  cp = CiteProc::Processor.new style: @style, format: 'html'
  cp.import posts

  # to check for duplicate file names
  file_names = []

  # filter posts by group
  # 2017-05-30, rja, disabled until group information is returned by the API
  # posts.delete_if do |v|
  #   if v["group"] == @group
  #     true
  #   else
  #     print("WARN: " + v["group"])
  #     false
  #   end
  # end

  # sort posts by year
  sorted_keys = posts.keys.sort { |a,b| get_sort_posts(posts[b], posts[a]) }

  result = ""

  # print first heading
  last_year = 0

  if @year_headings and sorted_keys.length > 0
    last_year = get_year(posts[sorted_keys[0]])
    result += "<h3>" + last_year + "</h3>"
  end

  result += "<ul class='#{@css_class}'>\n"
  for post_id in sorted_keys
    post = posts[post_id]

    # print heading
    if @year_headings
      year = get_year(post)
      if year != last_year
        last_year = year
        result += "</ul>\n<h3>" + last_year + "</h3>\n<ul class='#{@css_class}'>\n"
      end
    end

    # render metadata
    csl = cp.render(:bibliography, id: post_id)
    result += "<li class='" + post["type"] + "'>#{csl[0]}"

    # extract the post's id
    intra_hash, user_name = get_intra_hash(post_id)

    # optional parts
    options = []
    # attach documents
    if @pdf_dir
      for doc in get_public_docs(post["documents"])
        # fileHash, fileName, md5hash, userName
        file_path = get_document(@bibsonomy, intra_hash, user_name, doc, @pdf_dir, file_names)
        options << "<a href='#{file_path}'>PDF</a>"
      end
    end
    # attach DOI
    doi = post["DOI"]
    if @doi_link and doi != ""
      doi, doi_url = get_doi(doi)
      options << "DOI:<a href='#{doi_url}'>#{doi}</a>"
    end
    # attach URL
    url = post["URL"]
    if @url_link and url != ""
      options << "<a href='#{url}'>URL</a>"
    end
    # attach BibTeX
    if @bibtex_link
      options << "<a href='https://www.bibsonomy.org/bib/publication/#{intra_hash}/#{user_name}'>BibTeX</a>"
    end
    # attach link to BibSonomy
    if @bibsonomy_link
      options << "<a href='https://www.bibsonomy.org/publication/#{intra_hash}/#{user_name}'>BibSonomy</a>"
    end

    # attach options
    if options.length > 0
      result += " <span class='opt'>[" + options.join(@opt_sep) + "]</span>"
    end

    # attach Altmetric badge
    if @altmetric_badge_type and doi != ""
      doi, doi_url = get_doi(doi)
      result += "<div class='altmetric-embed' data-badge-type='#{@altmetric_badge_type}' data-doi='#{doi}'></div>"
    end

    result += "</li>\n"
  end
  result += "</ul>\n"

  return result
end