From a309a7b23aae4ba798b60d64c771b778a489c2c7 Mon Sep 17 00:00:00 2001 From: eliott <eliott.sammier@tetras-libre.fr> Date: Fri, 28 Jun 2024 17:42:46 +0200 Subject: [PATCH] Rework templates in a hierarchical manner - Implement the st:start template to define which nodes to start the transformation with. In this case, filter top-level modules. - Add a named template mt:module, called for Modules and SousParties, which generates the module's index file then recurses on its descendants (modules or activities) - Activity template is now named and explicitly called instead of being the entrypoint - New function mt:start_marker helps to generate the file separator marker from any template. This rework changes the output format and is still in progress; Python-side parsing of the STTL output is broken, will be reworked as well --- .../macao_12/script/src/export_corese.py | 1 + .../macao_12/script/templates/activite.rq | 47 +++++++++---------- .../macao_12/script/templates/functions.rq | 13 ++++- .../macao_12/script/templates/module.rq | 24 ++++++++++ .../macao_12/script/templates/start.rq | 14 ++++++ 5 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 tetras_extraction/macao_12/script/templates/module.rq create mode 100644 tetras_extraction/macao_12/script/templates/start.rq diff --git a/tetras_extraction/macao_12/script/src/export_corese.py b/tetras_extraction/macao_12/script/src/export_corese.py index ac8292c8..19f42d7f 100644 --- a/tetras_extraction/macao_12/script/src/export_corese.py +++ b/tetras_extraction/macao_12/script/src/export_corese.py @@ -46,4 +46,5 @@ def apply_templates() -> str: # ==> Run STTL transformation tr = Transformer.create(graph, MODULE_DIR + "/../templates/") result = tr.transform() + print(result) return result diff --git a/tetras_extraction/macao_12/script/templates/activite.rq b/tetras_extraction/macao_12/script/templates/activite.rq index 781318af..c104b543 100644 --- a/tetras_extraction/macao_12/script/templates/activite.rq +++ b/tetras_extraction/macao_12/script/templates/activite.rq @@ -3,38 +3,37 @@ PREFIX mt: <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/template/> PREFIX st: <http://ns.inria.fr/sparql-template/> PREFIX owl: <http://www.w3.org/2002/07/owl#> -# Entry point of the template set. # Generic Activity template that creates a Markdown page with Hugo front-matter, # and calls other specific templates when necessary -template { - "####### " st:call-template(mt:path, ?subj) - format { - """ -+++ -title = "%s" -weight = %s0 -+++ +template mt:activite(?act) { + mt:start_marker(?act) "/index.md" st:nl() +# format { +# """ +# +++ +# title = "%s" +# weight = %s0 +# +++ -%s -""" - ?title - ?weight - ?desc - } - # Insert a Hugo shortcode to load the quiz if this is a quiz activity - if (exists { ?subj a :Exercice }, "\n{{< quiz >}}", "") - "%%%%%%%" - st:call-template(mt:quiz,?subj) +# %s +# """ +# ?title +# ?weight +# ?desc +# } +# # 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) } where { - ?subj a :Activite . - ?subj :id ?id . - ?subj :titre ?title . - ?subj :description ?desc . + ?act a :Activite . + ?act :id ?id . + ?act :titre ?title . + ?act :description ?desc . optional { # Turn the page index into a Hugo weight: increment and add a zero, to # leave room for adding new pages in-between later - ?subj :index ?index . + ?act :index ?index . bind(?index + 1 as ?weight) } } diff --git a/tetras_extraction/macao_12/script/templates/functions.rq b/tetras_extraction/macao_12/script/templates/functions.rq index dce99d75..7c3ce17e 100644 --- a/tetras_extraction/macao_12/script/templates/functions.rq +++ b/tetras_extraction/macao_12/script/templates/functions.rq @@ -16,16 +16,25 @@ function st:process(?x) { # st:format("%s", ?x), substr(?x, 0 , 40), if (isURI(?x), - replace(?x, :, ":"), + # Explicitly show when no template was found for a URI by printing + # it with a "default" marker + st:format("[default %s]", replace(?x, :, ":")), st:turtle(?x) ) ) } +# Generates the marker that indicates the start of a new file to create. +# The marker begins with a constant distinctive string, then the node's path +# built with the mt:path template. +function mt:start_marker(?node) { + concat("####### ", st:call-template(mt:path, ?node)) +} + # JSON lines often end with a comma, and st:nl() is a newline # with the current indent level (see the docs). function mt:sep() { - st:format(",%s", st:nl()) + concat(",", st:nl()) } # Escape illegal characters for JSON strings diff --git a/tetras_extraction/macao_12/script/templates/module.rq b/tetras_extraction/macao_12/script/templates/module.rq new file mode 100644 index 00000000..2b656c5d --- /dev/null +++ b/tetras_extraction/macao_12/script/templates/module.rq @@ -0,0 +1,24 @@ +PREFIX : <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/> +PREFIX mt: <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/template/> +PREFIX st: <http://ns.inria.fr/sparql-template/> + +template mt:module(?mod) { + mt:start_marker(?mod) "/_index.md" st:nl() + + group { + st:call-template(mt:module, ?child) ; separator='' + } + group { + st:call-template(mt:activite, ?child_act) ; separator='' + } +} where { + { ?mod a :Module } union { ?mod a :SousPartie } . + ?mod :titre ?title . + + optional { + { ?mod :contientModule ?child } union { ?mod :contientSousPartie ?child } + } + optional { + ?mod :contientActivite ?child_act + } +} diff --git a/tetras_extraction/macao_12/script/templates/start.rq b/tetras_extraction/macao_12/script/templates/start.rq new file mode 100644 index 00000000..e623679a --- /dev/null +++ b/tetras_extraction/macao_12/script/templates/start.rq @@ -0,0 +1,14 @@ +PREFIX : <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/> +PREFIX mt: <http://www.semanticweb.org/eliott/ontologies/2024/4/macao/template/> +PREFIX st: <http://ns.inria.fr/sparql-template/> + +# Entry point of the template set. +# Begins with the top-level modules +template st:start { + st:call-template(mt:module, ?top_module) +} +where { + ?top_module a :Module . + # Top level <=> not contained in another module + FILTER NOT EXISTS { ?mod :contientModule ?top_module } +} \ No newline at end of file -- GitLab