Class: Nesta::Plugin::Maldini::Bibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/nesta-plugin-maldini/init.rb

Overview

module Helpers

INITIALIZATION collapse

FILES collapse

CITATIONS collapse

REFERENCE LISTS collapse

Constructor Details

#initialize(file = nil) ⇒ Bibliography

USAGE: Nesta::Plugin::Maldini::Bibliography.new('file')

  • 'file' is optional


19
20
21
22
23
24
25
26
27
28
# File 'lib/nesta-plugin-maldini/init.rb', line 19

def initialize(file = nil)
  # Initialise instance variables
  @thebibliography = nil
  @theentries = []

  # Open file, if such there be
  if (!file.nil?)
    self.open(file)
  end # if (!file.nil?)
end

Instance Method Details

#cite(citekey, postnote = "", prenote = "") ⇒ Object

USAGE: cite('citationkey', 'postnote', 'prenote')

  • 'prenote' and 'postnote' are both optional

  • Modeled on biblatex command cite[postnote]citationkey


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/nesta-plugin-maldini/init.rb', line 122

def cite(citekey, postnote="", prenote="")
  citestring = ""
  theentry=addentry(citekey)
  if theentry.nil?
    citestring << "**Maldini: Invalid Citation Key**"
  else
    citestring << prenote << " " unless prenote == ""
    citestring << formatauthors(theentry[:author]) << ", " << theentry[:year].to_s
    citestring << ", " << postnote unless postnote == ""
  end # if
  citestring = "**Maldini: Invalid encoding in entry `" << citekey << "`**" unless citestring.force_encoding("UTF-8").valid_encoding?
  return citestring
end

#citeauthor(citekey, postnote = "", prenote = "") ⇒ Object

USAGE: citeauthor('citationkey', 'postnote', 'prenote')

  • 'prenote' and 'postnote' are both optional

  • Modeled on biblatex command citeauthor[postnote]citationkey


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nesta-plugin-maldini/init.rb', line 94

def citeauthor(citekey, postnote="", prenote="")

  # We don't keep track of entries cited this way for displaying in the bibliography,
  # so it may make sense to give a warning if this is called for an entry that is not
  # already in @theentries

  # Assign an entry based on the citation key we have been passed.
  # TODO: Something smart if lookup fails.
  theentry = @thebibliography[citekey.to_sym]

  if theentry.nil?
    citestring << "**Maldini: Invalid Citation Key**"
  else
    # Format page numbers
    theentry[:pages] = theentry[:pages].gsub(/(\d+)(-+)(\d+)/,'\\1&ndash;\\3') if theentry.has_field?(:pages)

    citestring = ""
    citestring << prenote << " " unless prenote == ""
    citestring << formatauthors(theentry[:author])
    citestring << ", " << postnote unless postnote == ""
  end # if
  citestring = "**Maldini: Invalid encoding in entry `" << citekey << "`**" unless citestring.force_encoding("UTF-8").valid_encoding?
  return citestring
end

#fullcite(citekey, postnote = "", prenote = "") ⇒ Object

USAGE: fullcite('citationkey', 'postnote', 'prenote')

  • 'prenote' and 'postnote' are both optional

  • Modeled on biblatex command fullcite[postnote]citationkey


149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/nesta-plugin-maldini/init.rb', line 149

def fullcite(citekey, postnote="", prenote="")
  citestring = ""
  theentry=addentry(citekey)
  if theentry.nil?
    citestring << "**Maldini: Invalid Citation Key**"
  else
    citestring << prenote << " " unless prenote == ""
    citestring << formatentry(theentry)
    citestring << ", " << postnote unless postnote == ""
  end # if
  citestring = "**Maldini: Invalid encoding in entry `" << citekey << "`**" unless citestring.force_encoding("UTF-8").valid_encoding?
  return citestring
end

#nocite(citekey) ⇒ Object

USAGE: nocite('citationkey')

  • Modeled on biblatex command nocitecitationkey

  • nocite('*') is a special case, which cites every entry in the bibliography


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nesta-plugin-maldini/init.rb', line 60

def nocite(citekey) 
  citestring = ""
  if citekey == '*'
    @thebibliography.entries.each_value { |entry|
      addentry(entry.key)
    }
  else
    theentry = addentry(citekey)
    citestring  << "**Maldini: Invalid Citation Key `" << citekey << "`**" if theentry.nil?
  end
  return citestring
end

#open(file) ⇒ Object

TODO:

  • Set default file to '/attachments/bibliography.bib'


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nesta-plugin-maldini/init.rb', line 34

def open(file)
  # USAGE: open('file')
  # 'file' is ordinarily a URI specifying the address of a .bib file formatted as BibTeX
  
  # The :filter => :latex hash tells BibTeX to convert all strings to Unicode,
  # which among other things converts '--' to endashes and special characters to
  # their Unicode equivalents.  See: https://github.com/inukshuk/latex-decode
  #
  # TODO: Something smart if file cannot be opened.
  @thebibliography = BibTeX.open(file)
  @thebibliography.convert(:latex)
end

#parencite(citekey, postnote = "", prenote = "") ⇒ Object

USAGE: parencite('citationkey', 'postnote', 'prenote')

  • 'prenote' and 'postnote' are both optional

  • Modeled on biblatex command parencite[postnote]citationkey


139
140
141
142
143
144
# File 'lib/nesta-plugin-maldini/init.rb', line 139

def parencite(citekey, postnote="", prenote="")
  citestring = "("
  citestring << cite(citekey, postnote, prenote)
  citestring << ")"
  return citestring
end

#printbibliographyObject

USAGE: printbibliography()

  • Modeled on biblatex command printbibliography


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/nesta-plugin-maldini/init.rb', line 167

def printbibliography()
  # Sort by last name first, first name second, year third
  @theentries.sort_by!{ |e| [
    e.has_field?(:author) ? 1 : 0,
    e.has_field?(:author) ? e[:author].each { |a| (a.last && a.first) ? a.last + a.first : a.last || ""} : "",
    e.has_field?(:year) ? e[:year].to_i : 0
  ] }
  bibliographystring = ""
  @theentries.each do |e|
      # TODO:
      # - Add SmartyPants processing here, with eg. rubypants, though once 
      #   Nesta depends on redcarpet >= 2.0.0 we can do SmartyPants processing with this:
      #     Redcarpet::Render::SmartyPants.render(formatentry(e))
      bibliographystring << "- " << formatentry(e) << ".  \n"
  end # each
  return bibliographystring
end

#textcite(citekey, postnote = "", prenote = "") ⇒ Object

USAGE: textcite('citationkey', 'postnote', 'prenote')

  • 'prenote' and 'postnote' are both optional

  • Modeled on biblatex command textcite[postnote]citationkey


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nesta-plugin-maldini/init.rb', line 76

def textcite(citekey, postnote="", prenote="")
  citestring = ""
  theentry=addentry(citekey)
  if theentry.nil?
    citestring << "**Maldini: Invalid Citation Key** `" << citekey << "`**"
  else
    citestring << prenote << " " unless prenote == ""
    citestring << formatauthors(theentry[:author]) << " (" << theentry[:year].to_s
    citestring << ", " << postnote unless postnote == ""
    citestring << ")"
  end # if
  citestring = "**Maldini: Invalid encoding in entry `" << citekey << "`**" unless citestring.force_encoding("UTF-8").valid_encoding?
  return citestring
end