2022-08-18 21:47:17 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
text: '"hello *world*"',
|
|
|
|
to: 'html5',
|
|
|
|
from: 'markdown',
|
|
|
|
standalone: false,
|
|
|
|
citeproc: false };
|
|
|
|
|
|
|
|
function permalink() {
|
|
|
|
let input = document.getElementById("text").value;
|
|
|
|
let from = document.getElementById("from").value;
|
|
|
|
let to = document.getElementById("to").value;
|
|
|
|
let standalone = document.getElementById("standalone").checked ? true : false;
|
|
|
|
let citeproc = document.getElementById("citeproc").checked ? true : false;
|
|
|
|
let href = window.location.href;
|
|
|
|
const URLparams = new URLSearchParams(Object.entries({text: input, from: from, to: to, standalone: standalone, citeproc: citeproc}));
|
|
|
|
return href.replace(/([?].*)?$/,"?" + URLparams);
|
|
|
|
}
|
|
|
|
|
|
|
|
const binaryFormats = {
|
|
|
|
docx: { extension: "docx",
|
|
|
|
mime: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
|
|
|
|
odt: { extension: "odt",
|
|
|
|
mime: "application/vnd.oasis.opendocument.text" },
|
|
|
|
pptx: { extension: "pptx",
|
|
|
|
mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
|
|
|
|
epub: { extension: "epub",
|
|
|
|
mime: "application/epub+zip" },
|
|
|
|
epub2: { extension: "epub",
|
|
|
|
mime: "application/epub+zip" },
|
|
|
|
epub3: { extension: "epub",
|
|
|
|
mime: "application/epub+zip" }
|
|
|
|
};
|
|
|
|
|
|
|
|
const binaryMimeTypes = {
|
|
|
|
["application/epub+zip"]: true,
|
|
|
|
["application/vnd.openxmlformats-officedocument.wordprocessingml.document"]: true,
|
|
|
|
["application/vnd.openxmlformats-officedocument.presentationml.presentation"]: true,
|
|
|
|
["application/vnd.oasis.opendocument.text"]: true
|
|
|
|
};
|
|
|
|
|
|
|
|
function paramsFromURL() {
|
|
|
|
if (window.location.search.length > 0) {
|
|
|
|
const uparams = new URLSearchParams(window.location.search);
|
|
|
|
params.text = uparams.get("text") || "";
|
|
|
|
params.from = uparams.get("from") || "markdown";
|
|
|
|
params.to = uparams.get("to") || "html5";
|
|
|
|
params.standalone = uparams.get("standalone") === "true";
|
|
|
|
params.citeproc = uparams.get("citeproc") === "true";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleErrors(response) {
|
2022-08-19 19:33:24 +02:00
|
|
|
if (response.status == 503) {
|
2022-08-18 22:58:02 +02:00
|
|
|
throw Error("Conversion timed out.")
|
2022-08-19 19:49:16 +02:00
|
|
|
// } else if (!response.ok) {
|
|
|
|
// throw Error(response.statusText);
|
2022-08-18 22:58:02 +02:00
|
|
|
}
|
2022-08-18 21:47:17 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convert() {
|
2022-08-19 19:49:16 +02:00
|
|
|
document.getElementById("results").textContent = "";
|
2022-08-18 21:47:17 +02:00
|
|
|
let text = document.getElementById("text").value;
|
|
|
|
let from = document.getElementById("from").value;
|
|
|
|
let to = document.getElementById("to").value;
|
|
|
|
let standalone = document.getElementById("standalone").checked;
|
|
|
|
let citeproc = document.getElementById("citeproc").checked;
|
|
|
|
params = { text: text, from: from, to: to, standalone: standalone,
|
|
|
|
citeproc: citeproc };
|
|
|
|
|
|
|
|
if (text && text != "") {
|
|
|
|
let commandString = "pandoc"
|
|
|
|
+ " --from " + from + " --to " + to
|
|
|
|
+ (standalone ? " --standalone" : "")
|
|
|
|
+ (citeproc ? " --citeproc" : "") ;
|
|
|
|
document.getElementById("command").textContent = commandString;
|
|
|
|
fetch("/cgi-bin/pandoc-server.cgi", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {"Content-Type": "application/json"},
|
2022-08-19 18:02:13 +02:00
|
|
|
body: JSON.stringify({ from: params.from,
|
|
|
|
to: params.to,
|
|
|
|
text: params.text,
|
|
|
|
standalone: params.standalone,
|
2022-08-22 06:33:52 +02:00
|
|
|
citeproc: params.citeproc })
|
2022-08-18 21:47:17 +02:00
|
|
|
})
|
2022-08-19 06:57:00 +02:00
|
|
|
.then(handleErrors)
|
2022-08-18 21:47:17 +02:00
|
|
|
.then(response => response.text())
|
|
|
|
.then(restext => {
|
|
|
|
let binary = binaryFormats[to];
|
|
|
|
if (binary) {
|
|
|
|
document.getElementById("results").innerHTML =
|
|
|
|
'<a download="trypandoc.' + binary.extension +
|
|
|
|
'" href="data:' + binary.mime + ';base64,' + restext +
|
|
|
|
'">click to download trypandoc.' + binary.extension + '</a>';
|
|
|
|
} else {
|
|
|
|
document.getElementById("results").textContent = restext;
|
|
|
|
}
|
|
|
|
document.getElementById("permalink").href = permalink();
|
2022-08-19 06:57:00 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2022-08-19 19:49:16 +02:00
|
|
|
document.getElementById("results").textContent = error
|
2022-08-19 06:57:00 +02:00
|
|
|
}
|
|
|
|
);
|
2022-08-18 21:47:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
paramsFromURL();
|
|
|
|
document.getElementById("text").value = params.text;
|
|
|
|
document.getElementById("from").value = params.from;
|
|
|
|
document.getElementById("to").value = params.to;
|
|
|
|
document.getElementById("standalone").checked = params.standalone;
|
|
|
|
document.getElementById("citeproc").checked = params.citeproc;
|
|
|
|
|
|
|
|
document.getElementById("convert").onclick = convert;
|
|
|
|
document.getElementById("from").onchange = convert;
|
|
|
|
document.getElementById("to").onchange = convert;
|
|
|
|
document.getElementById("standalone").onchange = convert;
|
|
|
|
document.getElementById("citeproc").onchange = convert;
|
|
|
|
|
|
|
|
document.getElementById("examples").onchange =
|
|
|
|
(e => window.location.href = e.target.value );
|
|
|
|
|
|
|
|
const fileInput = document.getElementById('loadfile');
|
|
|
|
|
|
|
|
// Listen for the change event so we can capture the file
|
|
|
|
fileInput.addEventListener('change', (e) => {
|
|
|
|
// Get a reference to the file
|
|
|
|
const file = e.target.files[0];
|
|
|
|
const mimetype = file.type;
|
|
|
|
let binary = binaryMimeTypes[mimetype];
|
|
|
|
|
|
|
|
// Encode the file using the FileReader API
|
|
|
|
const reader = new FileReader();
|
|
|
|
let inputtext = document.getElementById("text");
|
|
|
|
reader.onloadend = () => {
|
|
|
|
// Use a regex to remove data url part
|
|
|
|
if (binary) {
|
|
|
|
const base64String = reader.result
|
|
|
|
.replace('data:', '')
|
|
|
|
.replace(/^.+,/, '');
|
|
|
|
inputtext.value = base64String;
|
|
|
|
} else {
|
|
|
|
inputtext.value = reader.result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (binary) {
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
} else {
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-19 06:57:00 +02:00
|
|
|
fetch("/cgi-bin/pandoc-server.cgi/version")
|
|
|
|
.then(handleErrors)
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(restext =>
|
|
|
|
document.getElementById("version").textContent = restext
|
|
|
|
);
|
|
|
|
|
2022-08-18 21:47:17 +02:00
|
|
|
convert();
|
|
|
|
|
|
|
|
})();
|
|
|
|
|