checkpoint

master
Kuba Orlik 3 years ago
parent d8b1b414e2
commit 1371daae7f

@ -25,21 +25,23 @@ function parse_mail(sexp_string) {
const mail_obj = parse_mail(mail_body); const mail_obj = parse_mail(mail_body);
let html = mail_obj["body-html"] let html =
? mail_obj["body-html"] mail_obj["body-html"] && mail_obj["body-html"].match(/body/i) !== null
: /* HTML */ `<html> ? mail_obj["body-html"]
<head> : /* HTML */ `<html>
<meta charset="utf-8" /> <head>
</head> <meta charset="utf-8" />
<body> </head>
<pre style="white-space: pre-wrap">${mail_obj["body-txt"]}</pre> <body>
</body> <pre style="white-space: pre-wrap">${mail_obj["body-txt"]}</pre>
</html>`; </body>
</html>`;
const attachments = (await $`mu extract ${path}`).stdout; const attachments = (await $`mu extract ${path}`).stdout;
function getSimpleHeader(body, header_name) { function getSimpleHeader(body, header_name) {
const regexp = new RegExp(`:${header_name} (.*)\n`); const regexp = new RegExp(`:${header_name} (.*)\n`);
console.log("get simple header", header_name, body);
return body.match(regexp)[1]; return body.match(regexp)[1];
} }
@ -57,11 +59,17 @@ function escape(str) {
return str.replace(/</g, "&lt;"); return str.replace(/</g, "&lt;");
} }
console.log({ attachments });
const to_download = attachments const to_download = attachments
.split("\n") .split("\n")
.slice(1) .slice(1)
.map((line) => {
//someimtes images have the wrong mime type, like for example in photos from straż miejska confirmation emails
if (line.includes(".jpg") && line.includes("application/octet-stream")) {
return line.replace("application/octet-stream", "image/jpg");
} else {
return line;
}
})
.filter((line) => line.includes("image")) .filter((line) => line.includes("image"))
.map((line) => { .map((line) => {
return { return {
@ -119,7 +127,7 @@ const date = await formatDate(getSimpleHeader(mail_body, "date"));
html = html.replace( html = html.replace(
/<body>/i, /<body>/i,
`<body>` + `<meta charset="utf-8"><body>` +
/* HTML */ `<h1>${subject}</h1> /* HTML */ `<h1>${subject}</h1>
<table> <table>
<tr> <tr>
@ -146,9 +154,10 @@ const output = (await $`mktemp --suffix=.html`).stdout.replace("\n", "");
await fs.writeFile(output, html); await fs.writeFile(output, html);
const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`; const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`.replace(
",",
console.log("Wrote to", output_file); " "
);
await $`wkhtmltopdf ${output} ${output_file}`; await $`wkhtmltopdf ${output} ${output_file}`;
// console.log(pwd + "output.html"); // console.log(pwd + "output.html");

Loading…
Cancel
Save