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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/my_scripts/scripts/msdn/msdn_helper.rb', line 20
def self.convert(text, page_margin=6, page_width=110)
text.gsub! /(^\s*|\r)/m, ""
syntax = text[/^Syntax.*?(}|\)).*?;/m]
if syntax =~ /typedef struct/ type = :struct
name = syntax.match(/}\s*([\w*]*)/m)[1] params = syntax.scan(/\s(\w*)\s # member type is a first word, followed by whitespace
(\*?[\w]*) # followed by member name
(?:\[([\w]*)\])? # POSSIBLY followed by [array dimension] ( it may be number or CONST)
(?:\s*?;)/mx)
ffi_params = params.map{|p| ":#{p[1].snake_case}, " + (p[2] ? "[:#{p[0]}, #{p[2]}]" : ":#{p[0]}")}.
join(",\n ")
ffi_syntax = "\nclass #{name} < FFI::Struct\n layout #{ffi_params}\nend\n"
original_params = params.map{|p| "#{p[0]} #{p[1]}" + (p[2] ? "[#{p[2]}]" : "")}.join("; ")
original_syntax = "[*Typedef*] struct { #{original_params} } #{name};"
elsif syntax =~ /.\(.*\);/m type = :function
returns, name = syntax.match(/(\w*\*?|\w*\*? \w*\*?) (\w*)\(/m)[1..2]
params = syntax.scan(/\s(\w*\*?) (\w*)(?:,|\s*\);)/m)
ffi_params = params.map{|p| ":" + p.first}.join(", ")
ffi_returns = returns =~ /BOOL/ ? ":int8, boolean: true" : ":#{returns}"
ffi_syntax = "function :#{name}, [#{ffi_params}], #{ffi_returns}"
original_params = params.empty? ? 'void' : params.map{|p| p.join(" ")}.join(", ")
original_syntax = "[*Syntax*] #{returns} #{name}( #{original_params} );"
call_syntax = "success = #{name.snake_case}(#{params.map{|p| p.last.snake_case}.join(", ")})"
snake_syntax = "success = #{name.snake_case}(#{params.map{|p| "#{p.last.snake_case}=0"}.join(", ")})"
camel_syntax = "success = #{name}(#{params.map{|p| "#{p.last.snake_case}=0"}.join(", ")})"
text += "\n---\n<b>Enhanced (snake_case) API: </b>"
text += "\n\n:call-seq:\n #{call_syntax}\n \n"
else
type = :unknown
return 'Unknown syntax. Only functions and structs are converted.'
end
params.each {|p| text.gsub!(Regexp.new("^#{p[1]}\n"), "#{p[1]}:: ")}
replace = {
/^Syntax.*?(}|\)).*?;/m => "\n#{original_syntax}",
/^Parameters\s*/m => "\n",
/^Members\s*/m => "\n",
/^Return Value\n\s*/m => "\n*Returns*:: ",
/^Remarks\s*/m => "---\n*Remarks*:\n",
/\[(in|out|in,.?out)\]/ => '<\1>',
}
replace.each {|from, to| text.gsub!( from, to)}
text_width = page_width - page_margin - 2
lines = []
text.split("\n").each do |line|
list_label = line.match(/:: |^\[.*?\] /)
indent = list_label ? list_label.end(0) : 0
first_pattern = Regexp.new ".{1,#{text_width}}(?: |$)"
chunk_pattern = Regexp.new ".{1,#{text_width - indent}}(?: |$)"
if line.length < text_width || indent > text_width || !(first_chunk = line.match(first_pattern))
lines << line
else
lines << first_chunk[0]
line[first_chunk.end(0)..-1].scan(chunk_pattern).each do |chunk|
lines << " " * indent + chunk
end
end
end
text = lines.join("\n")
tab = " " * page_margin
text.gsub! /^(?!##)/m, tab + "# "
text += "\n#{tab}#{ffi_syntax}"
if type == :function
text = "#{tab}##\n#{text}"
description = text[Regexp.new "(?<=(?:^F|f)unction\\s).{1,#{text_width}}(?: |$)"]
text += %Q[
describe "##{name.snake_case}" do
spec{ use{ #{camel_syntax} }}
spec{ use{ #{snake_syntax} }}
it "original api #{description}" do
pending
#{camel_syntax}
end
it "snake_case api #{description}" do
pending
#{snake_syntax}
end
end # describe #{name.snake_case}\n]
end
text
end
|