-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmlify-csv.js
143 lines (112 loc) · 3.73 KB
/
htmlify-csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
const fs = require('fs');
const parse = require('papaparse');
const app = require('caporal');
// Config parameters
const config = {
outputFile: 'datas/output.html',
};
app
.version('1.0.1')
.description('htmlify-csv')
.command('convert', 'Convert CSV file to HTML table')
.argument('<file>', 'Input file (CSV) to render in HTML')
.option('--delimiter <delimiter>', 'CSV delimiter', /(.*)/, ',')
.option('--output <output>', 'Output filename', /(.*)/, config.outputFile)
.action((args, options) => {
// Define output filename
let { outputFile } = config;
if (options.output) {
outputFile = options.output;
}
// Read CSV file
fs.readFile(args.file, 'utf8', (err, datas) => {
// Parse CSV file
const results = parse.parse(datas, {
delimiter: options.delimiter,
skipEmptyLines: true,
});
const output = results.data;
const columns = output[0];
// Create HTML's table structure
let html = '';
html += '<table class="tablesorter">\n';
html += '\t<thead>\n';
html += '\t\t<tr>\n';
// Columns
for (let i = 0; i < columns.length; i++) {
html += `\t\t\t<th>${columns[i]}</th>\n`;
}
html += '\t\t</tr>\n';
html += '\t</thead>\n';
html += '\t<tbody>\n';
// Body
for (let i = 1; i < output.length; i++) {
const rows = output[i];
const duration = parseFloat(rows[3]);
const maxAllowed = parseFloat(rows[4]);
if (!Number.isNaN(maxAllowed)) {
html += '\t\t<tr>\n';
for (let j = 0; j < rows.length; j++) {
if (Number.isNaN(duration) || duration > maxAllowed) {
html += `\t\t\t<td style="color: red; font-weight: bold;">${rows[j]}</td>\n`;
} else {
html += `\t\t\t<td>${rows[j]}</td>\n`;
}
}
html += '\t\t</tr>\n';
}
}
html += '\t</tbody>\n';
html += '</table>\n';
// Create full HTML code
const generatedOutput = createTemplate(html);
// Write HTML code in output file
fs.writeFile(outputFile, generatedOutput, (err) => {
if (err) {
console.log(err);
}
});
});
});
app.parse(process.argv);
// Creates HTML's code
function createTemplate(table) {
let html = '';
html += `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>CSV to HTML table - htmlify-csv</title>
<link href="assets/theme.blue.css" rel="stylesheet">
<style>
html { font-family: Arial; font-size: 12px; }
footer { font-size: 11px; }
</style>
</head>
<body>
`;
html += table;
html += `
<footer>
<p>Made by <a href="https://github.com/shevabam/htmlify-csv">htmlify-csv.js</a></p>
</footer>
<script src="assets/jquery.min.js"></script>
<script src="assets/jquery.tablesorter.min.js"></script>
<script src="assets/jquery.tablesorter.widgets.min.js"></script>
<script>
$(document).ready(function(){
$('table').tablesorter({
theme: 'blue',
widgets: ["zebra", "filter"],
widgetOptions : {
filter_columnFilters: true
}
});
});
</script>
</body>
</html>`;
return html;
}