Module: Serializers::BibTex

Included in:
Document
Defined in:
lib/serializers/bib_tex.rb

Overview

Convert a document to a BibTeX record

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

Register this serializer in the Document list



9
10
11
12
13
14
15
# File 'lib/serializers/bib_tex.rb', line 9

def self.included(base)
  base.register_serializer(
    :bibtex,
    'BibTeX', ->(doc) { doc.to_bibtex },
    'http://mirrors.ctan.org/biblio/bibtex/contrib/doc/btxdoc.pdf'
  )
end

Instance Method Details

- (String) to_bibtex

Returns this document as a BibTeX record

Examples:

Download this document as a bib file

controller.send_data doc.to_bibtex, filename: 'export.bib',
                     disposition: 'attachment'


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
# File 'lib/serializers/bib_tex.rb', line 24

def to_bibtex
  # We don't have a concept of cite keys, so we're forced to just use
  # AuthorYear and hope it doesn't collide
  if formatted_author_list.nil? || formatted_author_list.count == 0
    first_author = 'Anon'
  else
    first_author = formatted_author_list[0].last.gsub(' ', '').gsub(/[^A-za-z0-9_]/, '')
  end
  cite_key = "#{first_author}#{year}"

  ret  = "@article{#{cite_key},\n"
  unless author_list.nil? || author_list.count == 0
    ret << "    author = {#{author_list.join(' and ')}},\n"
  end
  ret << "    title = {#{title}},\n" unless title.blank?
  ret << "    journal = {#{journal}},\n" unless journal.blank?
  ret << "    volume = {#{volume}},\n" unless volume.blank?
  ret << "    number = {#{number}},\n" unless number.blank?
  ret << "    pages = {#{pages}},\n" unless pages.blank?
  ret << "    doi = {#{doi}},\n" unless doi.blank?
  ret << "    year = {#{year}}\n" unless year.blank?
  ret << "}\n"

  ret
end