From 0cfed7902facbe3c87795ea0b0af63017fcb7d8d Mon Sep 17 00:00:00 2001
From: eliott <eliott.sammier@tetras-libre.fr>
Date: Thu, 1 Aug 2024 10:30:53 +0200
Subject: [PATCH] A bit of cleaning on the STTL templates

---
 .../script/templates/activite.rq              | 20 ++++++++-----------
 tetras_extraction/script/templates/array.rq   | 11 ----------
 tetras_extraction/script/templates/qcu.rq     |  2 +-
 tetras_extraction/script/templates/quiz.rq    |  4 ++--
 4 files changed, 11 insertions(+), 26 deletions(-)
 delete mode 100644 tetras_extraction/script/templates/array.rq

diff --git a/tetras_extraction/script/templates/activite.rq b/tetras_extraction/script/templates/activite.rq
index 243f45e8..485db416 100644
--- a/tetras_extraction/script/templates/activite.rq
+++ b/tetras_extraction/script/templates/activite.rq
@@ -6,19 +6,15 @@ PREFIX owl: <http://www.w3.org/2002/07/owl#>
 # Generic Activity template that creates a Markdown page with Hugo front-matter,
 # and calls other specific templates when necessary
 template mt:activite(?act) {
-    mt:start_marker(?act) "/index.md" st:nl()
-    format {
-        """+++
-title = "%s"
-weight = %s0
-+++
+    mt:start_marker(?act) "/index.md"                                    st:nl()
+    '+++'                                                                st:nl()
+    'title = "' ?title '"'                                               st:nl()
+    # This is how optional variables (may be unbound) are handled
+    coalesce(st:format('weight = %s\n', ?weight * 10), "")
+    '+++'                                                        st:nl() st:nl()
 
-%s
-"""
-        ?title
-        ?weight
-        ?desc
-    }
+    ?desc                                                                st:nl()
+ 
     # Insert a Hugo shortcode to load the quiz if this is a quiz activity
     if (exists { ?act a :Exercice }, "\n{{< quiz >}}", "")
     st:call-template(mt:quiz, ?act)
diff --git a/tetras_extraction/script/templates/array.rq b/tetras_extraction/script/templates/array.rq
deleted file mode 100644
index 9bc0ddd9..00000000
--- a/tetras_extraction/script/templates/array.rq
+++ /dev/null
@@ -1,11 +0,0 @@
-PREFIX : <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/>
-PREFIX testtl: <http://www.semanticweb.org/eliott/test/>
-PREFIX st: <http://ns.inria.fr/sparql-template/>
-
-template testtl:array(?qcu) {
-    format { '"%s"' ?html }
-}
-where {
-    ?qcu :aReponse ?choice .
-    ?choice :html ?html
-}
diff --git a/tetras_extraction/script/templates/qcu.rq b/tetras_extraction/script/templates/qcu.rq
index 63cf76e1..e55e895f 100644
--- a/tetras_extraction/script/templates/qcu.rq
+++ b/tetras_extraction/script/templates/qcu.rq
@@ -23,7 +23,7 @@ template mt:qcu(?qcu) {
         }
     }
     ']' mt:sep()
-    '"correctAnswer": ' if(bound(?correct_choice_index), ?correct_choice_index, "-1")
+    '"correctAnswer": ' coalesce(?correct_choice_index, "-1")
 }
 where {
     ?qcu a :ExerciceQC_QCU
diff --git a/tetras_extraction/script/templates/quiz.rq b/tetras_extraction/script/templates/quiz.rq
index 4d3f573b..95f4af23 100644
--- a/tetras_extraction/script/templates/quiz.rq
+++ b/tetras_extraction/script/templates/quiz.rq
@@ -17,12 +17,12 @@ template mt:quiz(?quiz) {
                 # Unfortunately, STTL doesn't have a concise syntax for blocks
                 # that depend on a variable being bound, we have to use 
                 # SPARQL functions and st:format
-                if (bound(?correct_comment), st:format(
+                coalesce(st:format(
                     '%s"correctComment": "%s"',
                     mt:sep(),
                     mt:json_escape(?correct_comment)
                 ), "")
-                if (bound(?incorrect_comment), st:format(
+                coalesce(st:format(
                     '%s"incorrectComment": "%s"',
                     mt:sep(),
                     mt:json_escape(?incorrect_comment)
-- 
GitLab