Class: RD::ReferenceResolver

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/rd/reference-resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, prefix = "label:") ⇒ ReferenceResolver

Returns a new instance of ReferenceResolver.



15
16
17
18
19
# File 'lib/rd/reference-resolver.rb', line 15

def initialize(tree, prefix = "label:")
  init_labels(tree)
  @label_prefix = prefix
  @rbl_suite = RBLSuite.new(tree.include_path)
end

Instance Attribute Details

#label_prefixObject (readonly)

Returns the value of attribute label_prefix.



12
13
14
# File 'lib/rd/reference-resolver.rb', line 12

def label_prefix
  @label_prefix
end

#labelsObject (readonly)

Returns the value of attribute labels.



11
12
13
# File 'lib/rd/reference-resolver.rb', line 11

def labels
  @labels
end

#rbl_suiteObject (readonly)

Returns the value of attribute rbl_suite.



13
14
15
# File 'lib/rd/reference-resolver.rb', line 13

def rbl_suite
  @rbl_suite
end

Instance Method Details

#each_labelObject Also known as: each



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rd/reference-resolver.rb', line 43

def each_label
  tmp = []
  labels.each_value do |i|
	i.each do |j|
	  tmp.push(j)
	end
  end
  tmp.sort{|i,j| i[1] <=> j[1]}.each do |i|
	yield(i[0])
  end
end

#get_anchor(element) ⇒ Object



88
89
90
91
92
# File 'lib/rd/reference-resolver.rb', line 88

def get_anchor(element)
  if num = get_label_num(element)
	num2anchor(num)
  end
end

#get_label_num(element) ⇒ Object



82
83
84
85
86
# File 'lib/rd/reference-resolver.rb', line 82

def get_label_num(element)
  entry = labels[element.to_label].find{|i| i[0] == element }
  return nil unless entry
  entry[1]
end

#label2str(label) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/rd/reference-resolver.rb', line 99

def label2str(label)
  case label
  when String
	label
  when Element, Reference::RDLabel
	label.to_label
  else
	raise ArgumentError, "can't extract Label from #{label}."
  end
end

#make_rbl_file(filename) ⇒ Object



110
111
112
# File 'lib/rd/reference-resolver.rb', line 110

def make_rbl_file(filename)
  RBLFile.create_rbl_file(filename, self)
end

#refer(label) ⇒ Object



67
68
69
70
71
# File 'lib/rd/reference-resolver.rb', line 67

def refer(label)
  matched = labels[label2str(label)]
  return nil unless matched
  num2anchor(matched[0][1])
end

#refer_element(label) ⇒ Object



73
74
75
# File 'lib/rd/reference-resolver.rb', line 73

def refer_element(label)
  labels.fetch(label2str(label), []).collect{|i| i[0] }
end

#refer_external_file(label) ⇒ Object



77
78
79
80
# File 'lib/rd/reference-resolver.rb', line 77

def refer_external_file(label)
  label = label.label if label.is_a? Reference
  rbl_suite.refer(label.element_label, label.filename)
end

#referent_of_label(label) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/rd/reference-resolver.rb', line 56

def referent_of_label(label)
  label = label.label if label.is_a? Reference
  if label.filename
	refer_external_file(label)
  else
	anchor = refer(label)
	return nil unless anchor
	[nil, anchor]
  end
end