Added optional vowel length verification

This commit is contained in:
Sergiusz Warga 2020-11-25 23:50:44 +01:00
parent 510cec2d9a
commit 0db5003917

View File

@ -10,13 +10,12 @@
Array.from(form.elements).forEach((input) => {
if (input.type=="text") {
console.log(input.dataset.expected.length)
input.setAttribute("style", input.getAttribute("style") + "; width: " + input.dataset.expected.length + "em");
}
});
}
function mapper(char) {
function nomacron_to_macron(char) {
var mapper = { 'A': 'Ā',
'a': 'ā',
'E': 'Ē',
@ -30,22 +29,45 @@
return mapper[char[0]];
}
function input_to_macron(input) {
return input.replace(/(\w)\1/, mapper);
function macron_to_nomacron(char) {
var mapper = { 'Ā': 'A',
'ā': 'a',
'Ē': 'E',
'ē': 'e',
'Ī': 'I',
'ī': 'i',
'Ō': 'O',
'ō': 'o',
'Ū': 'U',
'ū': 'u'};
return mapper[char];
}
function input_to_nomacron(input) {
return input.replace(/Ā|ā|Ē|ē|Ī|ī/, macron_to_nomacron);
}
function input_to_macron(input) {
return input.replace(/(\w)\1/, nomacron_to_macron);
}
function validate() {
const forms = document.querySelectorAll('form');
const form = forms[0];
Array.from(form.elements).forEach((input) => {
is_double_ok = (input_to_macron(input.value) == input.dataset.expected);
is_macron_ok = (input.value == input.dataset.expected);
if (input.type=="text" && document.getElementById('vowel_length').checked == false) {
is_macron_ok = (input.value == input_to_nomacron(input.dataset.expected));
console.log(input_to_nomacron(input.dataset.expected));
is_double_ok = false;
} else {
is_double_ok = (input_to_macron(input.value) == input.dataset.expected);
is_macron_ok = (input.value == input.dataset.expected);
}
if (input.type=="text" && !(is_double_ok || is_macron_ok)) {
input.setAttribute("style", input.getAttribute("style") + "; border-color:red");
console.log('expected: ' + input.dataset.expected);
console.log('input: ' + input_to_macron(input.value));
} else if (input.type=="text") {
input.setAttribute("style", input.getAttribute("style") + "; border-color:green");
}
@ -69,6 +91,7 @@
<body>
<h1><a href="/latin/llpsi">Lingua Latina Per Se Illustrata</a></h1>
<h2>{{pensum_title}}</h2>
Quantitās: <input type="checkbox" id="vowel_length">
<form class="Pensum_A" onsubmit="return validate();">
{{pensum_content|safe}}
<br>