Created
February 10, 2017 07:58
-
-
Save erd0s/1d56b8ca6e0455264a7dbd79074bd0b7 to your computer and use it in GitHub Desktop.
Extract railway codes (Blake's better method)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var setValue = function (row, col, value) { | |
records[row][col] = value; | |
// TESTING ONLY | |
var td = testTable.querySelector('tr:nth-child(' + (row + 1) + ') > td:nth-child(' + (col + 1) + ')'); | |
td.innerText = value; | |
// EOF TESTING | |
}; | |
// http://www.railwaycodes.org.uk/crs/CRSa.shtm | |
var table = document.querySelector('table div table'); | |
var rows = table.querySelectorAll('tbody tr'); | |
var records = []; | |
// TESTING ONLY | |
table.parentNode.parentNode.parentNode.parentNode.parentNode.setAttribute('width', '100%'); | |
table.parentNode.style.height = 'auto'; | |
table.parentNode.style.width = 'auto'; | |
table.parentNode.style.overflow = 'visible'; | |
table.setAttribute('width', '50%'); | |
table.style.float = 'left'; | |
var testTable = document.createElement('table'); | |
testTable.setAttribute('cellspacing', '0'); | |
testTable.setAttribute('cellpadding', '1'); | |
testTable.setAttribute('border', '1'); | |
testTable.setAttribute('width', '50%'); | |
testTable.style.float = 'right'; | |
table.parentNode.appendChild(testTable); | |
var ignoreEasyRows = true; | |
var offset = 0; // 9 for first rowspan, 0 to do ALL | |
var limit = 4; | |
// EOF TESTING | |
rows.forEach(function(tr, i) { | |
// TESTING ONLY | |
if (offset && (i < offset || i > offset + limit)) return; | |
var tr = document.createElement('tr'); | |
testTable.appendChild(tr); | |
for (var j = 0; j < 6; j++) { | |
var td = document.createElement('td'); | |
td.appendChild(document.createTextNode("\u00A0")); | |
tr.appendChild(td); | |
} | |
// EOF TESTING | |
records.push(new Array(6)); | |
}); | |
rows.forEach(function(tr, i) { | |
// TESTING ONLY | |
if (offset && (i < offset || i > offset + limit)) return; | |
i -= offset; | |
var isEasyRow = true; | |
// EOF TESTING | |
var row = []; | |
var tds = tr.querySelectorAll('td'); | |
Array.prototype.forEach.call(tds, function (td, c) { | |
if (tds.length === 6) { | |
// All cells | |
if (td.rowSpan && td.rowSpan > 1) { | |
// TESTING ONLY | |
isEasyRow = false; | |
// EOF TESTING | |
for (var j = 1; j < td.rowSpan; j++) { | |
setValue(i+j, c, td.innerText); | |
} | |
} | |
if (td.colSpan && td.colSpan > 1) { | |
// TESTING ONLY | |
isEasyRow = false; | |
// EOF TESTING | |
for (var j = 1; j < td.colSpan; j++) { | |
setValue(i, c+j, td.innerText); | |
} | |
} | |
setValue(i, c, td.innerText); | |
} else { | |
for (var j = 0; j < 6; j++) { | |
if (typeof records[i][j] === 'undefined') { | |
setValue(i, j, td.innerText); | |
break; | |
} | |
} | |
} | |
}); | |
// TESTING ONLY | |
if (tds.length === 6) { | |
if (ignoreEasyRows && isEasyRow) { | |
tr.style.display = 'none'; | |
var testTr = testTable.querySelector('tr:nth-child(' + (i + 1) + ')'); | |
testTr.style.display = 'none'; | |
} | |
} | |
// EOF TESTING | |
}); | |
var recordsString = JSON.stringify(records); | |
console.log(recordsString); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment