Skip to content

Instantly share code, notes, and snippets.

@finalchild
Last active July 28, 2018 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalchild/54b874731f22f3ceb6cbe3f2e1ac8e40 to your computer and use it in GitHub Desktop.
Save finalchild/54b874731f22f3ceb6cbe3f2e1ac8e40 to your computer and use it in GitHub Desktop.
Hangaram High School timetable processor
function downloadTimetable(timetableElement) {
const timetable = makeTimetable(timetableElement);
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 makeTimetable(timetableElement) {
return processGrid(makeGrid(timetableElement));
}
function makeGrid(timetableElement) {
const result = [];
for (let i = 0; i < timetableElement.rows.length; i++) {
result[i] = [];
}
for(let i = 0; i < timetableElement.rows.length; i++) {
const row = timetableElement.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 = [];
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++) {
dayOfTheWeekResult[period - 1] = toClassArray(getClassString(grid, grade, lectureClass, dayOfTheWeek, period));
}
lectureClassResult[dayOfTheWeek - 1] = dayOfTheWeekResult;
}
gradeResult[lectureClass - 1] = lectureClassResult;
}
result[grade - 1] = gradeResult;
}
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) {
if (classString === '') {
return [];
}
if (classString === '자율(자치, 보건) 봉사') {
return [{
'subject': '자율(자치, 보건) 봉사',
'teacher': '담임'
}];
}
if (classString === '동아리/행사') {
return [{
'subject': '동아리/행사',
'teacher': '담임'
}];
}
const result = classString.split(/\/(?![^()]*(?:\([^()]*\))?\))/);
if (result.length === 2 && result[1].startsWith('B')) {
result[1] = result[0].split('A')[0] + result[1];
}
return result.map(e => {
const split = e.split('(');
return {
'subject': split[0],
'teacher': split[1].split(')')[0]
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment