Upload files to "/"
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
// Export helpers: wikitext source as HTML for PDF and Word, and the Word (.docx) writer.
|
||||
'use strict';
|
||||
const HTMLtoDOCX = require('html-to-docx');
|
||||
|
||||
const PAPER = {
|
||||
Letter: { w: 12240, h: 15840 },
|
||||
A4: { w: 11906, h: 16838 }
|
||||
};
|
||||
|
||||
function esc(s) {
|
||||
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
// Full page for the PDF of the wikitext source.
|
||||
function markupPage(title, text) {
|
||||
return '<!DOCTYPE html><html><head><meta charset="utf-8"><title>' + esc(title) + '</title><style>' +
|
||||
'body{margin:0;font:11px/1.45 Consolas,"Cascadia Mono","Courier New",monospace;color:#111}' +
|
||||
'h1{font:bold 15px/1.3 "Segoe UI",Arial,sans-serif;margin:0 0 10px}' +
|
||||
'pre{margin:0;white-space:pre-wrap;word-break:break-word;tab-size:4}' +
|
||||
'</style></head><body><h1>' + esc(title) + '</h1><pre>' + esc(text.replace(/\r\n?/g, '\n')) + '</pre></body></html>';
|
||||
}
|
||||
|
||||
// Body markup for Word: one paragraph per line so spacing and line breaks survive.
|
||||
function markupBody(title, text) {
|
||||
const lines = text.replace(/\r\n?/g, '\n').split('\n').map((l) => {
|
||||
const lead = /^ */.exec(l)[0].length;
|
||||
const body = ' '.repeat(lead) + esc(l.slice(lead)).replace(/\t/g, ' ');
|
||||
return '<p style="font-family:Consolas;font-size:10pt;margin:0">' + (body || ' ') + '</p>';
|
||||
});
|
||||
return '<h1>' + esc(title) + '</h1>' + lines.join('');
|
||||
}
|
||||
|
||||
function wrapDoc(title, body) {
|
||||
return '<!DOCTYPE html><html><head><meta charset="utf-8"><title>' + esc(title) + '</title></head><body>' + body + '</body></html>';
|
||||
}
|
||||
|
||||
// The Word writer drops a lone space between two inline elements, so make those spaces non-breaking.
|
||||
function fixSpaces(html) {
|
||||
const inl = 'b|i|u|s|em|strong|span|a|code|sup|sub|small|big|cite|abbr|q|mark|del|ins';
|
||||
const re = new RegExp('(</(?:' + inl + ')>)\\s+(<(?:' + inl + '|img)[\\s>])', 'gi');
|
||||
let prev;
|
||||
do { prev = html; html = html.replace(re, '$1 $2'); } while (html !== prev);
|
||||
return html;
|
||||
}
|
||||
|
||||
async function toDocx(title, body, paper) {
|
||||
body = fixSpaces(body);
|
||||
const p = PAPER[paper] || PAPER.Letter;
|
||||
const opts = { title, pageSize: { width: p.w, height: p.h }, table: { row: { cantSplit: true } }, footer: false, pageNumber: false };
|
||||
let out;
|
||||
try {
|
||||
out = await HTMLtoDOCX(wrapDoc(title, body), null, opts);
|
||||
} catch (e) {
|
||||
// Pictures that cannot be fetched can sink the whole file, so try again without them.
|
||||
out = await HTMLtoDOCX(wrapDoc(title, body.replace(/<img\b[^>]*>/gi, '')), null, opts);
|
||||
}
|
||||
return Buffer.isBuffer(out) ? out : Buffer.from(await out.arrayBuffer ? await out.arrayBuffer() : out);
|
||||
}
|
||||
|
||||
module.exports = { markupPage, markupBody, toDocx, fixSpaces, PAPER };
|
||||
Reference in New Issue
Block a user