-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvfl2objc.rb
executable file
·397 lines (358 loc) · 14 KB
/
vfl2objc.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env ruby
class UIView
# r means right, b means bottom
# xref means reference object for x. e.g. [v1]-10-[v2] makes v2.xref=v1, v2.x=10 if v1's size is unknown
attr_accessor :name, :x, :y, :r, :b, :w, :h, :xref, :yref, :rref, :bref, :added_to_list, :centerx, :centery
def code_for_x
return "#{self.xref.to_s.length>0 ? "CGRectGetMaxX(#{self.xref}.frame) + " : ""}#{self.x}"
end
def code_for_right
return "#{self.rref.to_s.length>0 ? "CGRectGetMinX(#{self.rref}.frame)" : "superview.bounds.size.width"} - (#{self.r})"
end
def code_for_y
return "#{self.yref.to_s.length>0 ? "CGRectGetMaxY(#{self.yref}.frame) + " : ""}#{self.y}"
end
def code_for_bottom
return "#{self.bref.to_s.length>0 ? "CGRectGetMinY(#{self.bref}.frame)" : "superview.bounds.size.height"} - (#{self.b})"
end
end
PARAMS = {:VFL => ""}
HASH = {}
LIST = []
#common beginnings and endings of VFL block comment delimiters (ie // --- VFL... )
STARTS_AND_ENDS = [
["\/\/ \-\-\- VFL", "VFL \-\-\-\n"],
["\/\/ VFL begin", "\/\/ VFL end\n"],
["\/\/ begin VFL", "\/\/ end VFL\n"]
]
def get_or_create_view(name)
if HASH[name]
return HASH[name]
else
view = UIView.new
view.name = name
view.added_to_list = false
HASH[name] = view
return view
end
end
def exists(v)
unless v
return false
end
return v.length > 0
end
def parse(vfl)
simpler_vfl = vfl.strip
# make parsing simpler
simpler_vfl.gsub! "|[", "|-["
simpler_vfl.gsub! "][", "]-["
simpler_vfl.gsub! "]|", "]-|"
PARAMS[:VFL] = vfl
lines = simpler_vfl.split("\n")
lines.each do |line|
line.strip!
if line.index("V:")==0
orientation = :vertical
else
orientation = :horizontal
end
if line[/center\[([a-zA-Z0-9_\.]+)\s*(?:\(([a-zA-Z\d]+)[^\)]*\))?\]/]
view = get_or_create_view($1)
if orientation == :horizontal
view.centerx = true
view.w = $2 if $2
else
view.centery = true
view.h = $2 if $2
end
next
end
# puts orientation.to_s
line.gsub!(/^(H|V):/, "")
elements = line.split("-")
position = "" # positon=empty string means position is unknown. If it's unknown we don't allow any calculation on that
ref = ""
treat_element = lambda { |element,reversed|
# puts "element: #{element}"
if element == "|"
position = "0"
elsif element[/^([_a-zA-Z\d]+).*/]
# get number between elements
if exists(position)
position = position + " + " + $1
end
elsif element[/\[([a-zA-Z0-9_\.]+)\s*(?:\(([_a-zA-Z\d\>]+)[^\)]*\))?\]/]
# get element in brackets (possibly with number in paren)
view = get_or_create_view($1)
# puts "treatint #{view.name}"
if orientation == :horizontal
if reversed
view.r = position
view.rref = ref
# puts "setting the right of #{element} to #{position}, ref is #{ref}."
else
view.x = position
view.xref = ref
# puts "setting the left of #{element} to #{position}, ref is #{ref}."
end
if $2 and (not $2.index(">"))
view.w = $2
if exists(position)
position = position + " + " + view.w
end
# puts "#{element} does have a width: #{$2}, position becomes #{position}."
elsif (not $2) and exists(position)
ref = view.name
position = "0"
# puts "#{element} doesn't have a width, position becomes 0 and ref becomes #{ref}."
else
position = ""
# puts "#{element} has no width and can't determine the edge"
end
else # vertical
if reversed
view.b = position
view.bref = ref
else
view.y = position
view.yref = ref
end
if $2 and (not $2.index(">"))
view.h = $2
if exists(position)
position = position + " + " + $2
end
elsif (not $2) and exists(position)
ref = view.name
position = "0"
else
position = ""
end
end
end
# puts "position becomes #{position}"
}
# puts "regular direction (l->r or top->bottom)"
elements.each_with_index do |element, idx|
treat_element.call(element, false)
end
# puts "reversed direction (r->l or bottom->top)"
position = ""
ref = ""
elements.reverse.each_with_index do |element, idx|
treat_element.call(element, true)
end
end
# puts "hash:\n#{HASH}"
end
def add_to_list(position, view)
unless view
return
end
if view.added_to_list
return
end
LIST.insert(position, view)
view.added_to_list = true
if view.xref.to_s.length>0
v2 = HASH[view.xref]
# puts "#{view.name} depends on #{v2.name}, so before adding #{v2.name} before #{view.name}"
add_to_list(LIST.index(view), v2)
end
if view.yref.to_s.length>0
v2 = HASH[view.yref]
# puts "#{view.name} depends on #{v2.name}, so before adding #{v2.name} before #{view.name}"
add_to_list(LIST.index(view), v2)
end
if view.rref.to_s.length>0
v2 = HASH[view.rref]
# puts "#{view.name} depends on #{v2.name}, so before adding #{v2.name} before #{view.name}"
add_to_list(LIST.index(view), v2)
end
if view.bref.to_s.length>0
v2 = HASH[view.bref]
# puts "#{view.name} depends on #{v2.name}, so before adding #{v2.name} before #{view.name}"
add_to_list(LIST.index(view), v2)
end
end
def construct_list
HASH.each {|k, v|
# puts "adding #{k} currently it's #{LIST.collect{|v| v.name}.join("-")}"
add_to_list(LIST.length, v)
}
# puts LIST.collect{|v| v.name}
end
def objc_gen
code = "// --- VFL GENERATED CODE ---\n"
code << "/*\n"
code << PARAMS[:VFL].split("\n").collect{|l| " "+l.strip}.join("\n")
code << "\n */\n{\n"
code << " // You need to predefine superview before this.\n\n CGRect frame;\n\n"
arh = "" # horizontal autoresizing mask
arv = ""
LIST.each { |view|
code << " frame = #{view.name}.frame;\n" # in case user had predefined sizes, or autocalculated sizes
if view.centerx
code << (exists(view.w) ? " frame.size.width = #{view.w};\n" : " // You need to set frame.size.width before this\n")
code << " frame.origin.x = (superview.bounds.size.width - frame.size.width) / 2.0;\n"
arh = "UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin"
elsif exists(view.x) and exists(view.w)
code << " frame.origin.x = #{view.code_for_x};\n"
code << " frame.size.width = #{view.w};\n"
arh = "UIViewAutoresizingFlexibleRightMargin"
elsif exists(view.w) and exists(view.r)
code << " frame.origin.x = #{view.code_for_right} - (#{view.w});\n"
code << " frame.size.width = #{view.w};\n"
arh = "UIViewAutoresizingFlexibleLeftMargin"
elsif exists(view.x) and exists(view.r)
code << " frame.origin.x = #{view.code_for_x};\n"
code << " frame.size.width = #{view.code_for_right} - frame.origin.x;\n"
arh = "UIViewAutoresizingFlexibleWidth"
else
arh = ""
if exists(view.w)
code << " frame.size.width = #{view.w};\n // You need to set frame.origin.x\n"
arh = "?"
end
if exists(view.x)
code << " frame.origin.x = #{view.code_for_x};\n // You need to set frame.size.width\n"
arh = "UIViewAutoresizingFlexibleRightMargin"
end
if exists(view.r)
code << " // You need to set frame.size.width\n frame.origin.x = #{view.code_for_right} - frame.size.width;\n"
arh = "UIViewAutoresizingFlexibleLeftMargin"
end
if arh.length==0
code << " // You need to set frame.size.width\n // You need to set frame.origin.x\n"
arh = "?"
end
end
if view.centery
code << (exists(view.h) ? " frame.size.height = #{view.h};\n" : " // You need to set frame.size.height before this\n")
code << " frame.origin.y = (superview.bounds.size.height - frame.size.height) / 2.0;\n"
arv = "UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin"
elsif exists(view.y) and exists(view.h)
code << " frame.origin.y = #{view.code_for_y};\n"
code << " frame.size.height = #{view.h};\n"
arv = "UIViewAutoresizingFlexibleBottomMargin"
elsif exists(view.h) and exists(view.b)
code << " frame.origin.y = #{view.code_for_bottom} - #{view.h};\n"
code << " frame.size.height = #{view.h};\n"
arv = "UIViewAutoresizingFlexibleTopMargin"
elsif exists(view.y) and exists(view.b)
code << " frame.origin.y = #{view.code_for_y};\n"
code << " frame.size.height = #{view.code_for_bottom} - frame.origin.y;\n"
arv = "UIViewAutoresizingFlexibleHeight"
else
arv = ""
if exists(view.h)
code << " frame.size.height = #{view.h};\n // You need to set frame.origin.y\n"
arv = "?"
end
if exists(view.y)
code << " frame.origin.y = #{view.code_for_y};\n // You need to set frame.size.height\n"
arv = "UIViewAutoresizingFlexibleBottomMargin"
end
if exists(view.b)
code << " // You need to set frame.size.height\n frame.origin.y = #{view.code_for_bottom} - frame.size.height;\n"
arv = "UIViewAutoresizingFlexibleTopMargin"
end
if arv.length==0
code << " // You need to set frame.size.height\n // You need to set frame.origin.y\n"
arv = "?"
end
end
code << " #{view.name}.frame = frame;\n"
code << (arh=="?" ? " // You need to figure out the horizontal autoresizing mask\n" : " #{view.name}.autoresizingMask |= #{arh};\n")
code << (arv=="?" ? " // You need to figure out the vertical autoresizing mask\n" : " #{view.name}.autoresizingMask |= #{arv};\n")
code << " [superview addSubview:#{view.name}];\n"
code << "\n"
}
code << "}\n// --- END OF CODE GENERATED FROM VFL ---\n"
code
end
def transform_raw_code(vfl)
PARAMS[:VFL] = ""
HASH.clear
LIST.clear
parse(vfl)
construct_list
objc_gen
end
def transform_delimited_code(input)
code = input
vfl_start = "/*\n"
vfl_finish = "*/"
last_i_finish = 0
STARTS_AND_ENDS.each do |start, finish|
$stderr.puts "checking blocks between #{start} and #{finish}"
last_i_finish = 0
while true do
i_start = code.index(start, last_i_finish)
break unless i_start
i_finish = code.index(finish, i_start)
break unless i_finish
last_i_finish = i_finish
$stderr.puts "detected block: #{i_start} - #{i_finish}" if $stdout.isatty
i_vfl_start = code.index(vfl_start, i_start)
i_vfl_end = code.index(vfl_finish, i_vfl_start)
$stderr.puts "vfl: #{i_vfl_start} - #{i_vfl_end}" if $stdout.isatty
vfl = code[i_vfl_start+vfl_start.length..i_vfl_end-1]
newcode = transform_raw_code(vfl.strip)
# find indent
indent = ""
i = i_start - 1
while code[i..i]!="\n" do
indent = code[i..i] + indent
i = i - 1
end
lines = newcode.split("\n")
newcode = ""
lines.each_with_index {|line, idx|
if idx==0 or line.strip.length==0
newcode << line + "\n"
else
newcode << (indent + line) + "\n"
end
}
code[i_start..i_finish+finish.length-1] = newcode
end
end
code
end
def update_file(path)
# start = "\/\/ \-\-\- VFL"
# finish = "VFL \-\-\-\n"
File.open(path, "r") do |f|
code = f.read
code = transform_delimited_code(code)
File.open(path, "w") do |f|
f.write(code)
end
end
end
if __FILE__ == $0
if ARGV[0]=="-h"
$stderr.puts "Usage: #{$0} [-r|--raw]|[-f filename]"
$stderr.puts
$stderr.puts " -r, --raw Treat standard input as raw VFL without comment delimiters"
$stderr.puts " -f filename Transform a file IN PLACE"
$stderr.puts
$stderr.puts "With no file specified, will transform standard input and output to standard output"
elsif ARGV[0]=="-f"
update_file(ARGV[1])
else
if ARGV[0]=="--raw" or ARGV[0]=="-r"
mode = :raw
else
mode = :delimited
end
str = STDIN.read
if mode == :raw
puts transform_raw_code(str)
else
puts transform_delimited_code(str)
end
end
end