-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.rb
178 lines (167 loc) · 5.15 KB
/
writer.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
#Writer: writes the line from the state file:
#And saves the critical date in werustate.txt.
#For testing it can be called with a count in which case it will only read the prescribed number of entries.
#
require 'state.rb' #needed to save the state
require 'nokogiri'
def hourname(hour)
return "12am" if hour == 0
return "#{hour}am" if hour < 11
return "12pm" if hour == 11
return "#{hour-11}pm"
end
def display_lines( lines )
puts '['
lines.each do |line|
puts "[ #{line[0]} , #{line[1]}, #{line[2]} , #{line[3]} ], "
end
puts ']'
end
def display_show(doc, lines, entry )
line = lines[entry]
return 0 if line.nil?
name = line[:name]
height = line[:height]
genre = line[:genre]
time = line[:time]
#remove one pixel for the border
pixelheight = Integer(height.match(/(\d*)px/)[1]) - 1
showdiv( doc, genre, pixelheight, name)
end
def showdiv( doc, genre, pixelheight, name)
doc.div( :class => "#{genre} show" ,
:style => "height: #{pixelheight}"){
doc.text name
}
return pixelheight + 1
end
def write_schedule( lines )
daynames = %w(Sun Mon Tue Wed Thu Fri Sat )
@builder = Nokogiri::HTML::Builder.new { |doc|
doc.html {
doc.head {
doc.title.titleclass "WERU schedule"
doc.link(:rel =>"stylesheet",
:type => "text/css", :href => "weru.css")
doc.link(:rel =>"stylesheet",
:type => "text/css", :href => "base.css")
}
doc.body {
doc.div.heading.thing! {
doc.h1 "WERU Schedule"
}
doc.table {
doc.tr { # top row, with days of week
doc.td(:class => 'day-heading') {
doc.text 'Time'
}
(0..6).each { |day|
doc.td.dayheading {
doc.text daynames[day]
}
}
} #end of top row
hourslist = ( (5..22).each.collect + [0, 1, 2] )
doc.tr { #write the row that is the body of the table
#write first column with hours
doc.td(:class=>'hours'){
#cycle through the hours from 5AM and then to midhnight
hourslist.each { |hour|
doc.div.hoursdiv {
doc.text(hourname(hour))
}
}
} #finished with the hours column
entry = 0
(0..6).each { |day|
doc.td(:class => 'days') {
#puts "going into loop"
first_in_loop = entry; #this is the midnight show
loop do #skip until the 5AM show
line = lines[entry]
break if line.nil?
#p line
time = line[:time]
break if time =~ /^5.*am/
entry += 1
end
#keep track of number of pixels on this page...
pixels = 0;
loop do
pixels += display_show(doc, lines, entry)
#go to next entry and see if we have switched times or finished
entry += 1
line = lines[entry]
break if line.nil?
time = line[:time]
#p line if time =~ /^12.*am/
break if time =~ /^12.*am/
end #finish the day..
pixels += display_show(doc, lines, first_in_loop)
if( pixels < hourslist.length * 60 )
showdiv( doc, "filler", hourslist.length * 60 - pixels, "Filler!")
end
} #finish the td
} #loop through the days
} #finish the big row containig it all
}#finish the table
} #finish the body
} #finish the HTML
} #finish the doc
File.open("public/weru.html","w") do |file|
file.write @builder.to_html
end
end #finished with procedure
def map_genres( genres )
begin
#read the file of show genres, and the genre category
File.open("public/genremap.in", "r" ).readlines.each do |line|
line.chomp!
genre,category = line.split('=')
genres[genre]=category
end
rescue
end
genres.keys.each do |genre|
#puts" #{genre} : #{genres[genre]}\n"
if genres[genre] == 1
puts "Genre not mapped #{genre}\n"
genres[genre]=genre
end
end
genres
end
def get_colormap()
category_colors = Hash.new(:undefined)
begin
File.open("public/categorymap.in", "r").readlines.each do |line|
line.chomp!
#p line
category,color = line.split('=')
category_colors[category] = color
end
rescue
end
category_colors
end
def write_css( genres, category_colors)
File.open("public/weru.css", "w") do |file|
genres.keys.each do |genre|
category = genres[genre]
color = category_colors[category]
if color == :undefined
color = "white"
puts "no color for category #{category}"
end
file.write( ".#{genre} {background-color: #{color}}\n" )
end
end
end
lines,genres = state_restore("werustate.yaml")
#puts lines
#puts genres
write_schedule(lines); p "scheduled"
colormap = get_colormap(); p "colors";# p colormap
genres = map_genres(genres); p "genres";# p genres
write_css(genres,colormap); p "css"
#vim tabstop=2:softtabstop=2:shiftwidth=2:expandtab