Skip to content

Commit

Permalink
Allow spaces as vertical separators when parsing
Browse files Browse the repository at this point in the history
Checks if spaces are being used as vertical separators if no other separators are found.
Corrected the recursion to remove the first element from the array each iteration.
Combined notifications so only one alert box is ever shown when the user uses the parse functionality.
  • Loading branch information
dwesely committed Sep 4, 2016
1 parent 672019d commit b0de77e
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ function parseTableClick() {
}

function parseTable(table) {
// Parse the Output table and write the separated values to the Input text area
var separator = $('#separator').val();
var notifications = "";

if (separator == "") {
//Default separator is the tab
Expand All @@ -373,21 +375,34 @@ function parseTable(table) {

// Identify column separators
var colIndexes = [];

for (var j = 0; j < longest.length; j++) {
if (isColumnSeparator(lines.slice(), j)) {
colIndexes.push(j);
}
}

if (colIndexes.length < 2) {
alert("No results parsed. Whitespace is not yet parsable as a column separator.");
return lines.join('\n');
// Try spaces as a separator
notifications += ("\nNo vertical separators identified, using spaces as a separator.");
colIndexes = [];
for (var j = 0; j < longest.length; j++) {
if (isColumnSeparator(lines.slice(), j, " ")) {
colIndexes.push(j);
}
}
if (colIndexes.length < 2) {
notifications += ("\nNo results parsed. No column separators were found.");
alert(notifications);
return lines.join('\n');
}
} else if (colIndexes.length >= longest.length) {
alert("No results parsed. Single lines are not yet parsable.");
notifications += ("\nNo results parsed. Single lines are not yet parsable.");
alert(notifications);
return lines.join('\n');
}

alert("Parsed rows: " + lines.length + ", length: " + longest.length + ", column locations: " + colIndexes);
notifications += ("\nParsed rows: " + lines.length + ", length: " + longest.length + ", column locations: " + colIndexes);

// Loop over all items and extract the data
var result = "";
Expand All @@ -412,24 +427,27 @@ function parseTable(table) {
result += '\n';
}

alert(notifications);
return result;
}

function isColumnSeparator(lines, column) {
function isColumnSeparator(testArray, column, separator) {
// Return true if this column is the same character all the way to the last row
if (lines.length < 2) {
if (testArray.length < 2 || column == 0) {
// Last line in array, must be a valid separator
// First column should always be a separator

return true;
} else {
var thisLine = lines[0];
var nextLine = lines[1];
var thisLine = testArray[0];
var nextLine = testArray[1];
if (column >= thisLine.length) {
// Column is out of range, must not be a separator
return false;
}
if (thisLine[column] == nextLine[column] && thisLine[column] != " ") {
if ( thisLine[column] == nextLine[column] && ( ( thisLine[column] != " " ) || ( thisLine[column] == separator ) ) ) {
// Rows match, check next row down
return isColumnSeparator(lines.splice(0,1), column);
return isColumnSeparator(testArray.splice(1,testArray.length), column, separator);
} else {
// Rows are different, this is not a separator
return false;
Expand Down

1 comment on commit b0de77e

@dwesely
Copy link
Contributor Author

@dwesely dwesely commented on b0de77e Sep 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still in reference to #20, but it still does not parse Wikimedia tables.

Please sign in to comment.