templateGen/js/9.9.3/parseTemplate.js
maru21 92bbebd2a1 Bugfixes and feature improvements new ver 9.9.3
fixed bug with sidebar click event not being cleared after accessing files and then pressing edit
some cosmetic changes animations
reimplemented bookshelfexport in localonly mode
added logout functionality
2023-10-07 20:42:58 +02:00

389 lines
13 KiB
JavaScript

function parseInput(wordArray, objects, i) {
let word = wordArray[i];
if (word.substring(0, 1) == "%") {
//check if regular use of % in text 20% an ignoreCase
if (word.substring(0, 2) !== "% ") {
//found simple input %word / excluding %m:
if (word.substring(2, 3) !== ":") {
//init Word Object
var wordObj = {};
let w = word.substring(1);
//bugfix if the title of an input has no space in it ex: %test=l:first word;l:second word;%1
let oneWordFlag = false;
if (word.substring(0, 1) == "%" && word.indexOf('=') != -1) {
oneWordFlag = true;
}
//for loop to escape spaces in simple input
for (let j = i+1; j < wordArray.length; j++) {
//if title has no space then go back one word to include "=" ex:
if (oneWordFlag) {
j = i;
oneWordFlag = false;
} else {
w = w + " " + wordArray[j];
}
//invoke look for gender specific template
i = findGenderSpecificInput(wordArray, wordObj, j);
//invoke look for list template
i = findListInput(wordArray, wordObj, j);
//invoke connected fields
i = findConnectedListInput(wordArray, wordObj, j);
//find end of template string and format it for future handling¨
if (w.indexOf("%") !== -1) {
//found % sign
if (w.indexOf("%") !== w.length - 1) {
//% is not last char of string
word = "%" + w;
} else {
//% is last
//no prio has been set
word = w.slice(0, -1);
}
break;
}
}
if (word.indexOf("\n") !== -1) {
if (word.substring(word.indexOf("\n"), 2).indexOf("%") !== -1) {
//alert("attention newlineChar in: "+ word.substring(word.indexOf("\n"),2));
}
}
//parse priority
if (word.substring(1).indexOf("%") === -1) {
//object if no prio was set
wordObj.word = word;
wordObj.arrayPos = objects.length;
wordObj.prio = 0;
} else {
//handle edgecase if punctuation mark is directly after prio
let postMarker = word.substring(1).indexOf("%") + 2;
let postMarkerEnd = word.length;
let isPriority = true;
let i = 0;
//console.log(word + " * " + word.substring(postMarkerEnd-2, postMarkerEnd) + " - " + postMarker + ":" + postMarkerEnd + " - " + word.length);
while (
!isCharNumber(word.substring(postMarkerEnd - 1, postMarkerEnd))
) {
postMarkerEnd = postMarkerEnd - 1;
//if no priority has been set; set flag
//console.log(word.substring(postMarkerEnd-1, postMarkerEnd));
if (postMarkerEnd < 1) {
isPriority = false;
break;
}
i++;
}
if (isPriority) {
//console.log(word + " prio: "+isPriority);
//object if prio has been defined
wordObj.word = word.substring(1, postMarker - 1);
wordObj.arrayPos = objects.length;
wordObj.prio = parseInt(
word.substring(postMarker, postMarkerEnd),
10
);
if (isNaN(wordObj.prio)) {
alert(
"error in template: %" +
wordObj.word +
"% there must always a space after the priority number"
);
wordObj.prio = 0;
}
} else {
//object if no prio has been defined
wordObj.word = word.substring(1, postMarker - 1);
wordObj.arrayPos = objects.length;
wordObj.prio = 0;
}
}
//check if genderSpecific or list has been found and if so reformat word
//console.log(wordObj);
switch (wordObj.type) {
case "genderSpecific":
if (word.substring(0, 1) === "%") {
wordObj.word = word.split("=")[0].substring(1);
} else {
wordObj.word = word.split("=")[0];
}
break;
case "list":
if (word.substring(0, 1) === "%") {
wordObj.word = word.split("=")[0].substring(1);
} else {
wordObj.word = word.split("=")[0];
}
break;
case "conList":
if (word.substring(0, 1) === "%") {
wordObj.word = word.split("=")[0].substring(1);
} else {
wordObj.word = word.split("=")[0];
}
//check if format has been given or markup
for (let i = 0; i <= wordObj.listCount; i++) {
let params = wordObj[i].split(":");
if (params[1] !== undefined) {
wordObj[i] = params[0];
wordObj["clType-"+wordObj[i]] = (params[2] !== undefined) ? params[1]+":"+params[2]: params[1];
} else {
wordObj["clType-"+wordObj[i]] = "cl-simpleInput";
}
}
break;
default:
wordObj.type = "simpleInput";
//check if customTemplate was used set type and format word
if (word.indexOf("=") !== -1) {
parseCustomTemplates(wordObj);
}
break;
}
objects.push(wordObj);
}
}
}
}
function findGenderSpecificInput(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf("=m:") !== -1) {
wordObj.type = "genderSpecific";
let mw = word.substring(word.indexOf("=m:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
mw = mw + " " + wordArray[j];
i = j;
if (mw.indexOf(";") !== -1) {
wordObj.m = mw.slice(0, mw.indexOf(";"));
//adding length word + 3 = m: ;
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
if (word.indexOf("=w:") !== -1) {
let ww = word.substring(word.indexOf("=w:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
ww = ww + " " + wordArray[j];
i = j;
if (ww.indexOf(";") !== -1) {
wordObj.w = ww.slice(0, ww.indexOf(";"));
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
if (word.indexOf("=d:") !== -1) {
let dw = word.substring(word.indexOf("=d:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
dw = dw + " " + wordArray[j];
i = j;
if (dw.indexOf(";") !== -1) {
wordObj.d = dw.slice(0, dw.indexOf(";"));
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function parseGenderTemplate(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf(";m:") !== -1) {
let mw = word.substring(word.indexOf(";m:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
mw = mw + " " + wordArray[j];
i = j;
if (mw.indexOf(";") !== -1) {
wordObj.m = mw.slice(0, mw.indexOf(";"));
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
if (word.indexOf(";w:") !== -1) {
let ww = word.substring(word.indexOf(";w:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
ww = ww + " " + wordArray[j];
i = j;
if (ww.indexOf(";") !== -1) {
wordObj.w = ww.slice(0, ww.indexOf(";"));
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
if (word.indexOf(";d:") !== -1) {
let dw = word.substring(word.indexOf(";d:") + 3);
for (let j = i + 1; j < wordArray.length; j++) {
dw = dw + " " + wordArray[j];
i = j;
if (dw.indexOf(";") !== -1) {
wordObj.d = dw.slice(0, dw.indexOf(";"));
i = parseGenderTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function findListInput(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf("=l:") !== -1) {
wordObj.type = "list";
wordObj.listCount = 0;
let lw = word.substring(word.indexOf("=l:") + 3);
//escape if there is no space in listitem
if (lw.indexOf(";") !== -1) {
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//adding length word + 3 = m: ;
i = parseListTemplate(wordArray, wordObj, i);
return i;
}
for (let j = i + 1; j < wordArray.length; j++) {
lw = lw + " " + wordArray[j];
i = j;
if (lw.indexOf(";") !== -1) {
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//adding length word + 3 = m: ;
i = parseListTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function parseListTemplate(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf(";l:") !== -1) {
let lw = word.substring(word.indexOf(";l:") + 3);
//escape if there is no space in listitem
if (lw.indexOf(";") !== -1) {
wordObj.listCount++;
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//shorten wordArray and hand it to parseListTemplate: firstItem;l:secondItem;l: to ;l:secondItem;l:third Item
wordArray[i] = wordArray[i]
.substring(1)
.substring(wordArray[i].indexOf(";") + 1);
//advance wordArray i if third Item has a space in it and no ; is found to avoid endless loop
if (wordArray[i].substring(1).indexOf(";") === -1) {
i++;
}
//console.log(wordArray[i].substring(1));
i = parseListTemplate(wordArray, wordObj, i);
return i;
}
for (let j = i + 1; j < wordArray.length; j++) {
lw = lw + " " + wordArray[j];
i = j;
if (lw.indexOf(";") !== -1) {
wordObj.listCount++;
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
i = parseListTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function findConnectedListInput(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf("=h:") !== -1) {
wordObj.type = "conList";
wordObj.listCount = 0;
let lw = word.substring(word.indexOf("=h:") + 3);
//escape if there is no space in listitem
if (lw.indexOf(";") !== -1) {
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//adding length word + 3 = m: ;
i = parseConnectedListTemplate(wordArray, wordObj, i);
return i;
}
for (let j = i + 1; j < wordArray.length; j++) {
lw = lw + " " + wordArray[j];
i = j;
if (lw.indexOf(";") !== -1) {
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//adding length word + 3 = m: ;
i = parseConnectedListTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function parseConnectedListTemplate(wordArray, wordObj, i) {
let word = wordArray[i];
if (word.indexOf(";h:") !== -1) {
let lw = word.substring(word.indexOf(";h:") + 3);
//escape if there is no space in listitem
if (lw.indexOf(";") !== -1) {
wordObj.listCount++;
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
//shorten wordArray and hand it to parseListTemplate: firstItem;l:secondItem;l: to ;l:secondItem;l:third Item
wordArray[i] = wordArray[i]
.substring(1)
.substring(wordArray[i].indexOf(";") + 1);
//advance wordArray i if third Item has a space in it and no ; is found to avoid endless loop
if (wordArray[i].substring(1).indexOf(";") === -1) {
i++;
}
//console.log(wordArray[i].substring(1));
i = parseConnectedListTemplate(wordArray, wordObj, i);
return i;
}
for (let j = i + 1; j < wordArray.length; j++) {
lw = lw + " " + wordArray[j];
i = j;
if (lw.indexOf(";") !== -1) {
wordObj.listCount++;
wordObj[wordObj.listCount] = lw.slice(0, lw.indexOf(";"));
i = parseConnectedListTemplate(wordArray, wordObj, i);
break;
}
}
}
return i;
}
function parseCustomTemplates(wordObj) {
let word = wordObj.word;
for (let i = 0; i < activeState.templateFieldTypes.length; i++) {
if (word.indexOf("=" + activeState.templateFieldTypes[i]) !== -1) {
wordObj.word = word.split("=")[0];
wordObj.type = word.split("=")[1];
if (wordObj.type.indexOf(":") !== -1) {
let ltPlaceholder = wordObj.type.split(":")[1];
if (ltPlaceholder !== "undefined") {
wordObj.placeholder = ltPlaceholder;
//wordObj.type = wordObj.type.split(":")[0]; - dont do this
}
}
}
}
}
function isCharNumber(c) {
return c >= "0" && c <= "9";
}
export default parseInput;