Skip to content

Instantly share code, notes, and snippets.

@lminiero
Last active November 5, 2019 11:49
Show Gist options
  • Save lminiero/c8b40bbadf57ce61e03aa9d18cbc41b5 to your computer and use it in GitHub Desktop.
Save lminiero/c8b40bbadf57ce61e03aa9d18cbc41b5 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const yaml = require('yaml');
// Read lifeograph diary
var lifeograph = fs.readFileSync('activities.diary.txt', 'utf8'), rednotebook = {};
var lines = lifeograph.split('\n');
// Iterate on lifeograph diary lines
var item = null, content = [], newDate = 0;
for(var i in lines) {
var line = lines[i];
if(newDate === 0 && line !== '---------------------------------------------') {
// Skip all lines until we get to a date
continue;
}
if(line.indexOf('TAGS: ') === 0) {
// Skip tags
continue;
}
if(line === '---------------------------------------------') {
// Date formatter
newDate++;
if(newDate === 3)
newDate = 1;
continue;
}
if(newDate === 1) {
// This is the date of a new diary: process the previous one and go on
if(item !== null)
convert(item, content);
content = [];
item = line;
continue;
}
// If we're here, this is part of the content
content.push(line);
}
if(item !== null)
convert(item, content);
// Generate Rednotebook diaries
for(var my in rednotebook) {
if(!fs.existsSync('rednotebook'))
fs.mkdirSync('rednotebook');
console.log('[' + my + ']: ' + Object.keys(rednotebook[my]).length + ' items');
//~ console.log(rednotebook[my]);
fs.writeFileSync('rednotebook/' + my + '.txt', yaml.stringify(rednotebook[my], { schema: 'failsafe' }), 'utf8');
}
// Helper method to process the diary from a specific date
function convert(itemDate, itemContent) {
if(!itemDate || !itemContent || itemContent.length === 0)
return;
// Parse the date to get year, month and day
var rnbMY = itemDate.replace('.', '-').substring(0, 7);
var rnbDay = parseInt(itemDate.substring(8));
// Create a new object, that will be a Rednotebook month
if(!rednotebook[rnbMY])
rednotebook[rnbMY] = {};
// Each day in the month is a { day: { text: ".." } } object we'll turn to YAML
if(!rednotebook[rnbMY][rnbDay]) {
// First line, initialize
rednotebook[rnbMY][rnbDay] = { text: '' };
}
for(var c in itemContent) {
if(c == 0) {
// The first line in Lifeograph is always the header
rednotebook[rnbMY][rnbDay].text += ('= ' + convertLine(itemContent[0]) + ' =\n');
continue;
}
// Append the other lines, doing some manipulation/translation if needed
rednotebook[rnbMY][rnbDay].text += (convertLine(itemContent[c]) + '\n');
}
}
function convertLine(itemLine) {
var convertedLine = itemLine;
// Lines starting with a space are the beginning of a section in Lifeograph
if(convertedLine.indexOf(' ') == 0 && convertedLine.indexOf(' ') != 0)
convertedLine = ('==' + convertedLine + ' ==');
// Asterisks are used to turn the wrapper text to bold, in Lifeograph: that said,
// lines starting with an asterisk can be interpreted as a subsection in Lifeograph
else if(convertedLine.indexOf('*') == 0) {
var parts = convertedLine.split('*');
convertedLine = ('=== ' + parts[1] + ' ===');
}
else if(convertedLine.indexOf('\t') == 0) {
// [tab] + [asterisk] is an unordered list item
if(convertedLine.indexOf('\t•') == 0) {
convertedLine = convertedLine.replace('\t• ', '- ');
}
// [tab] + [number.] is an ordered list item
else {
for(var i=0; i<100; i++) {
if(convertedLine.indexOf('\t' + i + '. ') == 0)
convertedLine = convertedLine.replace('\t' + i + '. ', '+ ');
}
}
}
return convertedLine;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment