dyneval.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. setup.codeSanitize = (code) => {
  2. function varnameToTW(varname){
  3. varname = "QSP."+varname;
  4. if(varname.indexOf("[") == -1)
  5. varname += "[0]";
  6. return varname;
  7. }
  8. const regexp = /<<set QSP.*?\s*=\s*(.*?)>>/g;
  9. const matches = [...code.matchAll(regexp)];
  10. for(const match of matches){
  11. const completeMatch = match[0];
  12. let updatedMatch = completeMatch;
  13. const setValue = match[1];
  14. let updatedSetValue = setValue;
  15. const linkRegex = /<a\s+href\s*=\s*\"\s*exec\s*:(.*?)">(.*?)<\/a>/g;
  16. const linkMatches = [...setValue.matchAll(linkRegex)];
  17. for(const linkMatch of linkMatches){
  18. const completeLinkMatch = linkMatch[0];
  19. let updatedLink = completeLinkMatch;
  20. const commands = linkMatch[1];
  21. const label = linkMatch[2];
  22. const singleCommands = commands.split('&');
  23. const commandsOut = [];
  24. for(const singleCommand of singleCommands){
  25. let trimmed = singleCommand.trim();
  26. if (trimmed.startsWith('gt')){
  27. const gtArguments = trimmed.substring(3);
  28. commandsOut.push("<<gt "+gtArguments.replace(","," ").replaceAll("''","'")+">>");
  29. }else{
  30. //$start_type[''loc''] = ''' + QSP.$ARGS[2] + '''
  31. const setSplit = trimmed.split("=");
  32. let varname = varnameToTW(setSplit[0].replaceAll("''","'"));
  33. let val = setSplit[1].replaceAll("'''","''").replaceAll("[''","['").replaceAll("'']","']");
  34. commandsOut.push("<<set "+varname+"="+val+">>");
  35. }
  36. }
  37. updatedLink = "<<link '"+label+"'>>" + commandsOut.join("")+ "<</link>>";
  38. //updatedMatch = updatedMatch.replace(completeLinkMatch,updatedLink);
  39. updatedSetValue = updatedSetValue.replace(completeLinkMatch,updatedLink);
  40. }
  41. //code = code.replace(completeMatch, "`"+ updatedMatch + "`");
  42. code = code.replace(setValue, "`"+ updatedSetValue + "`");
  43. }
  44. //code = code.replaceAll("QSP.$ARGS[","_dynevalVars[");
  45. /*
  46. <<if QSP.$ARGS[0] == QSP.$ARGS[2] && QSP.$ARGS[1] == QSP.$ARGS[3]>> <<set QSP.$result[0] = `'<TD><center><img src="images/system/icons/check.png" height="50"></center></TD>'`>> <<else>> <<set QSP.$result[0] = `'<TD><center><<link '<img src="images/system/icons/uncheck.png" height="50">'>><<set $start_type['loc'] = '' + QSP.$ARGS[2] + ''>><<set $start_type['magic'] = '' + QSP.$ARGS[3] + ''>><<gt 'begin' 'start'>><</link>></center></TD>'`>> <</if>>
  47. */
  48. return code;
  49. }
  50. setup.dyneval = function (code, ...margs){
  51. const resultBackupt = QSP.$result[0];
  52. State.variables.ARGSstack.push(margs);
  53. //State.temporary.dynevalVars = margs;
  54. const saneCode = setup.codeSanitize(code);
  55. //const completeCode = "<<capture _dynevalVars>>"+saneCode+"<</capture>>";
  56. let completeCode = saneCode;
  57. for (let index = 0; index < margs.length; index++) {
  58. const varnamePlain = "QSP.$ARGS["+index+"]";
  59. const val = JSON.stringify(margs[index]);
  60. completeCode = completeCode.replaceAll(varnamePlain, val);
  61. }
  62. console.log("CODE",completeCode);
  63. $.wiki(completeCode);
  64. State.variables.ARGSstack.pop();
  65. const result = QSP.$result[0];
  66. QSP.$result[0] = resultBackupt;
  67. return result;
  68. }