Class: RCite::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/rcite/style.rb,
lib/rcite/style_options.rb,
lib/rcite/style_helpers.rb

Overview

Ancestor class for all user-defined styles. This class defines a unified interface for styles (namely the #cite and #bib method) as well as providing various helper methods to ease creating new styles.

The 'workflow' of the Style class goes as follows:

  1. #cite or #bib are called with a BibTeX::Entry for the text that is to be cited. They check if their style object has a cite_type and bib_type method respectively, where type is the BibTeX entry type for the current text. If so, they set #text accordingly and reset #elements.
  2. The cite_type and bib_type methods use #add to add Elements to #elements.
  3. When these methods return, the actual citation/bibliography entry is constructed from #elements.

Constant Summary

OPTIONS =

List of Options that determine the behaviour of Style methods and provide instructions for other class that deal with styles (most notably the TextProcessor).

For each of these options, a helper method named _<option name> is added to the RCite::Style class. Users can set options by calling _<option name>(value...) or get the value(s) of an option using the plain _<option name>.

TODO document individual options

Returns:

  • (Array<Option>)

    List of Options that determine the behaviour of Style methods and provide instructions for other class that deal with styles (most notably the TextProcessor).

    For each of these options, a helper method named _<option name> is added to the RCite::Style class. Users can set options by calling _<option name>(value...) or get the value(s) of an option using the plain _<option name>.

    TODO document individual options

[
  Option.new(:ordering,             default:     :last_first,
                                    good_values: [:last_first, :first_last],
                                    validator:   Proc.new { false },
                                    allow_nil:   false                    ),
  Option.new(:delim,                default:     '; ',
                                    transformer: String                   ),
  Option.new(:et_al,                default:     3,
                                    transformer: Integer                  ),
  Option.new(:et_al_string,         default:     'et al.',
                                    transformer: Integer                  ),
  Option.new(:around_each_bib,      transformer: String,
                                    default:     []                       ),
  Option.new(:around_each_cite,     transformer: String,
                                    default:     []                       ),
  Option.new(:around_all_bibs,      transformer: String,
                                    default:     []                       ),
  Option.new(:around_all_cites,     transformer: String,
                                    default:     []                       ),
  Option.new(:between_bibs,         transformer: String,
                                    default:     "\n"                     ),
  Option.new(:between_cites,        transformer: String,
                                    default:     "; "                     ),
  Option.new(:sort_bibliography_by, transformer: Symbol,
                                    default:     [:author, :title, :volume])
]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Style) initialize

Adds each style option in OPTIONS to #opts.



31
32
33
34
# File 'lib/rcite/style_helpers.rb', line 31

def initialize
  @opts = {}
  OPTIONS.each { |o| @opts.merge!(o.name => o.dup) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (String?) method_missing(method, *args)

Returns the value of a BibTeX field for the current #text.

Parameters:

  • method (#to_sym)

    Any field that can be specified in a BibTeX entry, like title, year, shorttitle, url etc. May not begin with an underscore.

Returns:

  • (String, nil)

    The field's value if it is set in the BibTeX entry #text, otherwise nil.

Raises:

  • NoMethodError if #text == nil.



22
23
24
25
26
# File 'lib/rcite/style_helpers.rb', line 22

def method_missing(method, *args)
  raise NoMethodError,
    "Can't look up BibTeX fields: Style#text is `nil`" unless @text
  @text[method.to_sym]
end

Instance Attribute Details

- (Array<RCite::Element>) elements

Array of Elements for the current #text. See RCite::Style for information on the RCite 'workflow'.

Returns:



31
32
33
# File 'lib/rcite/style.rb', line 31

def elements
  @elements
end

- (Hash<Symbol, Option>) opts

This style's global Options. Keys are option names (Option#name), values are the Option objects that describe the behaviour of various helper methods. For a list of all options see OPTIONS.

Returns:

  • (Hash<Symbol, Option>)

    This style's global Options. Keys are option names (Option#name), values are the Option objects that describe the behaviour of various helper methods. For a list of all options see OPTIONS.



10
11
12
# File 'lib/rcite/style_helpers.rb', line 10

def opts
  @opts
end

- (BibTeX::Entry) text

A BibTeX::Entry that describes the text currently being processed. Should not be changed by any methods except #cite and #bib. See RCite::Style for information on the RCite 'workflow'.

Returns:

  • (BibTeX::Entry)

    The current text's entry.



25
26
27
# File 'lib/rcite/style.rb', line 25

def text
  @text
end

Instance Method Details

- (Object) add(*elements)

Adds the specified elements to the variable @elements. Each element can be either a String or an Element. Strings are converted to Elements of type :con before appending. nil arguments and empty strings are dropped.

Parameters:

  • elements (Element, String)

    Any number of elements or strings that should be appended to @elements.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rcite/style_helpers.rb', line 45

def add(*elements)
  elements.map! do |e|
    if e.is_a? RCite::Element
      e
    elsif e && e.to_s == ''
      nil
    elsif e
      RCite::Element.new(:con, e)
    end
  end

  @elements.concat elements.compact
end

- (String?) authors Also known as: author

Returns a list of all authors of the given text if any are defined.

Parameters:

  • options (Hash)

    Controls the style of the list generated by this method.

Returns:

  • (String, nil)

    The list of authors, or nil if the bibliographic data for this text defines none.



100
101
102
# File 'lib/rcite/style_helpers.rb', line 100

def authors()
  authors_or_editors(@text[:author].to_names) if @text[:author]
end

- (String) bib(text, fields = {})

Generates a bibliography entry for the given text. This method

  1. Merges text.to_hash and fields whereby values from fields take precedence.
  2. Sets @text to the merge result. When this method returns, @text is set to nil again.
  3. Looks up the method "bib_#{text.type}" and calls it.

Parameters:

  • text (BibTeX::Entry)

    A bibliography entry in bibtex-ruby's format.

  • fields (Hash) (defaults to: {})

    A hash where each key is a BibTeX field and each value is that field's value. This can be used to set fields 'on the fly', most notably the thepage field that indicates which page the user wants to cite.

Returns:

  • (String)

    The bibliography entry.

Raises:

  • ArgumentError if the style does not support the given entry's type.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rcite/style.rb', line 81

def bib(text, fields = {})
  method = "bib_#{text.type}".to_s
  if !respond_to?(method, true)
    raise ArgumentError.new("This style does not define the type"+
      " '#{text.type}'.")
  end
  @text = text.dup << fields
  @elements= []
  begin
    send(method) 
  ensure
    @text = nil
  end
  elements_to_string(@elements)
end

- (String) cite(text, fields = {})

Generates a citation for the given text. This method

  1. Merges text.to_hash and fields whereby values from fields take precedence.
  2. Sets @text to the merge result. When this method returns, @text is set to nil again.
  3. Looks up the method "cite_#{text.type}" and calls it.

Parameters:

  • text (BibTeX::Entry)

    A bibliography entry in bibtex-ruby's format.

  • fields (Hash) (defaults to: {})

    A hash where each key is a BibTeX field and each value is that field's value. This can be used to set fields 'on the fly', most notably the thepage field that indicates which page the user wants to cite.

Returns:

  • (String)

    The citation.

Raises:

  • ArgumentError if the style does not support the given entry's type.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rcite/style.rb', line 52

def cite(text, fields = {})
  method = "cite_#{text.type}"
  if !respond_to?(method, true)
    raise ArgumentError.new("This style does not define the type"+
      "' #{text.type}'.")
  end
  @text = text.dup << fields
  @elements = []
  begin
    send(method)
  ensure
    @text = nil
  end
  elements_to_string(@elements)
end

- (String?) editors(options = {}) Also known as: editor

Returns a list of all editors of the given text if any are defined.

Parameters:

  • options (Hash) (defaults to: {})

    Controls the style of the list generated by this method.

Options Hash (options):

  • :ordering (:first_last, :last_first)

    Controls the order in which family and given names are printed. If :first_last is given, Mr Theodor zu Guttenberg is printed as "Theodor zu Guttenberg". For :last_first it's "zu Guttenberg, Theodor".

  • :delim (String)

    The list delimiter.

  • :et_al (Integer)

    The maximum number of persons that are listed. If there are more person, :et_al_string is added to the end of the list.

  • :et_al_string (String)

    The term that is appended when more than :et_al persons are given.

Returns:

  • (String, nil)

    The list of editors, or nil if the bibliographic data for this text defines none.



114
115
116
# File 'lib/rcite/style_helpers.rb', line 114

def editors(options = {})
  authors_or_editors(@text[:editor].to_names) if @text[:editor]
end

- (Element) sep(separator)

Defines a separator element. This method is a very simple helper method for style generation which can be used to indicate that separator is a separator, not bibliographic data. This method should be used in conjunction with #add, as shown in the example.

Examples:

Using sep with add

add authors
add sep ': '
add title

Parameters:

  • separator (String)

    Some string that should be used as a separator.

Returns:

  • (Element)

    An Element of type :sep with separator as the element's content.



75
76
77
# File 'lib/rcite/style_helpers.rb', line 75

def sep(separator)
  RCite::Element.new(:sep, separator)
end