This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgenerator.rb
232 lines (202 loc) · 5.29 KB
/
generator.rb
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
class SwiftClass
def initialize(name, attributes = nil)
@name = name
@property = []
@attributes = attributes
end
def addProperty(property)
registeredNames = []
@property.each{|item|
registeredNames.push(item.name)
}
property.each{|item|
if registeredNames.index(item.name) == nil
@property = @property + [item]
registeredNames.push(item.name)
end
}
end
def source
return if @property.length == 0
buf = ""
buf = buf + sprintf($file_header_template, @name.capitalize, Time.now)
buf = buf + "class " + @name.gsub(/\s\(.+?\)/, "").capitalize + " {\n"
@property.each{|p|
buf = buf + p.propertyDeclaration
}
buf = buf + "\n init(json:[String:AnyObject]) {\n"
buf = buf + " var updates = 0\n"
buf = buf + " var properties = 0\n"
@property.each{|p|
buf = buf + p.blockInInit
}
buf = buf + " println(\"update items = \\(updates)/\\(properties)\")\n"
buf = buf + " }\n"
buf = buf + "\n func toString() -> String {\n"
buf = buf + " return \""
@property.each{|p|
buf = buf + p.description
}
buf = buf + "\"\n }\n"
buf = buf + "}\n\n\n"
end
attr_accessor:property, :name, :attributes
end
class SwiftProperty
def initialize(type, name, comment)
@type = type
@name = name
@comment = comment
@typeDictionary = {
"array"=>"[]",
"int"=>"Int",
"long"=>"Int",
"boolean"=>"Bool",
"string"=>"String"
}
@typeDefaultValue = {
"array"=>"[]",
"int"=>"0",
"long"=>"0",
"boolean"=>"false",
"string"=>"\"\""
}
end
def propertyDeclaration()
if @typeDictionary[@type] != nil && @typeDictionary[@type] != "[]"
sprintf($property_template, @comment, @name, @typeDictionary[@type])
else
sprintf($property_template_anyobject, @comment, @name, "[AnyObject]")
end
end
def blockInInit
if @typeDictionary[@type] != nil && @typeDictionary[@type] != "[]"
sprintf($initialize_template, @name, @typeDictionary[@type], @name, @name, @typeDefaultValue[@type])
else
sprintf($initialize_template_comment_out, @name, @typeDictionary[@type], @name, @name, @typeDefaultValue[@type])
end
end
def description
if @typeDictionary[@type] != nil && @typeDictionary[@type] != "[]"
@name + "=>\\(" + @name + ") "
else
""
end
end
attr_accessor:type, :name, :comment
end
def main
implementations = []
classes = []
html = File::open("./source.html").read
p = nil
html.scan(/(<h2>(.+?)<\/h2>)|(<h3>(.+?)<\/h3>)|(<tr>(.+?)<\/tr>)/m).each{|result|
# p result[5]
if result[1] != nil
name = result[1].gsub(/(<.+?>)|\n/, "")
obj = SwiftClass.new(name)
implementations.push(obj)
p = obj
elsif result[3] != nil
name = result[3].gsub(/(<.+?>)|\n/, "")
attributes = ""
if name =~ /(.+?)\s\((.+?)\)/
name = $1
attributes = $2
end
obj = SwiftClass.new(name, attributes)
classes.push(obj)
p = obj
elsif result[5] != nil
elements = result[5].scan(/<td.+?>.+?<\/td>/m)
begin
type = elements[0].gsub(/(<.+?>)|\n/, "").downcase
name = elements[1].gsub(/(<.+?>)|\n/, "")
comment = elements[2].gsub(/(<.+?>)|\n/, "")
p.property.push(SwiftProperty.new(type, name, comment))
rescue
else
end
end
}
thing = nil
votable = nil
created = nil
implementations.each{|element|
if element.name =~ /thing/
thing = element
# implementations.delete(element)
elsif element.name =~ /votable/
votable = element
# implementations.delete(element)
elsif element.name =~ /created/
created = element
# implementations.delete(element)
end
}
implementations.each{|element|
if element.property.length > 0
fw = File::open("./" + element.name.capitalize + ".swift", "w")
fw.write element.source
fw.close
end
}
classes.each{|element|
if element.property.length > 0
if element.attributes == "implements votable | created"
element.addProperty(thing.property)
element.addProperty(created.property)
element.addProperty(votable.property)
elsif element.attributes == "implements created"
element.addProperty(thing.property)
element.addProperty(created.property)
else
element.addProperty(thing.property)
end
fw = File::open("./" + element.name.capitalize + ".swift", "w")
fw.write element.source
fw.close
end
}
end
$file_header_template = <<"EOS"
//
// %s.swift
// reddift
//
// Created by generator.rb via from https://github.com/reddit/reddit/wiki/JSON
// Created at %s
//
import UIKit
EOS
$initialize_template = <<"EOS"
if let temp = json["%s"] as? %s {
self.%s = temp
updates++
}
else {
self.%s = %s
}
properties++
EOS
$initialize_template_comment_out = <<"EOS"
// if let temp = json["%s"] as? %s {
// self.%s = temp
// }
// else {
// self.%s = %s
// }
EOS
$property_template = <<"EOS"
/**
%s
*/
let %s:%s
EOS
$property_template_anyobject = <<"EOS"
/**
%s
*/
// let %s:%s
EOS
main