Skip to content

Commit aab073d

Browse files
authored
Merge pull request #50 from tiennou/feature/link-types
Link more types in the documentation
2 parents 853e7b3 + 0bc30b6 commit aab073d

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

lib/docurium.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,23 +474,34 @@ def group_functions!(data)
474474
end
475475

476476
def find_type_usage!(data)
477-
# go through all the functions and callbacks and see where other types are used and returned
477+
# go through all functions, callbacks, and structs
478+
# see which other types are used and returned
478479
# store them in the types data
479480
h = {}
480481
h.merge!(data[:functions])
481482
h.merge!(data[:callbacks])
482-
h.each do |func, fdata|
483+
484+
structs = data[:types].find_all {|t, tdata| (tdata[:type] == :struct and tdata[:fields] and not tdata[:fields].empty?) }
485+
structs = Hash[structs.map {|t, tdata| [t, tdata] }]
486+
h.merge!(structs)
487+
488+
h.each do |use, use_data|
483489
data[:types].each_with_index do |tdata, i|
484-
type = tdata[0]
485-
data[:types][i][1][:used] ||= {:returns => [], :needs => []}
486-
if fdata[:return][:type].index(/#{type}[ ;\)\*]?/)
487-
data[:types][i][1][:used][:returns] << func
490+
type, typeData = tdata
491+
492+
data[:types][i][1][:used] ||= {:returns => [], :needs => [], :fields => []}
493+
if use_data[:return] && use_data[:return][:type].index(/#{type}[ ;\)\*]?/)
494+
data[:types][i][1][:used][:returns] << use
488495
data[:types][i][1][:used][:returns].sort!
489496
end
490-
if fdata[:argline].index(/#{type}[ ;\)\*]?/)
491-
data[:types][i][1][:used][:needs] << func
497+
if use_data[:argline] && use_data[:argline].index(/#{type}[ ;\)\*]?/)
498+
data[:types][i][1][:used][:needs] << use
492499
data[:types][i][1][:used][:needs].sort!
493500
end
501+
if use_data[:fields] and use_data[:fields].find {|f| f[:type] == type }
502+
data[:types][i][1][:used][:fields] << use
503+
data[:types][i][1][:used][:fields].sort!
504+
end
494505
end
495506
end
496507
end

site/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ <h3>Argument in</h3>
212212
<a href="<%= fun.url %>"><%= fun.name %></a>.
213213
<% } %> <!-- if we have 'needs' -->
214214

215+
<% if (fields.length > 0) { %>
216+
<h3>Field in</h3>
217+
<% _.each(_.initial(fields), function(fun) { %>
218+
<a href="<%= fun.url %>"><%= fun.name %></a>,
219+
<% }) %><!-- loop over each 'field' -->
220+
<% var fun = _.last(fields) %>
221+
<a href="<%= fun.url %>"><%= fun.name %></a>
222+
<% } %><!-- if we have 'fields' -->
223+
215224
<div class="fileLink">
216225
Defined in:
217226
<a href="<%= fileLink.url %>"><%= fileLink.name %></a>

site/js/docurium.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,22 +352,28 @@ $(function() {
352352
var tname = tdata[0]
353353
var data = tdata[1]
354354

355-
var toPair = function(fun) {
355+
var toFuncPair = function(fun) {
356356
var gname = this.groupOf(fun)
357357
var url = '#' + functionLink(gname, fun, version)
358358
return {name: fun, url: url}
359359
}
360360

361-
var returns = _.map(data.used.returns, toPair, docurium)
362-
var needs = _.map(data.used.needs, toPair, docurium)
361+
var toTypePair = function(type) {
362+
var url = '#' + typeLink(type, version)
363+
return {name: type, url: url}
364+
}
365+
366+
var returns = _.map(data.used.returns, toFuncPair, docurium)
367+
var needs = _.map(data.used.needs, toFuncPair, docurium)
368+
var fields = _.map(data.used.fields, toTypePair, docurium)
363369
var fileLink = {name: data.file, url: docurium.github_file(data.file, data.line, data.lineto)}
364370

365371
// Hot link our field types
366372
data.fields = _.map(data.fields, function(field) {
367373
return {type: this.hotLink(field.type), name: field.name, comments: field.comments}
368374
}, docurium)
369375

370-
this.set('data', {tname: tname, data: data, returns: returns, needs: needs, fileLink: fileLink})
376+
this.set('data', {tname: tname, data: data, returns: returns, needs: needs, fields: fields, fileLink: fileLink})
371377
}
372378
})
373379

@@ -594,18 +600,17 @@ $(function() {
594600
types = this.get('data')['types']
595601
var version = this.get('version')
596602

597-
for(var i=0; i<types.length; i++) {
598-
type = types[i]
599-
typeName = type[0]
600-
typeData = type[1]
601-
re = new RegExp(typeName + '\\s', 'gi');
603+
_.each(types, function(type) {
604+
var typeName = type[0];
605+
var typeData = type[1];
606+
var re = new RegExp('\\b' + typeName + '\\b', 'gi');
602607
var link = $('<a>').attr('href', '#' + typeLink(typeName, version)).append(typeName)[0]
603608
text = text.replace(re, link.outerHTML + ' ')
604-
}
609+
});
605610

606611
var callbacks = this.get('data')['callbacks']
607612
_.each(callbacks, function(cb, typeName) {
608-
re = new RegExp(typeName + '$', 'gi');
613+
var re = new RegExp(typeName + '$', 'gi');
609614
var link = $('<a>').attr('href', '#' + functionLink('callback', typeName, version)).append(typeName)[0]
610615
text = text.replace(re, link.outerHTML + ' ')
611616
});

test/docurium_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_can_extract_structs_and_enums
5959
end
6060

6161
def test_can_find_type_usage
62-
oid = @data[:types].assoc('git_oid')
62+
oid = @data[:types].find {|a| a[0] == 'git_oid' }
6363
oid_returns = [
6464
"git_commit_id",
6565
"git_commit_parent_oid",

0 commit comments

Comments
 (0)