word: common language handling in quiz
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 15 Nov 2021 16:43:40 +0000 (17:43 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 6 Dec 2021 11:07:29 +0000 (12:07 +0100)
Replace hardcoded nl include by same ?lang result as overview.

word.plp
word/quiz.js
word/quiz.plp

index 64a093fed42afa818d8185450b8815e80e82cedd..41d2bd330aa80a5d78b2a81c3a9e08284900ffdd 100644 (file)
--- a/word.plp
+++ b/word.plp
@@ -1,5 +1,8 @@
 <(common.inc.plp)><:
 
+our $lang = $get{lang} || 'en';
+our $wordlistbase = "data/wordlist.$lang";
+
 if ($Request and $Request =~ m{\A([^/]+)}) {
        my $page = "word/$1.plp";
        utf8::downgrade($page); # unicode filename breaks contents encoding
@@ -9,8 +12,7 @@ if ($Request and $Request =~ m{\A([^/]+)}) {
        }
 }
 
-my $lang = $get{lang} || 'en';
-my $wordlist = "data/wordlist.$lang.inc.pl";
+my $wordlist = "$wordlistbase.inc.pl";
 my $limit = $get{v} // (exists $get{v} ? 4 : 3);
 
 Html({
index d2a581824d6feb4f271af1524140c720e1eb968d..0518fa14bdbc72c386f1b5abe4554c1f05d30965 100644 (file)
@@ -21,9 +21,8 @@ class Quiz {
                });
        }
 
-       constructor() {
-               this.dataurl = '/data/wordlist.nl.json';
-               fetch(this.dataurl).then(res => res.json()).then(json => {
+       constructor(dataurl) {
+               fetch(dataurl).then(res => res.json()).then(json => {
                        this.form = document.getElementById('quiz');
                        this.words = Object.values(json)
                                .sort(() => {return .5 - Math.random()}) // shuffle
@@ -32,5 +31,3 @@ class Quiz {
                });
        }
 };
-
-new Quiz();
index 7fc375e8fcd5c81519ae1ae015c5586e7237904b..dc0e1e06ecf5a4771224f87f49f3b8a9ff2ccfde 100644 (file)
@@ -1,5 +1,7 @@
 <(../common.inc.plp)><:
 
+our $wordlistbase;
+
 Html({
        raw => <<'EOT',
 <script src="/word/put.min.js"></script>
@@ -23,4 +25,6 @@ li.good {background: #0F08}
 </style>
 EOT
 });
+
 say '<h1 id=quiz>quiz</h1>';
+say "<script>new Quiz('/$wordlistbase.json')</script>";