Class: CSL::Nodes::Node

Inherits:
Object
  • Object
show all
Includes:
Support::Attributes, Support::Tree
Defined in:
lib/csl/nodes.rb

Overview

Node

A Node represents a CSL rendering element. Rendering elements are used to specify which, and in what order, bibliographic data should be included in citations and bibliographies. Rendering elements also partly control the formatting of this data.

Each Node is bound to a node in a CSL Style document. Furthermore, before any processiong can be done, the Node must be linked with a processor, in order to be able to access items, format, or locale information.

Instance Attribute Summary collapse

Attributes included from Support::Tree

#node_name, #parent

Attributes included from Support::Attributes

#attributes, #key_filter, #value_filter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Tree

#add_children, #ancestors, #ancestors!, #children, #depth, #descendants, #descendants!, #each, #find_children_by_name, #has_children?, included, #name, #root, #root!, #root?

Methods included from Support::Attributes

#[], #[]=, included

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Node

Returns a new instance of Node.

Yields:

  • (_self)

Yield Parameters:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/csl/nodes.rb', line 71

def initialize(*args)
  @style = args.detect { |argument| argument.is_a?(Style) }
  args.delete(@style) unless @style.nil?
  
  args.each do |argument|
    case          
    when argument.is_a?(Node)
      @parent = argument
      @style = @parent.style || @style
      
    when argument.is_a?(String) && argument.match(/^\s*</)
      parse(Nokogiri::XML.parse(argument) { |config| config.strict.noblanks }.root)
    
    when argument.is_a?(Nokogiri::XML::Node)
      parse(argument)
    
    when argument.is_a?(Hash)
      merge!(argument)
    
    else
      CiteProc.log.warn "cannot initialize Node from argument #{ argument.inspect }" unless argument.nil?
    end
  end

  set_defaults
  
  yield self if block_given?
end

Instance Attribute Details

#styleObject (readonly)

Returns the value of attribute style.



45
46
47
# File 'lib/csl/nodes.rb', line 45

def style
  @style
end

Class Method Details

.format_on(*args) ⇒ Object

Chains the format method to the given methods



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/csl/nodes.rb', line 49

def format_on(*args)
  args.flatten.each do |method_id|

    # Set up Around Alias Chain
    original_method = [method_id, 'without_formatting'].join('_')
    alias_method original_method, method_id

    define_method method_id do |*args, &block|
      begin
        string = send(original_method, *args, &block)
        processor = args.detect { |argument| argument.is_a?(CiteProc::Processor) }
        
        processor.nil? ? string : processor.format(string, attributes)
      rescue Exception => e
        CiteProc.log :error, "failed to format string #{ string.inspect }", e
        args[0]
      end
    end
  end
end

Instance Method Details

#copyObject



122
# File 'lib/csl/nodes.rb', line 122

def copy; self.class.new(attributes, style); end

#evaluate(data, processor) ⇒ Object



155
156
157
# File 'lib/csl/nodes.rb', line 155

def evaluate(data, processor)
  false
end

#localized_date_parts(key, processor = nil) ⇒ Object



138
139
140
141
142
# File 'lib/csl/nodes.rb', line 138

def localized_date_parts(key, processor=nil)
  localize(:date, key, processor) do |hash|
    return hash[key] if hash.has_key?(key) && !hash[key].empty?
  end
end

#localized_options(key, processor = nil) ⇒ Object



144
145
146
147
148
# File 'lib/csl/nodes.rb', line 144

def localized_options(key, processor=nil)
  localize(:options, key, processor) do |hash|
    return hash[key] if hash.has_key?(key)
  end
end

#localized_terms(key, processor = nil) ⇒ Object



131
132
133
134
135
# File 'lib/csl/nodes.rb', line 131

def localized_terms(key, processor=nil)
  localize(:term, key, processor) do |hash|
    return hash[key] if hash.has_key?(key) && !hash[key].empty?
  end        
end

#merge(other) ⇒ Object

merged.



116
117
118
119
# File 'lib/csl/nodes.rb', line 116

def merge(other)
  return self.copy if other.nil?
  self.class.new(attributes.merge(other.attributes), other.style || style)
end

#parse(node) ⇒ Object

Parses the given XML node.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/csl/nodes.rb', line 101

def parse(node)
  return if node.nil?
  
  node.attributes.values.each { |a| attributes[a.name] = a.value }
  
  @children = node.children.map do |child|
    Nodes.parse(self, child)
  end
  
  inherit_attributes(node)
  self
end

#process(data, processor) ⇒ Object

Processes the supplied data. @returns a formatted string.



151
152
153
# File 'lib/csl/nodes.rb', line 151

def process(data, processor)
  ''
end

#reverse_merge(other) ⇒ Object

attributes in other take precedence.



126
127
128
# File 'lib/csl/nodes.rb', line 126

def reverse_merge(other)
  other.merge(self)
end

#to_sObject



159
160
161
# File 'lib/csl/nodes.rb', line 159

def to_s
  attributes.merge('node' => self.class.name).inspect
end