Skip to content

Instantly share code, notes, and snippets.

@foobartel
Created December 9, 2021 08:58
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 foobartel/7b20a081ae22adafa76cdaef568c9621 to your computer and use it in GitHub Desktop.
Save foobartel/7b20a081ae22adafa76cdaef568c9621 to your computer and use it in GitHub Desktop.
Create Kirby pages from CSV file
# csv2file
This script converts the content of a CSV file into a folder structure with TXT files as their content ready to use in Kirby.
# Example Input/Output
**CSV file input:**
`field1; field2; field3; field4`
**Output directories and files:**
```
output
|__ field2
|__ article.txt
```
The content of article.txt will result in:
```
Date: field1
----
Title: field2
----
Teaser: field3
----
Text: field4
----
```
# Installation
1. Clone or download this repo
2. run `npm install` in your csv2file directory
# Usage
run `node index.js <csvFile> [comma separated header]`
If no "comma separated header" is supplied, the script will use the 4 default headers: Date,Title,Teaser,Text
The supplied headers don't neccessarily have to match the amount of fields in your CSV file.
# Usage Example
run `node index.js sample.csv date-published, headline, teaser-text, body-content`
or using the default values:
run `node index.js sample.csv`
var csv = require('csv-parser');
var fs = require('fs');
var path = require('path');
var rmdir = require('rmdir');
var dateFormat = require('dateformat');
var unidecode = require('unidecode');
var slug = require('slug');
var outputDir = 'output';
var filename = 'default.txt';
var fieldSeparator = '\n----\n\n';
var headers = ['Timestamp','Title','Text','Random'];
var headerSeparator = ',';
var dispDateFormat = "yyyy-mm-dd";
function removeOutput(cb, csvFile, columnCount) {
rmdir(outputDir, function(err, dirs, files){
fs.mkdir(outputDir, function() {
// empty function for callback
});
cb(csvFile, columnCount);
});
}
function generateHeader(headerList) {
if (headerList) {
headers = headerList.split(',');
}
}
function parseCsv(csvFile, columnCount) {
generateHeader(columnCount);
var count = 0;
fs.createReadStream(csvFile)
.pipe(csv({
separator: headerSeparator,
headers: headers
}))
.on('data', function(data) {
count++;
console.log('Dispatched count: ' + count);
parseRow(count, data);
});
}
function getFolderName(outputDir, counter, rawDirBase) {
var dirBase = slug(rawDirBase, {lower: false, remove: /[.]/g}); // even though valid, remove dots completely
//dirBase = dirBase.replace(/-$/g, ''); // strip "-" for names that end with "-"
return path.join(outputDir, counter + '_' + dirBase);
}
function unixTimestamp2jsDate(unixTs) {
//return new Date(unixTs *1000)
}
function formatDate(date) {
//return dateFormat(date, dispDateFormat);
}
function formatRowData(row, i, fieldName, needFieldSeparator) {
var value = row[fieldName].replace(/^["'](.+)["'']$/,'$1');
var content = fieldName + ": " + value + '\n';
if (needFieldSeparator) {
content += fieldSeparator;
}
return content;
}
function parseRow(counter, row) {
var lastHeaderIndex = headers.length-1;
//row[headers[0]] = formatDate(unixTimestamp2jsDate(row[headers[0]]));
var dir = getFolderName(outputDir, counter, row[headers[1]]);
fs.mkdir(dir, function(err){
if (err) {
console.log("Failed to create folder: " + dir);
console.log(err);
} else {
var file = path.join(dir, filename);
var content = '';
for (var i = 0; i < headers.length; i++) {
content += formatRowData(row, i, headers[i], (i < lastHeaderIndex));
}
fs.writeFile(file, content, function(err){
if (err) {
console.log("Failed to create file: " + file);
console.log(err);
}
});
}
});
}
if (process.argv[2]) {
removeOutput(parseCsv, process.argv[2], process.argv[3]);
} else {
console.log('Usage: node index.js <csvFile> [comma separated header]');
console.log('if no "comma separated header" then will use the default headers: ' + headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment