Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Add support for tasks (closes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
brrd committed Feb 11, 2019
1 parent 988f747 commit 7136d59
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/abr-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function preprocessTemplate (abrApp, element, config, abrWin) {
if (item.id === "spelling") {
item.submenu = spellingMenuGenerator(item.submenu, config);
}
if (item.id === "tasks") {
item.submenu = tasksMenuGenerator(item.submenu, config);
}
if (item.id === "exportHtml") {
item.submenu = assetsMenuGenerator(item.submenu, constants.path.templatesDir, "template.json", "exportHtml");
}
Expand Down Expand Up @@ -147,6 +150,22 @@ function spellingMenuGenerator (submenu, config) {
return submenu;
}

// Generate tasks menu template (before preprocesssing)
function tasksMenuGenerator (submenu, config) {
var tasks = getConfig(config, "tasks") || [];
var submenu = tasks.reduce(function (res, task) {
if (task.name && task.exec) {
res.push({
label: task.name,
command: "runTask",
parameters: task.exec
});
}
return res;
}, []);
return submenu;
}

// Generate menu template (before preprocesssing)
function assetsMenuGenerator (submenu, dirPath, jsonName, command, menuType, checkId) {
// Get a menu item
Expand Down
23 changes: 23 additions & 0 deletions app/dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ Dialogs.prototype = {
return false;
},

taskDone: function (cmd, stdout) {
var options = {
title: this.localizer.get("task-done-title"),
message: this.localizer.get("task-done-message", [cmd]),
buttons: [this.localizer.get('button-ok')],
detail: stdout,
noLink: true
};
dialog.showMessageBox(this.win, options);
},

taskError: function (cmd, errMsg) {
var options = {
type: "error",
title: this.localizer.get("task-error-title"),
message: this.localizer.get("task-error-message", [cmd]),
detail: errMsg,
buttons: [this.localizer.get('button-ok')],
noLink: true
};
dialog.showMessageBox(this.win, options);
},

fileAccessDenied: function (path, callback) {
var userChoice = dialog.showMessageBox(this.win, {
title: this.localizer.get("permission-denied"),
Expand Down
8 changes: 8 additions & 0 deletions app/menu-window.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@
{
"type": "separator"
},
{
"labelKey": "menu-tasks",
"id": "tasks",
"submenu": []
},
{
"type": "separator"
},
{
"labelKey": "menu-close-document",
"accelerator": "CmdOrCtrl+Shift+W",
Expand Down
40 changes: 40 additions & 0 deletions app/renderer/abr-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,46 @@ AbrDocument.prototype = {
that.getConfig("preview-template", doExport); // TODO: log error if template don't exist
},

// Run tasks
runTask: function (instruction) {
var that = this;

// Ask for saving if not clean
if (instruction.indexOf("%inputFilepath%") !== -1 && (!this.path || !this.isClean())) {
that.dialogs.askNeedSave(this, main);
} else {
main();
}

function main () {
var args = {
inputFilepath: '"' + that.path.replace('"', '\"') + '"'
};

// Ask user for outputFilepath
if (instruction.indexOf("%outputFilepath%") !== -1) {
var path = that.dialogs.askSavePath(null, "document");
if (!path) return;
args.outputFilepath = '"' + path.replace('"', '\"') + '"';
}

// Replace args in command
var re = /%(inputFilepath|outputFilepath)%/g;
var cmd = instruction.replace(re, function (str, p1) {
return args[p1] ? args[p1] : str;
});

// Run command
cp.exec(cmd, function (err, stdout) {
if (err) {
that.dialogs.taskError(cmd, err.toString());
return;
}
that.dialogs.taskDone(cmd, stdout);
});
}
},

// Inline autopreview
autopreview: function (types, lines) {
var cm = this.cm;
Expand Down
4 changes: 4 additions & 0 deletions app/renderer/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ var commands = {
});
},

runTask: function(win, abrDoc, cm, param) {
abrDoc.runTask(param);
},

close: function(win, abrDoc, cm) {
abrDoc.close();
},
Expand Down
6 changes: 6 additions & 0 deletions default/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
"active": false,
"language": ""
},
"tasks": [
{
"name": "Pandoc",
"exec": "pandoc %inputFilepath% -o %outputFilepath%"
}
],
"theme": "default",
"window": {
"showMenuBar": true
Expand Down
6 changes: 6 additions & 0 deletions default/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"menu-save-autosave": "Save on Focus Lost",
"menu-export-html": "Export as HTML",
"menu-copy-images-on-html-export": "Copy Images when Exporting",
"menu-tasks": "Run Task",
"menu-close-document": "Close Document",
"menu-close-window": "Close Window",

Expand Down Expand Up @@ -160,6 +161,11 @@
"images-copied": "Images Copied",
"images-copied-message": "Document images have been copied in the '%0' directory.",

"task-done-title": "Task Completed",
"task-done-message": "The task was successfully completed: \n%0",
"task-error-title": "Task Error",
"task-error-message": "An error occurred while running the task: \n%0",

"html-export-title": "Abricotine Document",

"changed-file": "Changed File",
Expand Down
6 changes: 6 additions & 0 deletions default/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"menu-save-autosave": "Sauvegarde automatique",
"menu-export-html": "Exporter en HTML",
"menu-copy-images-on-html-export": "Copier les images lors de l'export",
"menu-tasks": "Exécuter une tâche",
"menu-close-document": "Fermer le document",
"menu-close-window": "Fermer la fenêtre",

Expand Down Expand Up @@ -160,6 +161,11 @@
"images-copied": "Images copiées",
"images-copied-message": "Les images du document ont été copiées dans le répertoire '%0'.",

"task-done-title": "Tâche terminée",
"task-done-message": "La tâche a été exécutée avec succès : \n%0",
"task-error-title": "Échec de la tâche",
"task-error-message": "Une erreur a été rencontrée lors de l'exécution de la tâche : \n%0",

"html-export-title": "Document Abricotine",

"changed-file": "Modification détectée",
Expand Down

0 comments on commit 7136d59

Please sign in to comment.