1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-29 00:33:15 +00:00
TiddlyWiki5/core/modules/parsers/csvparser.js
2022-05-08 20:48:33 +01:00

56 lines
1.2 KiB
JavaScript

/*\
title: $:/core/modules/parsers/csvparser.js
type: application/javascript
module-type: parser
The CSV text parser processes CSV files into a table wrapped in a scrollable widget
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var CsvParser = function(type,text,options) {
// Table framework
this.tree = [{
"type": "scrollable", "children": [{
"type": "element", "tag": "table", "children": [{
"type": "element", "tag": "tbody", "children": []
}], "attributes": {
"class": {"type": "string", "value": "tc-csv-table"}
}
}]
}];
// Split the text into lines
var lines = text.split(/\r?\n/mg),
tag = "th";
for(var line=0; line<lines.length; line++) {
var lineText = lines[line];
if(lineText) {
var row = {
"type": "element", "tag": "tr", "children": []
};
var columns = lineText.split(",");
for(var column=0; column<columns.length; column++) {
row.children.push({
"type": "element", "tag": tag, "children": [{
"type": "text",
"text": columns[column]
}]
});
}
tag = "td";
this.tree[0].children[0].children[0].children.push(row);
}
}
this.source = text;
this.type = type;
};
exports["text/csv"] = CsvParser;
})();