2017-06-02 15:34:42 +00:00
|
|
|
import QtQml 2.0
|
|
|
|
import QOwnNotesTypes 1.0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This script is used for manual interaction with Taskwarrior by either
|
|
|
|
* importing tasks from a certain project, or exporting them from a note.
|
|
|
|
*/
|
|
|
|
QtObject {
|
2017-06-11 14:09:49 +00:00
|
|
|
property string taskPath;
|
|
|
|
property bool verbose;
|
2017-06-11 13:43:20 +00:00
|
|
|
|
|
|
|
property variant settingsVariables: [
|
|
|
|
{
|
|
|
|
"identifier": "taskPath",
|
|
|
|
"name": "Taskwarrior path",
|
|
|
|
"description": "A path to your Taskwarrior instance",
|
|
|
|
"type": "string",
|
|
|
|
"default": "/usr/bin/task",
|
2017-06-11 14:09:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"identifier": "verbose",
|
|
|
|
"name": "Verbose logging",
|
|
|
|
"description": "Should the script log every action?",
|
|
|
|
"type": "boolean",
|
|
|
|
"default": false
|
2017-06-11 13:43:20 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
/**
|
|
|
|
* Initializes the custom actions
|
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
// export selected data to Taskwarrior
|
|
|
|
script.registerCustomAction("exportToTaskwarrior", "Export selected list as Taskwarrior tasks", "Export to Taskwarrior");
|
|
|
|
|
|
|
|
// create a menu entry "Create meeting note" with a button and a freedesktop theme icon
|
2017-06-02 16:41:47 +00:00
|
|
|
script.registerCustomAction("importFromTaskwarrior", "Import tasks from Taskwarrior as a list", "Import from Taskwarrior");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get selected text and separate it by lines.
|
|
|
|
*
|
|
|
|
* @returns array of strings representing separate lines
|
|
|
|
*/
|
|
|
|
function getSelectedTextAndSeparateByNewline() {
|
|
|
|
var text = script.noteTextEditSelectedText();
|
|
|
|
var separatedByLines;
|
|
|
|
// Consider Windows different newline method.
|
|
|
|
if (script.platformIsWindows()) {
|
|
|
|
separatedByLines = text.split('\r\n');
|
|
|
|
} else {
|
|
|
|
separatedByLines = text.split('\n');
|
|
|
|
}
|
|
|
|
return separatedByLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse input string to get the project name and run supplied function if found.
|
|
|
|
*
|
|
|
|
* @param str input string which may contain project name
|
|
|
|
* @param func function to be executed if the project name is found
|
|
|
|
*
|
|
|
|
* @returns boolean for if the project name was detected or not
|
|
|
|
*/
|
|
|
|
function getProjectNameAndRun(str, func) {
|
|
|
|
// We are trying to get the name of the project.
|
|
|
|
// To do so, we are getting the substring of a line by using regexp group.
|
2017-06-11 16:07:06 +00:00
|
|
|
var projectRegExp = /(#+)[\s*]?(.+)?[\s*]?/i;
|
2017-06-02 16:41:47 +00:00
|
|
|
var isProjectName = projectRegExp.exec(str);
|
|
|
|
if (isProjectName) {
|
2017-06-11 16:07:06 +00:00
|
|
|
var projectName = isProjectName[2];
|
|
|
|
var headerLevel = isProjectName[1].length;
|
|
|
|
func(projectName, headerLevel);
|
2017-06-02 16:41:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-02 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 14:09:49 +00:00
|
|
|
function logIfVerbose(str) {
|
|
|
|
if (verbose) {
|
|
|
|
script.log(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
/**
|
|
|
|
* This function is invoked when a custom action is triggered
|
|
|
|
* in the menu or via button
|
|
|
|
*
|
|
|
|
* @param identifier string the identifier defined in registerCustomAction
|
|
|
|
*/
|
|
|
|
function customActionInvoked(identifier) {
|
|
|
|
switch (identifier) {
|
|
|
|
// export selected lines to Taskwarriors as tasks.
|
|
|
|
// The project name will be taken from "Project:" keyword detected in first lines.
|
|
|
|
case "exportToTaskwarrior":
|
|
|
|
|
2017-06-11 14:09:49 +00:00
|
|
|
logIfVerbose("Exporting tasks from a note.");
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
// Starting with an empty default project name.
|
2017-06-11 16:07:06 +00:00
|
|
|
// We are keeping the project name as a array of strings. We will concatenate them to
|
|
|
|
// get the final projectName with nesting.
|
|
|
|
var projectName = [];
|
|
|
|
var referenceHeaderLevel = 0;
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
// For each line, we are gathering data to properly create tasks.
|
2017-06-02 16:41:47 +00:00
|
|
|
getSelectedTextAndSeparateByNewline().forEach(function (line){
|
2017-06-11 16:07:06 +00:00
|
|
|
if (getProjectNameAndRun(line, function (proName, headerLevel) {
|
2017-06-11 14:09:49 +00:00
|
|
|
logIfVerbose("Detected project name: " + proName);
|
2017-06-11 16:07:06 +00:00
|
|
|
logIfVerbose("Detected header level: " + headerLevel);
|
|
|
|
|
|
|
|
if (projectName.length === 0) {
|
|
|
|
referenceHeaderLevel = headerLevel - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (projectName.length + referenceHeaderLevel >= headerLevel) {
|
|
|
|
var i;
|
|
|
|
for (i = projectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) {
|
|
|
|
projectName.pop();
|
2017-06-11 16:45:15 +00:00
|
|
|
if (projectName.length === 0) {
|
|
|
|
referenceHeaderLevel = headerLevel - 1;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-11 16:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
projectName.push(proName);
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
// We expect, that the project name would be the only thing in line, hence `return`.
|
|
|
|
return;
|
2017-06-02 16:41:47 +00:00
|
|
|
})) return;
|
2017-06-02 15:34:42 +00:00
|
|
|
|
|
|
|
// We are trying to get the task description.
|
|
|
|
// It should be started with either - (minus) or * (asterisk) to indicate list item.
|
|
|
|
var taskRegExp = /[\*\-][\s*]?(.+)[\s*]?/;
|
|
|
|
var taskDescription;
|
|
|
|
|
|
|
|
var isTask = taskRegExp.exec(line);
|
|
|
|
if (isTask) {
|
2017-06-11 14:09:49 +00:00
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
taskDescription = isTask[1];
|
2017-06-11 14:09:49 +00:00
|
|
|
logIfVerbose("Detected task: " + taskDescription);
|
2017-06-11 16:07:06 +00:00
|
|
|
var concatenatedProjectName = projectName.join('.');
|
|
|
|
logIfVerbose("Executing \"" + taskPath + " add pro:" + concatenatedProjectName + " " + taskDescription + "\"");
|
2017-06-11 13:43:20 +00:00
|
|
|
script.startDetachedProcess(taskPath,
|
2017-06-02 15:34:42 +00:00
|
|
|
[
|
|
|
|
"add",
|
2017-06-11 16:07:06 +00:00
|
|
|
"pro:" + concatenatedProjectName,
|
2017-06-02 15:34:42 +00:00
|
|
|
taskDescription
|
|
|
|
]);
|
|
|
|
// We expect, that the task description would be the only thing in the line, hence `return`.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2017-06-02 16:41:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "importFromTaskwarrior":
|
|
|
|
// Get selected text to determine the project we want to import from.
|
|
|
|
|
|
|
|
var projectNames = [];
|
2017-06-11 16:45:15 +00:00
|
|
|
var referenceHeaderLevel = 0;
|
2017-06-02 16:41:47 +00:00
|
|
|
|
|
|
|
getSelectedTextAndSeparateByNewline().forEach(function (line){
|
2017-06-11 16:07:06 +00:00
|
|
|
if (getProjectNameAndRun(line, function (proName, headerLevel) {
|
2017-06-11 16:45:15 +00:00
|
|
|
if (projectNames.length === 0) {
|
|
|
|
logIfVerbose("No project detected yet. Inserting " + proName)
|
|
|
|
projectNames.push([proName]);
|
|
|
|
logIfVerbose("Reference header level set to " + headerLevel)
|
|
|
|
referenceHeaderLevel = headerLevel - 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newProjectName = projectNames[projectNames.length - 1].slice();
|
|
|
|
logIfVerbose("Last project name inserted was " + newProjectName.join('.'));
|
|
|
|
if (newProjectName.length + referenceHeaderLevel >= headerLevel) {
|
|
|
|
logIfVerbose("Same header level detected.");
|
|
|
|
var i;
|
|
|
|
for (i = newProjectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) {
|
|
|
|
newProjectName.pop();
|
|
|
|
if (newProjectName.length === 0) {
|
|
|
|
referenceHeaderLevel = headerLevel - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newProjectName.push(proName);
|
|
|
|
projectNames.push(newProjectName);
|
|
|
|
logIfVerbose("Project name detected. Inserted value is " + newProjectName.join('.'))
|
|
|
|
|
2017-06-02 16:41:47 +00:00
|
|
|
})) return;
|
|
|
|
});
|
|
|
|
|
|
|
|
// To avoid overwriting what we have selected, we are simply writing it
|
|
|
|
script.noteTextEditWrite(script.noteTextEditSelectedText());
|
|
|
|
|
|
|
|
projectNames.forEach( function(projectName) {
|
2017-06-11 16:45:15 +00:00
|
|
|
var concatenatedProjectName = projectName.join('.');
|
2017-06-11 13:43:20 +00:00
|
|
|
var result = script.startSynchronousProcess(taskPath,
|
2017-06-02 16:41:47 +00:00
|
|
|
[
|
2017-06-11 16:45:15 +00:00
|
|
|
"pro.is:" + concatenatedProjectName,
|
2017-06-11 13:43:20 +00:00
|
|
|
"rc.report.next.columns=description.desc",
|
2017-06-02 16:41:47 +00:00
|
|
|
"rc.report.next.labels=Desc"
|
|
|
|
],
|
|
|
|
"");
|
|
|
|
if (result) {
|
2017-06-11 16:45:15 +00:00
|
|
|
// via https://stackoverflow.com/a/35635260
|
|
|
|
var repeat = function(str, count) {
|
|
|
|
var array = [];
|
|
|
|
for(var i = 0; i < count;)
|
|
|
|
array[i++] = str;
|
|
|
|
return array.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
script.noteTextEditWrite("\n");
|
|
|
|
script.noteTextEditWrite(repeat('#', projectName.length) + ' ' + projectName[projectName.length - 1] + "\n\n");
|
2017-06-02 16:41:47 +00:00
|
|
|
var tasksSeparated;
|
|
|
|
// The result does not contain any \n, so we are splitting by whitespace.
|
|
|
|
tasksSeparated = result.toString().split('\n');
|
|
|
|
|
|
|
|
tasksSeparated.splice(0, 1); // removing ""
|
|
|
|
if (tasksSeparated.length === 0) {
|
2017-06-11 14:09:49 +00:00
|
|
|
logIfVerbose("No entries");
|
2017-06-02 16:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tasksSeparated.splice(0, 1); // removing "Desc"
|
|
|
|
tasksSeparated.splice(0, 1); // removing "----"
|
|
|
|
|
|
|
|
tasksSeparated.splice(tasksSeparated.length - 1, 1); // removing ""
|
|
|
|
tasksSeparated.splice(tasksSeparated.length - 1, 1); // removing "X tasks"
|
|
|
|
tasksSeparated.splice(tasksSeparated.length - 1, 1); // removing ""
|
|
|
|
|
|
|
|
tasksSeparated.forEach( function(taskDesc){
|
|
|
|
script.noteTextEditWrite("* " + taskDesc + "\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2017-06-02 15:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|