Commit 79ff856a authored by Guillaume Porte's avatar Guillaume Porte 😶
Browse files

Récupération / nettoyage / traitement des métadonnées depuis un fragment HTML...

Récupération / nettoyage / traitement des métadonnées depuis un fragment HTML ou un doc XML et renvois d'un map{}
parent daca1e59
Loading
Loading
Loading
Loading
+161
Changes for rxq/htt.xqm: 161 added lines, 0 removed lines.
Original line number Diff line number Diff line
(: For conditions of distribution and use, see the accompanying legal.txt file. :)

module namespace max.hht = 'pddn/max/hht';
import module namespace max.cons = 'pddn/max/cons' at 'cons.xqm';
import module namespace max.util = 'pddn/max/util' at 'util.xqm';
import module namespace max.config = 'pddn/max/config' at 'config.xqm';

import module namespace request = "http://exquery.org/ns/request";


(: 1.1 get default html head tags for pages without fragment nor xml doc :)
declare function max.hht:getHtmlHeadTags_Page($project, $pageType, $pageName, $defaultHtmlHeadTags){
    map:merge((
      max.hht:mapHtmlHeadTags(
        $project, $pageName, max.hht:edition($defaultHtmlHeadTags, $project, $pageType) ), 
      max.hht:mapHtmlHeadTags(
        $project, $pageName, max.hht:base($defaultHtmlHeadTags, $pageType) )
    )) 
};

(: 1.2 get default html head tags for HTML fragment :)
declare function max.hht:getHtmlHeadTags_HTML($project, $pageType, $pageName, $defaultHtmlHeadTags){
  let $fragment := doc(max.util:maxHome() || "/editions/" || $project || "/fragments/" || $pageName || ".frag.html")
  return max.hht:metaFromFragment($project, $defaultHtmlHeadTags, $pageType, $fragment, '')
};

(: 1.3 get default html head tags for XML document :)
declare function max.hht:getHtmlHeadTags_XML($project, $pageType, $pageName, $id as xs:string *, $defaultHtmlHeadTags){
    let $env := max.config:getXMLFormat($project)
    let $fragment := (
      if($env = 'tei')
      then ( collection(concat(max.config:getProjectDBPath($project), '/', $pageName))//*:TEI )
      else if ($env = 'ead')
      then ( "" )
      else( "" )
    )
    return max.hht:metaFromFragment($project, $defaultHtmlHeadTags, $env, $fragment, $id)
};

(: 1.4 get default html head tags for XML and HTML :)
declare function max.hht:metaFromFragment($project, $defaultHtmlHeadTags, $type, $fragment, $id as xs:string *){
  map:merge((
    max.hht:mapHtmlHeadTagsWithFragment(
      $project, $fragment, max.hht:edition($defaultHtmlHeadTags, $project, $type), $id), 
    max.hht:mapHtmlHeadTagsWithFragment(
      $project, $fragment, max.hht:base($defaultHtmlHeadTags, $type), $id)
  )) 
};

(: 2.1 default / common tags  :)
declare function max.hht:base($defaultHtmlHeadTags, $pageType) {
  $defaultHtmlHeadTags/common/source[@type=$pageType]/tag
};

(: 2.2 tags for edition :)
declare function max.hht:edition($defaultHtmlHeadTags, $project, $pageType) {
  $defaultHtmlHeadTags/editions/edition[@xml:id=$project]/source[@type=$pageType]/tag
};

(: 3.1 map html head meta tags :)
declare function max.hht:mapHtmlHeadTags($project, $pageName, $path){
  map:merge(

    for $defaultTag in $path
    let $valueTmp :=(
      if(contains($defaultTag/@key, "title") )
      then(concat(upper-case(substring($pageName, 1, 1)), lower-case(substring($pageName, 2))))
      else(string($defaultTag/@default))
    )
    return max.hht:mapEntry($project, $defaultTag, $valueTmp)
  )
};

(: 3.2 map html head meta tags for fragments and xml doc :)
declare function max.hht:mapHtmlHeadTagsWithFragment($project, $fragment, $path, $id as xs:string *){
  map:merge(

    for $defaultTag in $path
      let $valueTmp := (
        if($defaultTag/@path != "")
        then(
          let $xpath := replace($defaultTag/@path, '%id%', $id)
          return
            try{
              if($defaultTag/@length != "" and $defaultTag/@length > 0)
              then( concat(substring(max.hht:evalXpath($xpath, $fragment), 0, $defaultTag/@length), '...') ) 
              else( max.hht:evalXpath($xpath, $fragment) )
            } catch *{}
        )
        else("")
      )

      let $valueTmp := (
        if($valueTmp = '...')
        then("")
        else($valueTmp)
      )

      let $valueTmp2 :=(
        if($defaultTag/@default != "" and $valueTmp = "" )
        then(data($defaultTag/@default))
        else($valueTmp)
      )

      return max.hht:mapEntry($project, $defaultTag, $valueTmp2)
  )
};

(: 4 functions :)
declare function max.hht:evalXpath($xpath, $fragment){
  string-join(xquery:eval($xpath, map { '' : $fragment }), " | ")
};

declare function max.hht:mapEntry($project, $defaultTag, $tempValue){
  let $value := string(max.hht:setValue($project, $defaultTag, $tempValue))
  return 
    if(string($defaultTag/@key) = 'title')
    then(map:entry('title', max.hht:cleanValue($value) ))
    else(map:entry(string($defaultTag/@key), 
      map{"value" : max.hht:cleanValue($value)
    }))
};

declare function max.hht:setValue($project, $defaultTag, $tempValue){
  if($defaultTag/@template != "")
  then(max.hht:htmlHeadTagsTemplate($project, string($defaultTag/@template), $tempValue) )
  else($tempValue)
};

declare function max.hht:cleanValue($value){
  let $value := replace($value, '
', ' ')
  let $value := replace($value, '	', ' ')
  let $value := replace($value, '\s{2,}', '')
  return $value
};

(: 5 html head tags template:)
declare function max.hht:htmlHeadTagsTemplate($project, $template, $value){

  let $scheme := request:scheme()
  let $port := request:port()
  let $hostname := request:hostname()
  let $path := request:path()

  let $url := (
    if(fn:exists(string($port)) = xs:boolean(1))
    then(concat($scheme, "://", $hostname, ":", string($port)))
    else(concat($scheme, "://", $hostname))
  )

  let $projectName := max.config:getProjectPrettyName($project)
  let $return := $template
  let $return := replace($return, "%value%", $value)
  let $return := replace($return, "%project%", string($projectName))
  let $return := replace($return, "%url%", $url || $path )
  let $return := replace($return, "%image%", $url || $value )

  return 
    $return

};