Skip to content

Commit

Permalink
feat(usertemplates): adding arguments for user templates
Browse files Browse the repository at this point in the history
User templates are now considered functions, and you can pass them some arguments like so
tp.user.my_template(arg1=value1,arg2=value2)
This is not working for now because nunjucks doesn't
support async functions. I need to find a solution for this problem

re #34, re #65
  • Loading branch information
SilentVoid13 committed Mar 27, 2021
1 parent 7a08fd8 commit 58ff185
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 63 deletions.
6 changes: 6 additions & 0 deletions src/TParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { App, TFile } from "obsidian";

export abstract class TParser {
constructor(public app: App) {}
abstract generateContext(file: TFile): Promise<any>;
}
80 changes: 17 additions & 63 deletions src/UserTemplates/UserTemplateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { exec } from "child_process";
import { promisify } from "util";

import TemplaterPlugin from "main";
import { Parser, TemplateParser } from "TemplateParser";
import { ContextMode, TemplateParser } from "TemplateParser";
import { TParser } from "TParser";

export class UserTemplateParser extends Parser {
export class UserTemplateParser extends TParser {
cwd: string;
cmd_options: any;

Expand Down Expand Up @@ -36,21 +37,27 @@ export class UserTemplateParser extends Parser {
let user_templates = new Map();
const exec_promise = promisify(exec);

let cmd_options = {
timeout: this.plugin.settings.command_timeout,
cwd: this.cwd,
}

for (let [template, cmd] of this.plugin.settings.templates_pairs) {
if (template === "" || cmd === "") {
continue;
}

cmd = await this.template_parser.parseTemplates(cmd, file);
cmd = await this.template_parser.parseTemplates(cmd, file, ContextMode.INTERNAL);

user_templates.set(template, async (): Promise<string> => {
user_templates.set(template, async (kwargs: any): Promise<string> => {
try {
let {stdout, stderr} = await exec_promise(cmd, cmd_options);
let process_env = {
...process.env,
...kwargs
};

let cmd_options = {
timeout: this.plugin.settings.command_timeout * 1000,
cwd: this.cwd,
env: process_env,
};

let {stdout} = await exec_promise(cmd, cmd_options);
return stdout;
}
catch(error) {
Expand All @@ -66,57 +73,4 @@ export class UserTemplateParser extends Parser {
let user_templates = await this.generateUserTemplates(file);
return Object.fromEntries(user_templates);
}

async parseTemplates(content: string, file: TFile) {
/*
let child_process = require("child_process");
if (child_process === undefined) {
throw new Error("nodejs child_process loading failure.");
}
const util = require("util");
if (util === undefined) {
throw new Error("nodejs util loading failure.");
}
const exec_promise = util.promisify(child_process.exec);
for (let i = 0; i < this.plugin.settings.templates_pairs.length; i++) {
let template_pair = this.plugin.settings.templates_pairs[i];
let template = template_pair[0];
let cmd = template_pair[1];
if (template === "" || cmd === "") {
continue;
}
cmd = await this.internalTemplateParser.parseTemplates(cmd, file);
if (content.contains(template)) {
try {
let process_env = process.env;
process_env["test"] = "test";
console.log("PROCESS_ENV:", process_env);
let {stdout, stderr} = await exec_promise(cmd, {
timeout: this.plugin.settings.command_timeout*1000,
cwd: this.cwd
});
content = content.replace(
new RegExp(template, "g"),
stdout.trim()
);
}
catch(error) {
console.log(`Error with the template n° ${(i+1)}:\n`, error);
new Notice("Error with the template n°" + (i+1) + " (check console for more informations)");
}
}
}
*/

let context = await this.generateContext(file);
console.log("USER_CONTEXT:", context);
content = nunjucks.renderString(content, context);

return content;
}
}

0 comments on commit 58ff185

Please sign in to comment.