2023-03-04 23:52:46 +01:00
|
|
|
/**
|
|
|
|
* convertir un fichier .org vers des données structurées en json
|
|
|
|
* @type {*}
|
|
|
|
*/
|
|
|
|
// const fileToParse = process.argv[0]
|
|
|
|
import fs from 'node-fs';
|
2023-03-05 11:05:06 +01:00
|
|
|
const sourceFileName = 'demo.org'
|
|
|
|
const sourceFilePath = './sources/'+sourceFileName;
|
2023-03-04 23:52:46 +01:00
|
|
|
|
|
|
|
console.log('parse some org file', sourceFilePath)
|
|
|
|
if (!sourceFilePath) {
|
|
|
|
console.error('pas de fichier à ouvrir')
|
|
|
|
}
|
|
|
|
let headers = []
|
2023-03-04 23:57:03 +01:00
|
|
|
let tasksObjectsForJsonExport = []
|
2023-03-04 23:52:46 +01:00
|
|
|
let headersByKind = {}
|
|
|
|
|
|
|
|
|
|
|
|
fs.stat(sourceFilePath, function (err, stat) {
|
|
|
|
if (err == null) {
|
|
|
|
console.log(`File ${sourceFilePath} exists`);
|
|
|
|
|
|
|
|
} else if (err.code === 'ENOENT') {
|
|
|
|
// file does not exist
|
|
|
|
console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err);
|
|
|
|
} else {
|
|
|
|
console.log('Some other error: ', err.code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let keyword = 'SOMEDAY';
|
|
|
|
let keywordList = ['SOMEDAY', 'NEXT','TODO', 'CANCELLED','DONE', 'WAITING'];
|
|
|
|
|
2023-03-05 11:05:06 +01:00
|
|
|
let task = {
|
|
|
|
header : "",
|
|
|
|
level : "",
|
|
|
|
content : "",
|
|
|
|
state : "",
|
|
|
|
tags : [],
|
|
|
|
tagsInherited : [],
|
|
|
|
dates : {
|
|
|
|
'CREATED':'',
|
|
|
|
'REFILED':'',
|
|
|
|
'DONE':'',
|
|
|
|
},
|
|
|
|
properties : {},
|
|
|
|
}
|
|
|
|
let currentTask = Object.create(task);
|
|
|
|
|
2023-03-04 23:52:46 +01:00
|
|
|
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
console.log(" parsing...")
|
|
|
|
// parcourir chaque ligne du fichier org
|
|
|
|
let everyline = data.split('\n');
|
|
|
|
|
|
|
|
// trouver les entêtes toutes les lignes qui commencent par * et espace.
|
|
|
|
|
|
|
|
everyline.forEach((line) => {
|
|
|
|
|
|
|
|
|
|
|
|
if (line.match(/^\*+? /)) {
|
2023-03-05 11:05:06 +01:00
|
|
|
// add last task to export list
|
|
|
|
tasksObjectsForJsonExport.push(currentTask)
|
|
|
|
// create a new task
|
|
|
|
currentTask = Object.create(task);
|
2023-03-04 23:52:46 +01:00
|
|
|
headers.push(line)
|
2023-03-05 11:05:06 +01:00
|
|
|
currentTask.header = line;
|
|
|
|
keywordList.forEach(keyword => {
|
|
|
|
lookForKeywordInLine(line, keyword)
|
|
|
|
|
|
|
|
if(line.indexOf('* '+keyword) !== -1){
|
|
|
|
currentTask.state = keyword
|
|
|
|
}
|
|
|
|
// compter les étoiles pour trouver le niveau du header
|
|
|
|
currentTask.level = line.match(/\*/g).length
|
|
|
|
let tagsFound = line.match(/\:(.*)\:/g)
|
|
|
|
console.log('tagsFound', tagsFound)
|
|
|
|
if(tagsFound){
|
|
|
|
tagsFound = tagsFound[0];
|
|
|
|
console.log('tagsFound', tagsFound)
|
|
|
|
tagsFound = tagsFound.split(':').filter(item => item.length )
|
|
|
|
currentTask.tags = tagsFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2023-03-04 23:52:46 +01:00
|
|
|
// TODO gérer la création d'objets définissant les tâches et leurs propriétés
|
2023-03-05 11:05:06 +01:00
|
|
|
|
2023-03-04 23:52:46 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
console.log('headers', headers)
|
|
|
|
console.log(" parsing fini")
|
|
|
|
console.log('nombre de lignes', everyline.length)
|
|
|
|
console.log('nombre de headers', headers.length)
|
|
|
|
keywordList.forEach(keyword => console.log('nombre de headers',keyword, headersByKind[keyword]?.length))
|
|
|
|
|
2023-03-05 11:05:06 +01:00
|
|
|
console.log('tasksObjectsForJsonExport', tasksObjectsForJsonExport)
|
|
|
|
// writeJsonFile('export_'+sourceFileName +'.json' , JSON.stringify(tasksObjectsForJsonExport));
|
2023-03-04 23:57:03 +01:00
|
|
|
|
2023-03-04 23:52:46 +01:00
|
|
|
return;
|
|
|
|
})
|
|
|
|
|
|
|
|
function lookForKeywordInLine(line, keyword='TODO') {
|
|
|
|
|
|
|
|
|
2023-03-05 11:05:06 +01:00
|
|
|
if ( line.indexOf('* '+keyword) !== -1) {
|
|
|
|
createNewHeaderKind(keyword)
|
2023-03-04 23:52:46 +01:00
|
|
|
headersByKind[keyword].push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNewHeaderKind(keyword) {
|
|
|
|
if (!headersByKind[keyword]) {
|
|
|
|
headersByKind[keyword] = [];
|
|
|
|
}
|
2023-03-04 23:57:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-05 11:05:06 +01:00
|
|
|
function writeJsonFile(fileName, fileContent) {
|
|
|
|
console.log('write file ', fileName);
|
|
|
|
return fs.writeFile(
|
|
|
|
`./output/${fileName}`,
|
|
|
|
fileContent,
|
|
|
|
"utf8",
|
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(`Error writing file: ${err}`);
|
|
|
|
} else {
|
|
|
|
console.log(`File ${fileName} is written successfully!`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-03-04 23:52:46 +01:00
|
|
|
}
|