From 70c9745d34b705eb8a4dc72b6e4655739d31347c Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Fri, 20 Nov 2020 22:17:47 +0100
Subject: [PATCH] Factor some lines (raw loading pug files)

---
 client/package-lock.json         | 60 ++++++++++++++++----------------
 client/package.json              |  4 +--
 client/src/utils/afterRawLoad.js | 10 ++++++
 client/src/views/Analyse.vue     | 16 ++++-----
 client/src/views/Faq.vue         | 13 ++++---
 client/src/views/Game.vue        | 19 ++++------
 client/src/views/Problems.vue    | 19 ++++------
 client/src/views/Rules.vue       | 17 ++++-----
 client/src/views/Variants.vue    | 13 +++----
 9 files changed, 79 insertions(+), 92 deletions(-)
 create mode 100644 client/src/utils/afterRawLoad.js

diff --git a/client/package-lock.json b/client/package-lock.json
index 954dad64..d50195cc 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -575,18 +575,18 @@
       }
     },
     "@vue/cli-overlay": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-overlay/-/cli-overlay-4.5.8.tgz",
-      "integrity": "sha512-M6q4QPKQfErHk54ycxBASgBadgdsK45f6v2NWUTAiFXdTtqv1Z7gR8JAbQhiXbni/m9571bLJnZFv5c8uhAHnw==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-overlay/-/cli-overlay-4.5.9.tgz",
+      "integrity": "sha512-E2PWv6tCdUz+eEDj2Th2oxiKmzMe02qi0PcxiNaO7oaqggmEOrp1rLgop7DWpiLDBiqUZk2x0vjK/q2Tz8z/eg==",
       "dev": true
     },
     "@vue/cli-plugin-eslint": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-eslint/-/cli-plugin-eslint-4.5.8.tgz",
-      "integrity": "sha512-1lg3K2D/harXbN4FvRoA1b0X9947H+G4Ql/43rxJY39OqswfK/d1ck438Fo9M4l1+zhBSUNDmcjn7Q2EH6qWmA==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-eslint/-/cli-plugin-eslint-4.5.9.tgz",
+      "integrity": "sha512-wTsWRiRWPW5ik4bgtlh4P4h63Zgjsyvqx2FY0kcj+bSAnQGPJ3bKUOMU9KQP5EyNH6pAXMVGh2LEXK9WwJMf1w==",
       "dev": true,
       "requires": {
-        "@vue/cli-shared-utils": "^4.5.8",
+        "@vue/cli-shared-utils": "^4.5.9",
         "eslint-loader": "^2.2.1",
         "globby": "^9.2.0",
         "inquirer": "^7.1.0",
@@ -595,24 +595,24 @@
       }
     },
     "@vue/cli-plugin-router": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-router/-/cli-plugin-router-4.5.8.tgz",
-      "integrity": "sha512-tJwVLdX4jj0Ia/1hsBejRbo4gs0hb2z1z5ert+4Ve5RNdpOKUG69OaPQgXPqvuYSQh9MW7bqG0iJmYtVD+KBNw==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-router/-/cli-plugin-router-4.5.9.tgz",
+      "integrity": "sha512-eBBfbZpQ1sJrdlx8i7iReFxSnuzwmrv+s2OCT3kjBd6uWRqGnD4VihpS4srC7vZLzDQrDplumSn0a93L9Qf3wQ==",
       "dev": true,
       "requires": {
-        "@vue/cli-shared-utils": "^4.5.8"
+        "@vue/cli-shared-utils": "^4.5.9"
       }
     },
     "@vue/cli-plugin-vuex": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-vuex/-/cli-plugin-vuex-4.5.8.tgz",
-      "integrity": "sha512-wU9WPhay1zBxtdd/HLRYhFRLBbG9lw3YTBJep9sTnYwUeXiEgum4O88Q2j6cwBBPPHMgeMrKMPHS85Jf4hMc0g==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-plugin-vuex/-/cli-plugin-vuex-4.5.9.tgz",
+      "integrity": "sha512-mFNIJhYiJjzCgytkDHX00ROy5Yzl7prkZpUbeDE0biwcLteMf2s3qZVbESOQl6GcviqcfEt2f3tHQQtLNa+OLg==",
       "dev": true
     },
     "@vue/cli-service": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-service/-/cli-service-4.5.8.tgz",
-      "integrity": "sha512-YcVEJvA5vQOcfRzhCQDfHxRW9WHvKqlQeiuSvNddfV2uQqKD3ifhsNYiZZuFvbev9qgRUmtmxSafpwYzj/LLBw==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-service/-/cli-service-4.5.9.tgz",
+      "integrity": "sha512-E3XlfM0q+UnnjbC9rwLIWNo2umZCRwnlMJY0KOhY1hFvqisGIYzFmQQ4o01KGyTx2BZNMuQg7Kw+BZ5gyM1Wig==",
       "dev": true,
       "requires": {
         "@intervolga/optimize-cssnano-plugin": "^1.0.5",
@@ -621,10 +621,10 @@
         "@types/minimist": "^1.2.0",
         "@types/webpack": "^4.0.0",
         "@types/webpack-dev-server": "^3.11.0",
-        "@vue/cli-overlay": "^4.5.8",
-        "@vue/cli-plugin-router": "^4.5.8",
-        "@vue/cli-plugin-vuex": "^4.5.8",
-        "@vue/cli-shared-utils": "^4.5.8",
+        "@vue/cli-overlay": "^4.5.9",
+        "@vue/cli-plugin-router": "^4.5.9",
+        "@vue/cli-plugin-vuex": "^4.5.9",
+        "@vue/cli-shared-utils": "^4.5.9",
         "@vue/component-compiler-utils": "^3.1.2",
         "@vue/preload-webpack-plugin": "^1.1.0",
         "@vue/web-component-wrapper": "^1.2.0",
@@ -911,9 +911,9 @@
       }
     },
     "@vue/cli-shared-utils": {
-      "version": "4.5.8",
-      "resolved": "https://registry.npmjs.org/@vue/cli-shared-utils/-/cli-shared-utils-4.5.8.tgz",
-      "integrity": "sha512-pa6oenhBO/5HeDLRSokiwVN01gROACEDy3ESXWuPmragOREGNmmFKtkPHlqeYavGEX6LFp7f0VK3uMX6UYS5mQ==",
+      "version": "4.5.9",
+      "resolved": "https://registry.npmjs.org/@vue/cli-shared-utils/-/cli-shared-utils-4.5.9.tgz",
+      "integrity": "sha512-anvsrv+rkQC+hgxaT2nQQxnSWSsIzyysZ36LO7qPjXvDRBvcvKLAAviFlUkYbZ+ntbV8puzJ3zw+gUhQw4SEVA==",
       "dev": true,
       "requires": {
         "@hapi/joi": "^15.0.1",
@@ -3848,9 +3848,9 @@
       "dev": true
     },
     "electron-to-chromium": {
-      "version": "1.3.596",
-      "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.596.tgz",
-      "integrity": "sha512-nLO2Wd2yU42eSoNJVQKNf89CcEGqeFZd++QsnN2XIgje1s/19AgctfjLIbPORlvcCO8sYjLwX4iUgDdusOY8Sg==",
+      "version": "1.3.597",
+      "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.597.tgz",
+      "integrity": "sha512-VJI21MucKaqyFw0oe3j9BIg+nDF4MHzUZAmUwZzrxho+s8zPCD13Fds07Rgu+MTtAadO4tYTKFdAUksKYUyIJw==",
       "dev": true
     },
     "elliptic": {
@@ -7537,9 +7537,9 @@
       }
     },
     "node-releases": {
-      "version": "1.1.66",
-      "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.66.tgz",
-      "integrity": "sha512-JHEQ1iWPGK+38VLB2H9ef2otU4l8s3yAMt9Xf934r6+ojCYDMHPMqvCc9TnzfeFSP1QEOeU6YZEd3+De0LTCgg==",
+      "version": "1.1.67",
+      "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.67.tgz",
+      "integrity": "sha512-V5QF9noGFl3EymEwUYzO+3NTDpGfQB4ve6Qfnzf3UNydMhjQRVPR1DZTuvWiLzaFJYw2fmDwAfnRNEVb64hSIg==",
       "dev": true
     },
     "node-sass": {
diff --git a/client/package.json b/client/package.json
index 0e606bd7..dc407969 100644
--- a/client/package.json
+++ b/client/package.json
@@ -13,8 +13,8 @@
     "vue-router": "^3.4.9"
   },
   "devDependencies": {
-    "@vue/cli-plugin-eslint": "^4.5.8",
-    "@vue/cli-service": "^4.5.8",
+    "@vue/cli-plugin-eslint": "^4.5.9",
+    "@vue/cli-service": "^4.5.9",
     "ajv": "^6.12.6",
     "apply-loader": "^2.0.0",
     "babel-eslint": "^10.1.0",
diff --git a/client/src/utils/afterRawLoad.js b/client/src/utils/afterRawLoad.js
new file mode 100644
index 00000000..58bdb565
--- /dev/null
+++ b/client/src/utils/afterRawLoad.js
@@ -0,0 +1,10 @@
+export default function afterRawLoad(text) {
+  return (
+    text
+    // Next two lines fix a weird issue after last update (2019-11)
+    .replace(/\\n/g, " ")
+    .replace(/\\"/g, '"')
+    .replace('export default "', "")
+    .replace(/";$/, "")
+  );
+}
diff --git a/client/src/views/Analyse.vue b/client/src/views/Analyse.vue
index 175cc157..eb36422d 100644
--- a/client/src/views/Analyse.vue
+++ b/client/src/views/Analyse.vue
@@ -29,6 +29,7 @@ import BaseGame from "@/components/BaseGame.vue";
 import { processModalClick } from "@/utils/modalClick";
 import { replaceByDiag } from "@/utils/printDiagram";
 import { store } from "@/store";
+import afterRawLoad from "@/utils/afterRawLoad";
 export default {
   name: "my-analyse",
   // TODO: game import ==> require some adjustments, like
@@ -100,17 +101,12 @@ export default {
       })
       .catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
       this.rulesContent =
-        require(
-          "raw-loader!@/translations/rules/" +
-          this.gameRef.vname + "/" +
-          this.st.lang + ".pug"
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/rules/" +
+            this.gameRef.vname + "/" + this.st.lang + ".pug"
           ).default
-        .replace('export default "', "")
-        .replace(/";$/, "")
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
-        .replace(/(fen:)([^:]*):/g, replaceByDiag);
+        ).replace(/(fen:)([^:]*):/g, replaceByDiag);
     },
     loadGame: function(orientation) {
       this.game.vname = this.gameRef.vname;
diff --git a/client/src/views/Faq.vue b/client/src/views/Faq.vue
index 71647173..05b1e25a 100644
--- a/client/src/views/Faq.vue
+++ b/client/src/views/Faq.vue
@@ -7,6 +7,7 @@ main
 
 <script>
 import { store } from "@/store";
+import afterRawLoad from "@/utils/afterRawLoad";
 export default {
   name: "my-faq",
   data: function() {
@@ -14,14 +15,12 @@ export default {
   },
   computed: {
     content: function() {
-      // (AJAX) Request to get FAQ content (plain text, HTML)
       return (
-        require("raw-loader!@/translations/faq/" + this.st.lang + ".pug")
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
-        .replace('module.exports = "', "")
-        .replace(/"$/, "")
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/faq/" + this.st.lang + ".pug"
+          ).default
+        )
       );
     }
   },
diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue
index 9094309b..d66716c2 100644
--- a/client/src/views/Game.vue
+++ b/client/src/views/Game.vue
@@ -196,6 +196,7 @@ import { getDiagram, replaceByDiag } from "@/utils/printDiagram";
 import { processModalClick } from "@/utils/modalClick";
 import { playMove, getFilteredMove } from "@/utils/playUndo";
 import { ArrayFun } from "@/utils/array";
+import afterRawLoad from "@/utils/afterRawLoad";
 import params from "@/parameters";
 export default {
   name: "my-game",
@@ -1266,19 +1267,13 @@ export default {
         window.V = vModule[game.vname + "Rules"];
         this.loadGame(game, callback);
       });
-      // (AJAX) Request to get rules content (plain text, HTML)
       this.rulesContent =
-        require(
-          "raw-loader!@/translations/rules/" +
-          game.vname + "/" +
-          this.st.lang + ".pug"
-        )
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
-        .replace('module.exports = "', "")
-        .replace(/"$/, "")
-        .replace(/(fen:)([^:]*):/g, replaceByDiag);
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/rules/" +
+            game.vname + "/" + this.st.lang + ".pug"
+          ).default
+        ).replace(/(fen:)([^:]*):/g, replaceByDiag);
     },
     // 3 cases for loading a game:
     //  - from indexedDB (running or completed live game I play)
diff --git a/client/src/views/Problems.vue b/client/src/views/Problems.vue
index adc0ad36..a55126fa 100644
--- a/client/src/views/Problems.vue
+++ b/client/src/views/Problems.vue
@@ -131,6 +131,7 @@ import params from "@/parameters";
 import { getDiagram, replaceByDiag } from "@/utils/printDiagram";
 import { processModalClick } from "@/utils/modalClick";
 import { ArrayFun } from "@/utils/array";
+import afterRawLoad from "@/utils/afterRawLoad";
 import BaseGame from "@/components/BaseGame.vue";
 export default {
   name: "my-problems",
@@ -313,19 +314,13 @@ export default {
         this.loadedVar = vid;
         cb();
       });
-      // (AJAX) Request to get rules content (plain text, HTML)
       this.rulesContent =
-        require(
-          "raw-loader!@/translations/rules/" +
-          variant.name + "/" +
-          this.st.lang + ".pug"
-        )
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
-        .replace('module.exports = "', "")
-        .replace(/"$/, "")
-        .replace(/(fen:)([^:]*):/g, replaceByDiag);
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/rules/" + variant.name + "/" +
+            this.st.lang + ".pug"
+          ).default
+        ).replace(/(fen:)([^:]*):/g, replaceByDiag);
     },
     trySetDiagram: function(prob) {
       // Problem edit: FEN could be wrong or incomplete,
diff --git a/client/src/views/Rules.vue b/client/src/views/Rules.vue
index 2871f3f3..2edd8a88 100644
--- a/client/src/views/Rules.vue
+++ b/client/src/views/Rules.vue
@@ -44,6 +44,7 @@ import ComputerGame from "@/components/ComputerGame.vue";
 import { store } from "@/store";
 import { replaceByDiag } from "@/utils/printDiagram";
 import { CompgameStorage } from "@/utils/compgameStorage";
+import afterRawLoad from "@/utils/afterRawLoad";
 export default {
   name: "my-rules",
   components: {
@@ -77,19 +78,13 @@ export default {
     },
     content: function() {
       if (!this.gameInfo.vname) return ""; //variant not set yet
-      // (AJAX) Request to get rules content (plain text, HTML)
       return (
-        require(
-          "raw-loader!@/translations/rules/" +
-          this.gameInfo.vname + "/" +
-          this.st.lang + ".pug"
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/rules/" +
+            this.gameInfo.vname + "/" + this.st.lang + ".pug"
           ).default
-        .replace('export default "', "")
-        .replace(/";$/, "")
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
-        .replace(/(fen:)([^:]*):/g, replaceByDiag)
+        ).replace(/(fen:)([^:]*):/g, replaceByDiag)
       );
     }
   },
diff --git a/client/src/views/Variants.vue b/client/src/views/Variants.vue
index f0a98edd..da6478b4 100644
--- a/client/src/views/Variants.vue
+++ b/client/src/views/Variants.vue
@@ -9,6 +9,7 @@ main
 
 <script>
 import { store } from "@/store";
+import afterRawLoad from "@/utils/afterRawLoad";
 export default {
   name: "my-variants",
   data: function() {
@@ -18,16 +19,12 @@ export default {
   },
   computed: {
     content: function() {
-      // (AJAX) Request to get rules content (plain text, HTML)
       return (
-        require(
-          "raw-loader!@/translations/variants/" + this.st.lang + ".pug"
+        afterRawLoad(
+          require(
+            "raw-loader!@/translations/variants/" + this.st.lang + ".pug"
           ).default
-        .replace('export default "', "")
-        .replace(/";$/, "")
-        // Next two lines fix a weird issue after last update (2019-11)
-        .replace(/\\n/g, " ")
-        .replace(/\\"/g, '"')
+        )
       );
     }
   },
-- 
2.44.0