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 dctype: <http://purl.org/dc/dcmitype/>
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/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 

BASE <https://saref.etsi.org/documentation/>

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

  TEMPLATE <header.rqg>( st:call-template(<term/label.rqg>, ?example ) , ?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="{?example}">{ TEMPLATE <example/title.rqg>( ?example ) . }</a></h1>
        <p>{ TEMPLATE <example/description.rqg>( ?example ) . }</p>
        <dl>
          <dt>IRI:</dt><dd><a href='{?example}'>{?example}</a></dd>
          {
           TEMPLATE { 
            before = "<dt>Conforms to:</dt><dd>"; 
            """<a href="{?other}">{?other}</a>"""
            ; separator = "<br/>" ; after="</dd>" } 
           WHERE { ?term dcterms:conformsTo ?other } 
           ORDER BY DESC( ?other ) .  
           }
           <dt>Download serialization:</dt><dd>
            <span><a href="{ ?name }.jsonld" target="_blank"><img src="https://img.shields.io/badge/Format-JSON--LD-blue.svg"/></a></span>
            <span><a href="{ ?name }.n3" target="_blank"><img src="https://img.shields.io/badge/Format-N3-blue.svg"/></a></span>
            <span><a href="{ ?name }.nt" target="_blank"><img src="https://img.shields.io/badge/Format-N--Triples-blue.svg"/></a></span>
            <span><a href="{ ?name }.rdf" target="_blank"><img src="https://img.shields.io/badge/Format-RDF/XML-blue.svg"/></a></span>
            <span><a href="{ ?name }.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>
"""


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

}
FROM ?example
WHERE {

 BIND( fun:regex( str(?example), "^https://saref.etsi.org/([a-z4]+)/(v[0-9]+\\.[0-9]+\\.[0-9]+)/example/([^#]+)#$", 1 ) AS ?path )
 BIND( fun:regex( str(?example), "^https://saref.etsi.org/([a-z4]+)/(v[0-9]+\\.[0-9]+\\.[0-9]+)/example/([^#]+)#$", 2 ) AS ?versionInfo )
 BIND( fun:regex( str(?example), "^https://saref.etsi.org/([a-z4]+)/(v[0-9]+\\.[0-9]+\\.[0-9]+)/example/([^#]+)#$", 3 ) AS ?name )

 BIND( IF( ?path = "core" , "SAREF" , "SAREF{UCASE(SUBSTR(?path,6))}" ) AS ?project )
 BIND( IF( ?path = "core" , "saref:" , "s{SUBSTR(?path,6)}:" ) AS ?prefix )

 BIND( IRI( fun:regex( str(?example), "^(https://saref.etsi.org/[a-z4]+/v[0-9]+\\.[0-9]+\\.[0-9]+/)example/[^#]+#$", 1 ) ) AS ?onto )

 ?example a dctype:Dataset

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

}
ORDER BY DESC( ?ontoRef )
LIMIT 1
