forked from leafstorm/railgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
railgen.rb
executable file
·395 lines (328 loc) · 8.43 KB
/
railgen.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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Railgen
#
# Copyright (C) 2011 Matthew Frazier.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'optparse'
require 'haml'
require 'yaml'
def is_a_number?(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end
class RailNetwork
def self.load (data)
network = self.new(data["name"] || "Rail Network")
network.xrange = data["xrange"]
network.zrange = data["zrange"]
data["stations"].each do |n, st|
network.add_station n, st["x"], st["z"], st["notes"]
end
data["lines"].each do |n, ln|
line = network.add_line n, ln["name"], ln["direction"].to_sym, ln["flow"].to_sym, ln["level"].to_sym, ln["type"].to_sym, ln["notes"]
ln["stops"].each do |stop|
if stop.is_a?(Array)
if is_a_number?(stop[0]) and is_a_number?(stop[1])
line.add_stop(stop, nil)
else
line.add_stop(stop[0], stop[1])
end
else
line.add_stop(stop, nil)
end
end
end
network
end
def self.from_file (filename)
data = nil
File.open filename do |io|
data = YAML.load(io)
end
self.load(data)
end
attr_accessor :name, :stations, :lines, :xrange, :zrange
def initialize (name)
@name = name
@stations = {}
@lines = {}
end
def to_s
"#{name}"
end
def inspect
"#<RailNetwork:#{object_id} #{name.inspect}>"
end
def xlength
xrange[1] - xrange[0]
end
def zlength
zrange[1] - zrange[0]
end
def add_line (*args)
line = Line.new(self, *args)
@lines[line.number] = line
line
end
def add_station (*args)
station = Station.new(self, *args)
@stations[station.name] = station
station
end
def get_station (name)
if name.is_a?(String)
if @stations.has_key? (name)
return @stations[name]
else
raise ArgumentError, "No station named #{name}"
end
elsif name.is_a?(Station)
return name
else
raise TypeError, "name must be String or Station"
end
end
def each_station
stations.keys.sort.each do |code|
yield @stations[code]
end
end
def each_line
lines.keys.sort.each do |number|
yield @lines[number]
end
end
def dump
puts "== #{name} =="
each_line do |line|
puts "#{line.number} - #{line.name} (#{line.html_id})"
puts " ↓ = #{line.down_direction}, ↑ = #{line.up_direction}"
line.each_stop do |st, landing|
puts " #{st.name} (#{st.coords}, #{landing || 'nil'})"
end
puts
end
each_station do |st|
puts "#{st.name} (#{st.html_id})"
puts " #{st.notes || 'nil'}"
st.each_line do |line, landing|
puts " #{line.number} - #{line.name} (#{landing || 'nil'})"
end
puts
end
end
end
# In the form [start to end, end to start]
DIRECTIONS = {
:north => ["Northbound", "Southbound"],
:east => ["Eastbound", "Westbound"],
:south => ["Southbound", "Northbound"],
:west => ["Westbound", "Eastbound"],
:out => ["Outbound", "Inbound"],
:in => ["Inbound", "Outbound"],
:dextro => ["Dextro (Clockwise)", "Levo (Counterclockwise)"],
:levo => ["Levo (Counterclockwise)", "Dextro (Clockwise)"]
}
FLOW_TYPES = {
:twoway => "Two-Way",
:oneway => "One-Way",
:loop => "Two-Way Loop",
:onewayloop => "One-Way Loop"
}
LEVELS = {
:underground => "Underground",
:el => "Elevated",
:surface => "Surface"
}
LINE_TYPES = {
:trunk => "Trunk Line",
:majorloop => "Major Loop",
:local => "Local Line",
:citylink => "City Link",
:portallink => "Portal Link",
:branch => "Branch Link"
}
LINE_TYPE_COLORS = {
:trunk => "#ff0000",
:majorloop => "#0000ff",
:local => "#ff8000",
:citylink => "#008000",
:portallink => "#800080",
:branch => "#000080"
}
class Line
attr_accessor :number, :name, :direction, :flow, :level, :type, :notes, :stops
def initialize (network, number, name, direction, flow, level, type, notes)
@network = network
@number = number
@name = name
@direction = direction
@flow = flow
@level = level
@type = type
@notes = notes
@stops = []
end
def to_s
"#{number} - #{name}"
end
def inspect
"#<Line:#{object_id} #{to_s.inspect}>"
end
def html_id
"line-#{number}"
end
def html_link
"##{html_id}"
end
def down_direction
DIRECTIONS[direction][0]
end
def up_direction
DIRECTIONS[direction][1]
end
def level_name
LEVELS[level]
end
def flow_name
FLOW_TYPES[flow]
end
def loop?
[:loop, :onewayloop].include?(flow)
end
def oneway?
[:oneway, :onewayloop].include?(flow)
end
def type_name
LINE_TYPES[type]
end
def color
LINE_TYPE_COLORS[type]
end
def add_stop (stop, landing)
if !stop.is_a?(Array)
stop = @network.get_station stop
else
stop = Station.new(@network, '', stop[0], stop[1], nil)
end
@stops << [stop, landing]
stop.add_line(self, landing)
end
def each_stop
@stops.each do |s|
yield s[0], s[1]
end
end
end
class Station
attr_accessor :name, :notes, :lines
def initialize (network, name, x, z, notes)
@network = network
@name = name
@x = x
@z = z
@notes = notes
@lines = []
end
def to_s
@name
end
def inspect
"#<Station:#{object_id} #{name.inspect}>"
end
def slug
name.downcase.gsub(/[^a-z0-9]+/i, '-')
end
def html_id
"station-#{slug}"
end
def html_link
"##{html_id}"
end
def scalecoord (coord, factor=1)
factor == 1 ? coord : (coord.to_f / factor).round
end
def x (scale=1)
scalecoord(@x, scale)
end
def z (scale=1)
scalecoord(@z, scale)
end
def coords
"x = #{x}, z = #{z}"
end
def rel_x (scale=1)
scalecoord(-(@network.xrange[0]) + @x, scale)
end
def rel_z (scale=1)
scalecoord(-(@network.zrange[0]) + @z, scale)
end
def add_line (line, landing)
line = @network.lines[line] if line.is_a?(Integer)
@lines << [line, landing]
end
def each_line
@lines.sort_by {|ll| ll[0].number}.each do |ll|
yield ll[0], ll[1]
end
end
end
class RenderContext
def initialize (network, stylesheet)
@network = network
@stylesheet = stylesheet
end
end
def render_template(template_name, network, stylesheet)
haml = Haml::Engine.new(File.read(template_name))
haml.render RenderContext.new(network, stylesheet)
end
if __FILE__ == $0
options = {:template => "templates/listing.haml", :output => nil,
:style => "rail-style.css"}
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: ruby railgen.rb [OPTIONS] DATAFILE"
opts.on("-t", "--template FILE", "Use this Haml template") do |t|
options[:template] = t
end
opts.on("-o", "--output FILE", "Write output to this HTML file") do |f|
options[:output] = f
end
opts.on("-s", "--stylesheet LINK", "Use this stylesheet reference") do |s|
options[:style] = s
end
opts.on_tail("-h", "--help", "Display help") do
puts opts
exit
end
end
option_parser.parse!
data = ARGV[0]
unless data
puts option_parser
exit
end
template = options[:template]
output = (options[:output] or File.basename(template).gsub("haml", "html"))
network = RailNetwork.from_file(data)
File.open(output, 'w') do |io|
io.write render_template(template, network, options[:style])
end
end