Class: CSL::Term
- Inherits:
-
Object
- Object
- CSL::Term
- Includes:
- Support::Attributes
- Defined in:
- lib/csl/term.rb
Overview
Term
Terms are localized strings. For example, if a style specifies that the term “and” should be used, the string that appears in the style output depends on the locale: “and” for English, “und” for German. Terms are defined using cs:term elements, child elements of cs:terms, itself a child element of cs:locale. Terms are identified by the value of the name attribute of cs:term. Two types of terms exist: simple terms, where the content of the cs:term is the localized string, and compound terms, where cs:term includes the two child elements cs:single and cs:multiple, which respectively contain the singular and plural variant of the term (e.g. “page” and “pages”). Some terms are defined for multiple forms. In these cases, multiple cs:term element share the same value of name, but differ in the value of the optional form attribute. The different forms are:
-
“long” - the default, e.g. “editor” and “editors” for the term “editor”
-
“short” - e.g. “ed” and “eds” for the term “editor”
-
“verb” - e.g. “edited by” for the term “editor”
-
“verb-short” - e.g. “ed” for the term “editor”
-
“symbol” - e.g. “§” for the term “section”
The plural attribute can be set to choose either the singular (value “false”, the default) or plural variant (value “true”) of a term. In addition, the form attribute can be set to select the desired term form (“long” [default], “short”, “verb”, “verb-short” or “symbol”). If for a given term the desired form does not exist, another form may be used: “verb-short” reverts to “verb”, “symbol” reverts to “short”, and “verb” and “short” both revert to “long”.
Instance Attribute Summary
Attributes included from Support::Attributes
#attributes, #key_filter, #value_filter
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #has_gender? ⇒ Boolean
-
#initialize(argument = nil) {|_self| ... } ⇒ Term
constructor
A new instance of Term.
- #parse!(node) ⇒ Object
- #pluralize(options = {}) ⇒ Object
- #singularize(options = {}) ⇒ Object
- #to_s(options = {}) ⇒ Object
Methods included from Support::Attributes
#[], #[]=, included, #merge, #reverse_merge
Constructor Details
#initialize(argument = nil) {|_self| ... } ⇒ Term
Returns a new instance of Term.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/csl/term.rb', line 38 def initialize(argument=nil, &block) case when argument.nil? when argument.is_a?(Hash) merge!(argument) when argument.is_a?(Nokogiri::XML::Node) parse!(argument) when argument.is_a?(String) && argument.match(/^<term/) parse!(Nokogiri::XML.parse(argument).root) when argument.is_a?(String) || argument.is_a?(Symbol) attributes['name'] = argument.to_s else CiteProc.log.warn "failed to create new Term from #{ argument.inspect }" end yield self if block_given? end |
Class Method Details
Instance Method Details
#empty? ⇒ Boolean
108 109 110 |
# File 'lib/csl/term.rb', line 108 def empty? long.nil? && short.nil? && verb.nil? && verb_short.nil? && symbol.nil? end |
#has_gender? ⇒ Boolean
112 113 114 |
# File 'lib/csl/term.rb', line 112 def has_gender? !gender.nil? end |
#parse!(node) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/csl/term.rb', line 71 def parse!(node) raise(ArgumentError, "failed to parse node; expected <term>, was: #{ node.inspect }") unless node.name == 'term' self['name'] = node['name'] self['gender'] = node['gender'] self[node['form'] || node['gender-form'] || 'long'] = Hash[%w{ singular plural }.zip(node.children.map(&:content))] end |
#pluralize(options = {}) ⇒ Object
85 86 87 88 |
# File 'lib/csl/term.rb', line 85 def pluralize(={}) ['plural'] = 'true' to_s() end |
#singularize(options = {}) ⇒ Object
80 81 82 83 |
# File 'lib/csl/term.rb', line 80 def singularize(={}) ['plural'] = 'false' to_s() end |
#to_s(options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/csl/term.rb', line 90 def to_s(={}) plural = ['', 'false', '1', 'never'].include?(['plural'].to_s) ? false : true term = case ['form'] when 'verb-short' then verb_short || verb || long when 'symbol' then symbol || short || long when 'verb' then verb || long when 'short' then short || long else self[['form']] || self[['gender-form']] || long || masculine || feminine || neutral end || {} plural && !term['plural'].nil? ? term['plural'].to_s : term['singular'].to_s rescue Exception => e CiteProc.log.error "failed to convert Term to String: #{ e. }" '' end |