Skip to content
Snippets Groups Projects
Commit a309a7b2 authored by Eliott Sammier's avatar Eliott Sammier
Browse files

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
parent 781dfea0
Branches
No related tags found
No related merge requests found
......@@ -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
......@@ -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)
}
}
......
......@@ -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
......
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
}
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment