Class: Rice::Doc::Doxygen

Inherits:
Object
  • Object
show all
Defined in:
lib/rice/doc/doxygen.rb

Constant Summary collapse

OPERATORS =
{"assign" => "operator=",
"assign_plus" => "operator+=",
"assign_minus" => "operator-=",
"assign_multiply" => "operator*=",
"assign_divide" => "operator/=",
"call" => "operator()",
"decrement" => "operator--",
"dereference" => "operator*",
"increment" => "operator++",
"==" => "operator==",
"!=" => "operator!=",
"+" => "operator+",
"-" => "operator-",
"*" => "operator*",
"/" => "operator/",
"&" => "operator&",
"|" => "operator|",
"<" => "operator<",
">" => "operator>",
"<=" => "operator<=",
"<<" => "operator<<",
"[]" => "operator[]",
"[]=" => "operator[]"}

Instance Method Summary collapse

Constructor Details

#initialize(root, index, type_mappings = Hash.new, method_mappings = Hash.new) ⇒ Doxygen

Returns a new instance of Doxygen.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rice/doc/doxygen.rb', line 31

def initialize(root, index, type_mappings = Hash.new, method_mappings = Hash.new)
  @root = root
  @type_mappings = type_mappings
  @method_mappings = method_mappings
  @cache = Hash.new
  @typedefs = Hash.new(Hash.new)

  data = URI.open(index).read
  parser = LibXML::XML::Parser.string(data)
  @doc = parser.parse
end

Instance Method Details

#attribute_url(klass, native) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rice/doc/doxygen.rb', line 200

def attribute_url(klass, native)
  node = class_node(klass)

  # Find the member node
  attribute_name = camelize(native.name, false)

  xpath = "member[@kind='variable'][name='#{attribute_name}']"
  member_node = node&.find_first(xpath)
  if member_node
    url(member_node)
  else
    nil
  end
end

#camelize(content, capitalize = true) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rice/doc/doxygen.rb', line 43

def camelize(content, capitalize = true)
  result = content.gsub(/(?:_)(.)/) do |matched|
             $1.capitalize
           end

  result[0] = capitalize ? result[0].upcase : result[0].downcase
  result
end

#class_node(klass) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rice/doc/doxygen.rb', line 84

def class_node(klass)
  native_name = cpp_name(klass)
  typedef_parts = native_name.split("::")
  typedef_name = typedef_parts.pop

  typedefs = @typedefs[klass.name.split("::").first]

  if typedef_type = typedefs[typedef_name]
    typedef_parts.push(typedef_type)
    native_name = typedef_parts.join("::")
  end

  xpath = "//tagfile/compound[@kind='class' or @kind='struct' or @kind='union'][name='#{native_name}']"
  get_node(klass, xpath)
end

#class_url(klass) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/rice/doc/doxygen.rb', line 124

def class_url(klass)
  node = class_node(klass)
  if node
    file = node.find_first('filename').content
    "#{@root}/#{file}"
  end
end

#cpp_name(klass) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rice/doc/doxygen.rb', line 59

def cpp_name(klass)
  result = klass.cpp_class
  @type_mappings.each do |key, value|
    result = result.gsub(key, value)
  end
  result
end

#enum_kind(klass) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rice/doc/doxygen.rb', line 150

def enum_kind(klass)
  parent = figure_parent(klass)
  if parent == Object
    "group"
  elsif parent.is_a?(Class)
    "class"
  elsif parent.is_a?(Module)
    "namespace"
  else
    raise("Unknown parent type: #{parent}")
  end
end

#enum_node(klass) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rice/doc/doxygen.rb', line 100

def enum_node(klass)
  cpp_name = klass.cpp_class.gsub(/^enum /, '')
  cpp_parts = cpp_name.split("::")

  kind = enum_kind(klass)
  xpath = if kind == "group"
            "//tagfile/compound[@kind='#{enum_kind(klass)}']/member[@kind='enumeration'][name='#{cpp_parts.last}']"
          else
            parent_name = cpp_name.split("::")[0..-2].join("::")
            "//tagfile/compound[@kind='#{enum_kind(klass)}'][name='#{parent_name}']/member[@kind='enumeration'][name='#{cpp_parts.last}']"
          end
  get_node(klass, xpath)
end

#enum_url(klass) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rice/doc/doxygen.rb', line 163

def enum_url(klass)
  cpp_name = klass.cpp_class.gsub(/^enum /, '')
  cpp_parts = cpp_name.split("::")

  kind = enum_kind(klass)
  xpath = if kind == "group"
            "//tagfile/compound[@kind='#{enum_kind(klass)}']/member[@kind='enumeration'][name='#{cpp_parts.last}']"
          else
            parent_name = cpp_name.split("::")[0..-2].join("::")
            "//tagfile/compound[@kind='#{enum_kind(klass)}'][name='#{parent_name}']/member[@kind='enumeration'][name='#{cpp_parts.last}']"
          end

  member_node = @doc.find_first(xpath)
  if member_node
    url(member_node)
  else
    nil
  end
end

#enum_value_url(klass, value) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/rice/doc/doxygen.rb', line 183

def enum_value_url(klass, value)
  node = enum_node(klass)
  xpath = "../member[@kind='enumvalue'][name='#{value}']"
  member_node = node&.find_first(xpath)
  if member_node
    url(member_node)
  else
    nil
  end
end

#figure_member_node(native, xpath) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rice/doc/doxygen.rb', line 215

def figure_member_node(native, xpath)
  # First get all matching functions - remember C++ supports method overloads
  member_nodes = @doc.find(xpath)
  case member_nodes.size
    when 0
      return nil
    when 1
      return member_nodes.first
  end

  # Next filter members that have the same number of parameters
  filtered = member_nodes.find_all do |member_node|
    arglist_content = member_node.find_first('arglist').content
    match = arglist_content.match(/\((.*)\)/)
    tag_args = match[1].split(',')
    tag_args.size == native.parameters.size
  end

  if filtered.size == 1
    return filtered.first
  end

  # Next filter on type
  filtered.first
end

#figure_parent(klass) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rice/doc/doxygen.rb', line 135

def figure_parent(klass)
  # Split namespace
  names = klass.name.split('::')
  # Remove the class
  names.pop

  current_module = Object

  names.each do |name|
    current_module = current_module.const_get(name)
  end

  current_module
end

#get_node(klass, xpath) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/rice/doc/doxygen.rb', line 67

def get_node(klass, xpath)
  node = @cache[klass]
  if node.nil? && !@cache.has_key?(klass)
    node = @cache[klass] = @doc.find_first(xpath)
  end
  node
end

#method_url(klass, native) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rice/doc/doxygen.rb', line 252

def method_url(klass, native)
  node = if klass.instance_of?(Module)
           module_node(klass)
         else
           class_node(klass)
         end

  ruby_name = native.name
  method_names = Array.new
  if ruby_name == "initialize" || ruby_name == "initialize_copy"
    method_names << cpp_name(klass).split('::').last
  elsif native_name = OPERATORS[ruby_name]
    method_names << native_name
  elsif native_name = @method_mappings.dig(klass.name, ruby_name)
    method_names << native_name
  elsif ruby_name.match(/\?$/)
    native_name = ruby_name.gsub(/\?$/, "")
    method_names << camelize(native_name, false)
    method_names << camelize(native_name, true)
    method_names << "is#{camelize(native_name, true)}"
  else
    method_names << ruby_name
    method_names << camelize(ruby_name, false)
    method_names << camelize(ruby_name, true)
  end

  pattern = method_names.map do |method_name|
    "name='#{method_name}'"
  end.join(" or ")

  xpath = "member[@kind='function'][#{pattern}]"
  member_node = node&.find_first(xpath)
  if member_node
    url(member_node)
  elsif native.name == "initialize"
    class_url(klass)
  #else
  #STDERR << "'#{native.name}' => '#{camelize(native.name, false)}'," << "\n"
  end
end

#module_node(a_module) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/rice/doc/doxygen.rb', line 75

def module_node(a_module)
  xpath = "//tagfile/compound[@kind='namespace'][name='#{a_module.name.downcase}']"
  result = get_node(a_module, xpath)
  if result
    read_typedefs(a_module, result)
  end
  result
end

#module_url(klass) ⇒ Object



132
133
# File 'lib/rice/doc/doxygen.rb', line 132

def module_url(klass)
end

#read_typedefs(klass, node) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/rice/doc/doxygen.rb', line 114

def read_typedefs(klass, node)
  typedefs = @typedefs[klass.name.split("::").first]
  node.find("member[@kind='typedef']").each do |typedef|
    name = typedef.find_first("name").content
    type = typedef.find_first("type").content
    type.gsub!(/(const|volatile|&|\*| )/, "")
    typedefs[name] = type
  end
end

#singleton_method_url(klass, native) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/rice/doc/doxygen.rb', line 241

def singleton_method_url(klass, native)
  node = class_node(klass)
  member_name = native.name.gsub(/\?$/, "")
  member_name = camelize(member_name, false)
  xpath = "member[@kind='function' and @static='yes'][name='#{member_name}' or name='#{member_name.capitalize}']"
  member_node = node&.find_first(xpath)
  if member_node
    url(member_node)
  end
end

#union_url(union) ⇒ Object



194
195
196
197
198
# File 'lib/rice/doc/doxygen.rb', line 194

def union_url(union)
  node = class_node(union)
  url_node = node.find_first('filename')
  "#{OPENCV_ROOT}/#{url_node.content}"
end

#url(member_node) ⇒ Object



52
53
54
55
56
57
# File 'lib/rice/doc/doxygen.rb', line 52

def url(member_node)
  # Build url
  anchor_file = member_node.find_first('anchorfile').content
  anchor = member_node.find_first('anchor').content
  "#{@root}/#{anchor_file}##{anchor}"
end