kovachev.xyz/wiktionary/quotes.html

219 lines
9.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quote generator</title>
<style>
label {
display: block
}
.gloss {
text-decoration-style: dotted;
text-decoration: underline;
}
html {
background-color: black;
filter: invert(1);
}
</style>
<script>
const basic = "basic";
const additional = "additional";
const parameters = {
"type": {label: "Type", section: basic, type: "dropdown", fields: {
"book": "Book",
"web": "Web",
"song": "Song",
"journal": "Journal",
"wikipedia": "Wikipedia"
}},
"language": {label: "Language(s)", section: basic},
"text": {label: "Text", section: basic, type: "textarea"},
"t": {label: "Translation or re-wording", section: basic, type: "textarea"},
"year": {label: "Year", section: basic},
"author": {label: "Author", section: basic},
"title": {label: "Title", section: basic},
"volume": {label: "Volume", section: additional},
"chapter": {label: "Chapter", section: additional},
"page": {label: "Page", section: additional},
"publisher": {label: "Publisher", section: additional},
"location": {label: "Location", section: additional},
"url": {label: "URL", section: additional},
"pageurl": {label: "Page URL", section: additional},
"original": {label: "Original title", section: additional},
"origlang": {label: "Original language", section: additional},
"origyear": {label: "Original year", section: additional},
"translator": {label: "Translator", section: additional},
"trans-title": {label: "Translated title", section: additional},
"series": {label: "Series", section: additional},
"isbn": {label: "ISBN", section: additional},
"format": {label: "Format", section: additional, type: "dropdown", fields: {
"": "(unspecified)",
"hardcover": "Hardcover",
"paperback": "Paperback",
"blog": "Blog"
}},
"genre": {label: "Genre", section: additional, type: "dropdown", fields: {
"": "(unspecified)",
"fiction": "Fiction",
"non-fiction": "Non-fiction"
}}
}
let presetMenu;
addEventListener("DOMContentLoaded", (event) => {
presetMenu = document.getElementById("presets");
presetMenu.addEventListener("change", () => loadPreset(presetMenu.value));
generatePropertyInputs();
loadPresetList();
});
</script>
</head>
<body>
<h1 style="display: inline-block">en.wiktionary Quote Generator</h1> <span class="gloss" title="Enter values for each of the quote template fields. Once you're done, click Generate or Copy to clipboard to get the output as wikitext. You may set presets to skip out the repetitive bits if quoting from the same book many times.">(?)</span>
<form>
<hr>
<div id="basic">
<h2>Basic information</h2>
<hr>
</div>
<hr>
<div id="additional">
<h2>Additional information</h2>
<hr>
<br>
</div>
<br>
<input type="button" onclick="generate()" value="Generate" title="Generate">
<input type="button" onclick="copy()" value="Copy to clipboard" title="Copy to clipboard">
<br>
<textarea readonly="true" id="out" cols="60" rows="6"></textarea>
<hr>
<label>Preset <span class="gloss" title="When a preset name is input in the 'preset name' box below, you can save the current state of each option as a preset, which will be loaded again even if the page is refreshed.">(?)</span></label>
<select id="presets">
</select>
<label>Preset name</label> <input type="text" id="preset-name">
<input type="button" value="Save" onclick="savePreset()">
<input type="button" value="Delete" onclick="deletePreset()">
</form>
<script>
function getTemplateName(type) {
return `quote-${type}`;
}
function generate() {
// Template type chosen
const type = document.getElementById("type").value;
const templateName = getTemplateName(type);
// Language name passed as a positional parameter
const languages = document.getElementById("language").value;
// Generate template object
let out = `{{${templateName}|${languages}`;
out += addNamedParameters(getParameterObject());
out += "}}";
document.getElementById("out").innerText = out;
}
function copy() {
generate();
navigator.clipboard.writeText(document.getElementById("out").innerHTML);
}
function addNamedParameters(obj, ignore = ["type", "language"]) {
let out = "";
for (const key in obj) {
if (obj[key] == "" || ignore.includes(key)) continue;
out += `|${key}=${obj[key]}`;
}
return out;
}
function loadPresetList() {
const items = {...localStorage}; delete items.latestPreset;
for (const key in items) {
addPresetToMenu(key);
}
console.log(localStorage.getItem("latestPreset"));
loadPreset(localStorage.getItem("latestPreset"));
}
function generatePropertyInputs() {
for (const key in parameters) {
const label = document.createElement("label");
label.innerText = parameters[key].label;
let input;
if (parameters[key].type === "dropdown") {
input = document.createElement("select");
for (const optionName in parameters[key].fields) {
const option = document.createElement("option");
option.value = optionName;
option.innerText = parameters[key].fields[optionName];
input.appendChild(option)
}
} else if (parameters[key].type === "textarea") {
input = document.createElement("textarea");
input.cols = 60;
input.rows = 6;
} else {
input = document.createElement("input");
input.type = "text";
}
input.id = key;
document.getElementById(parameters[key].section).appendChild(label);
document.getElementById(parameters[key].section).appendChild(input);
}
}
function addPresetToMenu(key) {
const existingPresets = document.querySelectorAll("#presets option");
for (const node of existingPresets) {
if (node.value === key) {
return;
}
}
const elem = document.createElement("option");
elem.value = key;
elem.innerText = key;
presetMenu.appendChild(elem);
}
function savePreset() {
const presetName = document.getElementById("preset-name").value;
localStorage.setItem(presetName, JSON.stringify(getParameterObject()));
addPresetToMenu(presetName);
presetMenu.value = presetName;
}
function getParameterObject() {
const out = {}
for (const key in parameters) {
out[key] = document.getElementById(key).value;
}
return out;
}
function loadPreset(presetName) {
document.getElementById("preset-name").value = presetMenu.value;
const parameterValues = JSON.parse(localStorage.getItem(presetName));
for (const key in parameterValues) {
document.getElementById(key).value = parameterValues[key];
}
localStorage.setItem("latestPreset", presetName);
}
function deletePreset() {
const presetName = document.getElementById("preset-name").value;
localStorage.removeItem(presetName);
const existingPresets = presetMenu.children;
let currentPresetOptionNode;
for (const node of existingPresets) {
if (node.value === presetName) {
currentPresetOptionNode = node;
break;
}
}
presetMenu.removeChild(currentPresetOptionNode);
if (presetMenu.children.length === 1) {
document.getElementById("preset-name").value = presetMenu.children[0].value;
}
}
</script>
</body>
</html>