Class: RCite::Style
- Inherits:
-
Object
- Object
- RCite::Style
- 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:
- #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_typeandbib_typemethod respectively, wheretypeis the BibTeX entry type for the current text. If so, they set #text accordingly and reset #elements. - The
cite_typeandbib_typemethods use #add to add Elements to #elements. - When these methods return, the actual citation/bibliography entry is constructed from #elements.
Constant Summary
- OPTIONS =
List of Options that determine the behaviour of
Stylemethods 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)
- - (Array<RCite::Element>) elements
-
- (Hash<Symbol, Option>) opts
This style's global Options.
-
- (BibTeX::Entry) text
A BibTeX::Entry that describes the text currently being processed.
Instance Method Summary (collapse)
-
- (Object) add(*elements)
Adds the specified
elementsto the variable@elements. -
- (String?) authors
(also: #author)
Returns a list of all authors of the given text if any are defined.
-
- (String) bib(text, fields = {})
Generates a bibliography entry for the given
text. -
- (String) cite(text, fields = {})
Generates a citation for the given
text. -
- (String?) editors(options = {})
(also: #editor)
Returns a list of all editors of the given text if any are defined.
- - (Style) initialize constructor
-
- (String?) method_missing(method, *args)
Returns the value of a BibTeX field for the current #text.
-
- (Element) sep(separator)
Defines a separator element.
Constructor Details
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.
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'.
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.
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'.
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.
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:
Returns a list of all authors of the given text if any are defined.
100 101 102 |
# File 'lib/rcite/style_helpers.rb', line 100 def () (@text[:author].to_names) if @text[:author] end |
- (String) bib(text, fields = {})
Generates a bibliography entry for the given text. This method
- Merges
text.to_hashandfieldswhereby values fromfieldstake precedence. - Sets
@textto the merge result. When this method returns,@textis set tonilagain. - Looks up the method
"bib_#{text.type}"and calls it.
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
- Merges
text.to_hashandfieldswhereby values fromfieldstake precedence. - Sets
@textto the merge result. When this method returns,@textis set tonilagain. - Looks up the method
"cite_#{text.type}"and calls it.
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.
114 115 116 |
# File 'lib/rcite/style_helpers.rb', line 114 def 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.
75 76 77 |
# File 'lib/rcite/style_helpers.rb', line 75 def sep(separator) RCite::Element.new(:sep, separator) end |