PREFIX st:   <http://ns.inria.fr/sparql-template/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX fun: <http://w3id.org/sparql-generate/fn/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX ex: <http://example.org/>

TEMPLATE <term/main.rqg>( ?term ) {  

  TEMPLATE <header.rqg>( st:call-template(<term/label.rqg>, ?term ) , ?pathToStatic , false ).
 
""" 
<header id="g-header">
  <div class="g-container">
    <div class="g-block size-100">
      <div class="g-content g-particle">
        <h1><a href="{?term}">{?prefix}{?localName}</a> ({ TEMPLATE <term/label.rqg>( ?term ) . }{ 
            TEMPLATE { 
              ' <span class="alert alert-danger" role="alert"> &#9888; { fun:property(<en.properties>, 'deprecated') }</span>'
            } WHERE {
              ?term owl:deprecated true .
            } .
          })</h1>
        <p><em>Defined in <a href="{?ontoRef}">{?ontoRef}</a> as: </em></p>
        <p>{ TEMPLATE { ?description ; separator = "<br/>\n"} WHERE { ?term rdfs:comment ?description } . }</p>
        <dl>
          <dt>IRI:</dt><dd><a href='{?term}'>{?term}</a></dd>
          {
           TEMPLATE { 
            before = "<dt>Is defined by:</dt><dd>"; 
            """<a href="{?onto}">{?onto}</a>"""
            ; separator = "<br/>" ; after="</dd>" } 
           WHERE { ?term rdfs:isDefinedBy ?onto } 
           ORDER BY DESC( ?onto ) .  
           }</dd>
          {
           TEMPLATE { 
            before = "<dt>Is used by:</dt><dd>"; 
            """<a href="{?onto}">{?onto}</a>""" 
            ; separator="<br/>" ; after="</dd>" } 
           WHERE { ?term ex:isUsedBy ?onto } 
           ORDER BY DESC( ?onto ) . 
           }
           <dt>Download serialization:</dt><dd>
            <span><a href="{ ?localName }.jsonld" target="_blank"><img src="https://img.shields.io/badge/Format-JSON--LD-blue.svg"/></a></span>
            <span><a href="{ ?localName }.n3" target="_blank"><img src="https://img.shields.io/badge/Format-N3-blue.svg"/></a></span>
            <span><a href="{ ?localName }.nt" target="_blank"><img src="https://img.shields.io/badge/Format-N--Triples-blue.svg"/></a></span>
            <span><a href="{ ?localName }.rdf" target="_blank"><img src="https://img.shields.io/badge/Format-RDF/XML-blue.svg"/></a></span>
            <span><a href="{ ?localName }.ttl" target="_blank"><img src="https://img.shields.io/badge/Format-Turtle-blue.svg"/></a></span>
           </dd>
           <dt>License:</dt><dd>
            <span><a href="https://forge.etsi.org/etsi-software-license" target="_blank"><img src="https://img.shields.io/badge/License-https://forge.etsi.org/etsi--software--license-blue.svg"/></a></span>
           </dd>
         </dl>
      </div>
    </div>
  </div>
</header>
<section>
  <div class="g-container">
    <div class="g-content"> 
         { TEMPLATE{ TEMPLATE <term/class.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:Class } . 
         }
         { TEMPLATE{ TEMPLATE <term/objectProperty.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:ObjectProperty } .
         }
         { TEMPLATE{ TEMPLATE <term/dataProperty.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:DatatypeProperty } .
         }
         { TEMPLATE{ TEMPLATE <term/individual.rqg>( ?term ) . }
           FROM ?ontoRef 
           WHERE { ?term a owl:NamedIndividual } .
         }
    </div>
  </div>
</section>
"""

TEMPLATE <footer.rqg>( ?pathToStatic , false ) .

}
FROM ?term
WHERE {

 BIND( fun:regex( str(?term), "^https://saref.etsi.org/([a-z4]+)/([^/]+)$", 1 ) AS ?ext )
 BIND( IF( ?ext = "core" , "saref:" , "s{SUBSTR(?ext,6)}:" ) AS ?prefix )
 BIND( fun:regex( str(?term), "^https://saref.etsi.org/([a-z4]+)/([^/]+)$", 2 ) AS ?localName )
 BIND( fun:regex( str(?term), "^(https://saref.etsi.org/[a-z4]+/)[^/]+$", 1 ) AS ?namespace )
 BIND( "../static" AS ?static )

 ?term rdfs:isDefinedBy ?ontoRef

 BIND( "../static" AS ?pathToStatic )

}
ORDER BY DESC( ?ontoRef )
LIMIT 1
