get properties state and tags
This commit is contained in:
parent
7bf94e84c8
commit
8bef6d4d6a
|
@ -4,8 +4,8 @@
|
|||
*/
|
||||
// const fileToParse = process.argv[0]
|
||||
import fs from 'node-fs';
|
||||
|
||||
const sourceFilePath = './sources/demo.org';
|
||||
const sourceFileName = 'demo.org'
|
||||
const sourceFilePath = './sources/'+sourceFileName;
|
||||
|
||||
console.log('parse some org file', sourceFilePath)
|
||||
if (!sourceFilePath) {
|
||||
|
@ -32,6 +32,22 @@ fs.stat(sourceFilePath, function (err, stat) {
|
|||
let keyword = 'SOMEDAY';
|
||||
let keywordList = ['SOMEDAY', 'NEXT','TODO', 'CANCELLED','DONE', 'WAITING'];
|
||||
|
||||
let task = {
|
||||
header : "",
|
||||
level : "",
|
||||
content : "",
|
||||
state : "",
|
||||
tags : [],
|
||||
tagsInherited : [],
|
||||
dates : {
|
||||
'CREATED':'',
|
||||
'REFILED':'',
|
||||
'DONE':'',
|
||||
},
|
||||
properties : {},
|
||||
}
|
||||
let currentTask = Object.create(task);
|
||||
|
||||
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
||||
if (err) {
|
||||
return console.log(err);
|
||||
|
@ -46,9 +62,32 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
|||
|
||||
|
||||
if (line.match(/^\*+? /)) {
|
||||
// add last task to export list
|
||||
tasksObjectsForJsonExport.push(currentTask)
|
||||
// create a new task
|
||||
currentTask = Object.create(task);
|
||||
headers.push(line)
|
||||
keywordList.forEach(keyword => lookForKeywordInLine(line, keyword))
|
||||
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;
|
||||
}
|
||||
|
||||
})
|
||||
// TODO gérer la création d'objets définissant les tâches et leurs propriétés
|
||||
|
||||
}
|
||||
})
|
||||
console.log('headers', headers)
|
||||
|
@ -57,20 +96,17 @@ fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
|||
console.log('nombre de headers', headers.length)
|
||||
keywordList.forEach(keyword => console.log('nombre de headers',keyword, headersByKind[keyword]?.length))
|
||||
|
||||
writeJsonFile();
|
||||
console.log('tasksObjectsForJsonExport', tasksObjectsForJsonExport)
|
||||
// writeJsonFile('export_'+sourceFileName +'.json' , JSON.stringify(tasksObjectsForJsonExport));
|
||||
|
||||
return;
|
||||
})
|
||||
|
||||
function lookForKeywordInLine(line, keyword='TODO') {
|
||||
|
||||
// const regexp = new RegExp("(^\*+? "+ keyword+"", "gi")
|
||||
// const regexp = new RegExp("^{\*}+? "+keyword+".*?", "gi")
|
||||
|
||||
// if ( line.match(regexp)) {
|
||||
if ( line.indexOf('* '+keyword) !== -1) {
|
||||
// console.log('ligne trouvée', keyword)
|
||||
createNewHeaderKind(keyword)
|
||||
if ( line.indexOf('* '+keyword) !== -1) {
|
||||
createNewHeaderKind(keyword)
|
||||
headersByKind[keyword].push(line);
|
||||
}
|
||||
}
|
||||
|
@ -82,6 +118,18 @@ function createNewHeaderKind(keyword) {
|
|||
}
|
||||
|
||||
|
||||
function writeJsonFile(){
|
||||
// TODO write json
|
||||
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!`);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue