-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelldoc
executable file
·103 lines (93 loc) · 1.7 KB
/
shelldoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
#!/usr/bin/env ruby
class ShellDoc
def initialize(filename)
@filename = File.basename(filename)
@f = File.open(filename, 'r')
@functions = []
@collected = []
end
def process!
parse_header
parse_rest
output
end
def parse_header
l = @f.gets
raise 'Bad shebang detected' unless l =~ /^#!\/bin\/(ba|da|)sh/
@header = []
while l = @f.gets
break unless l =~ /^#\s(.*)$/
@header << $1
end
end
def parse_rest
@f.each_line { |l|
case l
when /^\s*#\s*(.*)$/
@collected << $1
when /^\s*$/
@collected = []
when /([A-Za-z0-9_]+)\(\)/
collect_function $1
end
}
end
def collect_function(name)
desc = []
input = []
output = []
collect_to = desc
@collected.each { |l|
case l
when /Input:$/
collect_to = input
when /Output:$/
collect_to = output
else
collect_to << l
end
}
@functions << {
:name => name,
:description => desc.join("\n"),
:input => input,
:output => output,
}
@collected = []
end
def output
puts <<__EOF__
---
title: #{@filename}
inMenu: true
---
h2. API reference: #{@filename}
#{@header.join("\n")}
__EOF__
@functions.each { |f|
puts "<h3><a name=\"#{f[:name]}\"></a>#{f[:name]}</h3>"
puts
puts f[:description]
puts
output_param_list(f[:input], 'Input')
output_param_list(f[:output], 'Output')
}
puts '--- sidebar,html'
puts '<ul>'
@functions.each { |f|
puts "<li><a href=\"##{f[:name]}\">#{f[:name]}</a></li>"
}
puts '</ul>'
end
def output_param_list(list, name)
return unless list and not list.empty?
puts "h4. #{name}"
puts
list.each { |l|
l.gsub!(/^(.*) - (.*)$/, '@\1@ - \2')
puts "* #{l}"
}
puts
end
end
ShellDoc.new(ARGV[0]).process!