Skip to content

Instantly share code, notes, and snippets.

@finalchild finalchild/processor2.js
Last active Dec 16, 2018

Embed
What would you like to do?
Hangaram Timetable Reader gist
const timetableElement = document.getElementById('timetable');
const roomDataElement = document.getElementById('room-data');
const timetableGrid = makeGrid(timetableElement);
const roomDataGrid = makeGrid(roomDataElement);
const timetable = processGrid(timetableGrid);
//download(timetable[0], 'timetable1.json');
//download(timetable[1], 'timetable2.json');
//download(timetable[2], 'timetable3.json');
function download(o, fileName) {
const blob = new Blob([JSON.stringify(o)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
function makeGrid(tableElement) {
const result = [];
for (let i = 0; i < tableElement.rows.length; i++) {
result[i] = [];
}
for(let i = 0; i < tableElement.rows.length; i++) {
const row = tableElement.rows[i];
let j = 0;
while (typeof result[i][j] !== 'undefined') {
j++;
}
for (let cell of row.children) {
let colspan = parseInt(cell.getAttribute('colspan'), 10);
if (Number.isNaN(colspan)) {
colspan = 1;
}
let rowspan = parseInt(cell.getAttribute('rowspan'), 10);
if (Number.isNaN(rowspan)) {
rowspan = 1;
}
for (let i2 = 0; i2 < rowspan; i2++) {
for (let j2 = 0; j2 < colspan; j2++) {
result[i + i2][j + j2] = cell.textContent;
}
}
j += colspan;
while (typeof result[i][j] !== 'undefined') {
j++;
}
}
}
return result;
}
function processGrid(grid) {
const result = [];
const allSubjects = new Set();
for (let grade = 1; grade <= 3; grade++) {
const gradeResult = [];
for (let lectureClass = 1; lectureClass <= 8; lectureClass++) {
const lectureClassResult = [];
for (let dayOfTheWeek = 1; dayOfTheWeek <= 5; dayOfTheWeek++) {
const dayOfTheWeekResult = [];
for (let period = 1; period <= 6; period++) {
const tmp = toClassArray(getClassString(grid, grade, lectureClass, dayOfTheWeek, period), dayOfTheWeek, period);
for (const clazz of tmp) {
let subjectName = clazz.subject;
if (subjectName.endsWith('A') || subjectName.endsWith('B') || subjectName.endsWith('C') || subjectName.endsWith('D') || subjectName.endsWith('E') || subjectName.endsWith('F') || subjectName.endsWith('G')) {
subjectName = subjectName.substring(0, subjectName.length - 1);
}
allSubjects.add(subjectName);
}
dayOfTheWeekResult[period - 1] = tmp;
}
lectureClassResult[dayOfTheWeek - 1] = dayOfTheWeekResult;
}
gradeResult[lectureClass - 1] = lectureClassResult;
}
result[grade - 1] = gradeResult;
}
console.log(allSubjects);
return result;
}
function getClassString(grid, grade, lectureClass, dayOfTheWeek, period) {
if (dayOfTheWeek === 5 && period === 6) {
period = 5;
}
return grid[2 + (dayOfTheWeek - 1) * 6 + (period - 1)][2 + (grade - 1) * 8 + (lectureClass - 1)];
}
function toClassArray(classString, dayOfTheWeek, period) {
if (classString === '' || classString === ' ' || classString === ' ' || classString.trim().length === 0) {
return [];
}
if (classString === '자율(자치, 보건) 봉사') {
return [{
'subject': '자율(자치, 보건) 봉사',
'teacher': '담임',
'room': '하우스'
}];
}
if (classString === '동아리/행사') {
return [{
'subject': '동아리/행사',
'teacher': '담임',
'room': '하우스'
}];
}
const result = classString.split(/\/(?![^()]*(?:\([^()]*\))?\))/);
if (result.length === 2 && result[1].startsWith('B')) {
result[1] = result[0].split('A')[0] + result[1];
}
if (result == [' ']) return []
return result.map(e => {
const split = e.split('(');
return applyRoomData({
'subject': split[0],
'teacher': split[1].split(')')[0]
}, dayOfTheWeek, period);
});
}
function applyRoomData(classObj, dayOfTheWeek, period) {
if (classObj.teacher.includes('/')) {
return {
'subject': classObj.subject,
'teacher': classObj.teacher,
'room': '???'
}
}
let teacher = classObj.teacher;
let room = '';
for (let i = 3; i < roomDataGrid.length; i++) {
console.log(roomDataGrid[i][2])
if (roomDataGrid[i][2].endsWith(teacher)) {
teacher = roomDataGrid[i][2];
room = roomDataGrid[i][3 + (dayOfTheWeek - 1) * 6 + (period - 1)];
break;
}
}
return {
'subject': classObj.subject,
'teacher': teacher,
'room': room
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.