-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse_programs_include.js
202 lines (162 loc) · 5.24 KB
/
parse_programs_include.js
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
$(document).ready(function() {
var allRows = $("table tr");
var groupsInProgram = parseInt(prompt("Колко групи има съответната специалност ?"), 10);
alert("Сега натисни F12 (ако си с chrome)");
var getDay = window.getDay = function(startFromIndex, groupsInProgram) {
var dayRows = [];
var counter = 0;
allRows.each(function(index, item){
if(index >= startFromIndex && counter < groupsInProgram) {
dayRows.push(item);
counter++
}
});
return dayRows;
};
var nextDayStart = (function() {
var current = 1;
return function() {
current = current + groupsInProgram;
return current;
};
})();
var monday = window.monday = function() {
return getDay(1, groupsInProgram);
};
var tuesday = window.tuesday = function() {
return getDay(nextDayStart(), groupsInProgram);
};
var wednesday = window.wednesday = function() {
return getDay(nextDayStart(), groupsInProgram);
};
var thursday = window.thursday = function() {
return getDay(nextDayStart(), groupsInProgram);
};
var friday = window.friday = function() {
// it's friday, friday, friday !
return getDay(nextDayStart(), groupsInProgram);
};
var saturday = window.saturday = function() {
return getDay(nextDayStart(), groupsInProgram);
};
var extractTableData = window.extractTableData = function(rows) {
var result = [];
$(rows).children("td").each(function(index, item){
result.push(item);
});
return result;
};
var extractCellHtml = function(cell) {
cell = $(cell);
var cellLabel = "";
if(cell.has("font").length !== 0) {
cellLabel = cell.children("font").html();
} else {
cellLabel = cell.html();
}
cellLabel = cellLabel.trim().replace(/(\r\n|\n|\r)/gm,"");
return cellLabel;
};
var extractRoomFromlabel = function(cell) {
};
var isCellGroupLabel = window.isCellGroupLabel = function(cell) {
if(_.isUndefined(cell)) {
return false;
}
var htmlContent = $(cell).children("font").html();
if(_.isUndefined(htmlContent)) {
return false;
}
return (htmlContent.indexOf("гр") !== -1);
};
var filterEmptyCells = window.filterEmptyCells = function(row) {
var result = [];
$(row).each(function(index, item) {
// fuck clean code
var td = $(item);
if (td.text !== '' && td.text() !== ' ' && td.html() !== ' ' && index != 0) {
if(!isCellGroupLabel(item)) {
result.push({
index : index,
item : item
});
}
}
});
return result;
};
var fixIndex = window.fixIndex = function(filteredGroup) {
for(var i = 0, n = filteredGroup.length; i < n; i ++) {
for(var j = i + 1; j < n; j++) {
var current = filteredGroup[i];
var next = filteredGroup[j];
// console.log(current, next);
if(_.isUndefined(next)) {
// we have reached the end;
break;
}
next.index = next.index + parseInt($(current.item).attr("colSpan"), 10) - 1;
}
}
return filteredGroup;
};
var getStartTime = window.getStartTime = function(cell, index, offset) {
if(_.isUndefined(offset)) {
offset = 5;
}
// console.log($(cell).attr("colSpan"));
return parseInt(index, 10) + parseInt(offset, 10);
};
var getEndTime = window.getEndTime = function(cell, index, offset) {
return getStartTime(cell, index, offset) + parseInt($(cell).attr("colSpan"), 10);
};
var getType = window.getType = function(cell) {
if($(cell).attr("rowspan") > 1) {
return "lecture"
}
return "lab";
};
var getLabel = function(cellHtml) {
return cellHtml.split("/")[0];
};
var getRoom = function(cellHtml) {
return _.last(cellHtml.split("/"));
}
var testData = function(allGroupsInGivenDay, label) {
console.log("----- TESTING ", label.toUpperCase(), " -----");
_.each(allGroupsInGivenDay, function(item) {
// console.log(item);
var currentGroup = fixIndex(filterEmptyCells(extractTableData($(item))));
var groupWithLabels = currentGroup.map(function(x) {
return {
"label" : getLabel(extractCellHtml(x.item)).trim(),
"room" : getRoom(extractCellHtml(x.item)).trim(),
"start" : getStartTime(x.item, x.index, 5),
"end" : getEndTime(x.item, x.index, 5),
"type" : getType(x.item)
};
}).map(function(item) {
if(item.type === "lecture") {
var roomLecture = _.compact(item.room.split(" "));
item.room = roomLecture.shift();
item.teacher = roomLecture.join(" ");
}
return item;
});
console.log(JSON.stringify(groupWithLabels, null, 4));
});
};
testData(monday(), "monday");
testData(tuesday(), "tuesday");
testData(wednesday(), "wednesday");
testData(thursday(), "thursday");
testData(friday(), "friday");
testData(saturday(), "saturday");
// var group1 = extractTableData([0]);
// var g1Filtered = fixIndex(filterEmptyCells(group1));
// var g1StartTimes = g1Filtered.map(function(x) {
// return getStartTime(x.item, x.index, 5);
// });
// console.log(g1Filtered);
// console.log(g1StartTimes);
});