Commit 1b549e54 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

routing update. Generic entry point which now calls vocaulary:xml-html function. #14

parent ed28f166
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -29,14 +29,17 @@ Note : `.max/basex/webapp/max/` pointe vers `src/main`, ce symlink est automat

- `src` : toutes les sources
  - `main` : sources applicatives
    - `core`
    - `bundles`
    - `core` : modules du coeur applicatif
    - `webapp` : sources de la partie webapp
      - `max`
        - `bundles` : bundles natifs
        - `routes.xq` : routing
  - `resources` : ressources additionnelles  
  - `test` : source des tests (@todo)

Ce dépôt contient les bundles 
 - `max-dev` : aide au développement ([README](src/main/max/bundles/max-dev/README.md)).
 - `max-sources` : export des sources XML ([README](src/main/max/bundles/max-sources/README.md)).
 - `max-dev` : aide au développement ([README](src/main/webapp/max/bundles/max-dev/README.md)).
 - `max-export` : export des sources XML au format zip ([README](src/main/webapp/max/bundles/max-export/README.md)).
 - `max-dumb-xml` : bundle de vocabulaire d'exemple.


+19 −0
Original line number Diff line number Diff line
@@ -32,6 +32,25 @@ declare function utils:doc-to-html($collection as xs:string?, $doc as xs:string,

};

(:~ Returns the result of the 'xml-to-html' function of the active vocabulary bundle
 : @param $lang language
 : @param $path document path
 : @param $id fragment id, optionnal
 : @return HTML page
 :)
declare function utils:xml-to-html($lang as xs:string, $path as xs:string, $id as xs:string? := ())  as element(html) {
    (:
     - execute xml-to-html func of the active vocabulary bundle
    :)

    let $ns := conf:get-vocabulary-namespace()
    let $xqFilePath := utils:get-bundles-path() || conf:get-vocabulary-bundle() || '/' || conf:get-vocabulary-xq-file()
    let $declaration := "declare variable $path external; declare variable $lang external; declare variable $id external;"
    let $import := "import module namespace vocabulary = '"|| $ns ||"' at '" || $xqFilePath ||"';"
    let $query := 'vocabulary:xml-to-html($lang, $path, $id)'
    return xquery:eval($import ||  $declaration || $query, map {'lang': $lang, 'path' : $path, 'id': $id})
};

(:declare function utils:eval-bundle-function($bundle as xs:string, $function as xs:string){
    try{
        let $ns := conf:get-bundle-namespace($bundle)
+22 −23
Original line number Diff line number Diff line
@@ -32,28 +32,27 @@ declare function dumb-xml:doc-to-html($document as document-node(), $lang as xs:
    return templating:render('page.html', map {'content': $content, 'lang': $lang})
};

declare function dumb-xml:xml-to-html($lang as xs:string, $path as xs:string, $id as xs:string? := ()) as element(html){

(:~
 : @return an identified node as dumb html
 :)
declare
%rest:GET
%output:method("html")
%output:html-version("5.0")
%output:encoding("UTF-8")
%rest:path("/{$lang=[a-z]{2}}/{$docPath=.+}/id/{$id}.html")
function dumb-xml:id-to-html($lang as xs:string, $docPath as xs:string, $id as xs:string){
   try {
    let $node := database:get-element-by-id($docPath || '.xml', $id)
    let $content := <div>
                        <h1>id: {$id}</h1>
                        {
                            if($node) then $node else 'Node with id "' || $id || '" was not found'
                        }
         let $customXQ := file:current-dir()|| 'xq/document.xq'
         let $content := if(file:exists($customXQ) and not($id))
                         then xquery:eval(xs:anyURI($customXQ), map {'path': $path, 'lang': $lang})
                         else
                             (:generate random http link to an identified node in $document:)
                             let $identifiers:= database:get-document($path)//*/@xml:id/string()
                             let $n := count($identifiers)
                             let $r := random:integer($n) + 1
                             let $target := $identifiers[$r]
                             let $href := fn:replace(request:path(),'.html', '') || '/id/' || $target || '.html'
                             return
                                 <div>
                                 <h2>{i18n:translate('raw', $lang)} | {fn:replace(fn:base-uri($path), '/' || $database:max-db || '/', '')}</h2>
                                     {database:get-document($path)}
                                     <nav>
                                         <h3><a href="{$href}">{i18n:translate('random-link', $lang)} ({$target})</a></h3>
                                     </nav>
                                 </div>
         return templating:render('page.html', map {'content': $content, 'lang': $lang})
   }
   catch * {
        errors:error($err:code, $err:description, $err:line-number, $lang)
   }
};

+20 −36
Original line number Diff line number Diff line
@@ -51,14 +51,29 @@ declare
%rest:GET
%output:method("html")
%output:html-version('5')
%rest:path("/{$lang=[a-z]{2}}/{$collection=.+}/{$doc=[a-zA-Z0-9_\-]+}.html")
function routes:document($lang as xs:string, $collection as xs:string, $doc as xs:string) as element(html){
%rest:path("/{$lang=[a-z]{2}}/{$path=.+}.html")
function routes:consult($lang as xs:string, $path as xs:string){
    try {
        utils:doc-to-html($collection, $doc, $lang)
        let $xqueryFile := utils:get-autoroute-file-path($path || '.xq')
        return xquery:eval(xs:anyURI($xqueryFile), map {'lang': $lang})
    }
    catch * {
           let $fullPath := $path || '.xml'
           return
               if(db:exists($database:max-db, $fullPath))
               then
                   utils:xml-to-html($lang, $fullPath)
               else
                   let $id := fn:tokenize($path, '/')[last()]
                   let $docPath := string-join(tokenize($path, "/")[1 to last()-1], "/") || '.xml'
                   return
                    if(database:get-element-by-id($docPath,$id))
                    then utils:xml-to-html($lang, $path, $id)
                    else errors:not-found('no autoroute, no fragment with path ' || $docPath || ' or id ' || $id, $lang)
    }
    catch err:max-database{errors:error($err:code, $err:module || ': ' ||  $err:description, $err:line-number, $lang)}
};


(:~
 : Returns a page (HTML or Markdown) from the "content_html" directory
 : @param $lang language
@@ -89,37 +104,6 @@ function routes:page($lang as xs:string, $path as xs:string){
};


(:~ Returns the result of autoroute/$page.xq if exists, else transforms $page document as HTML
 : @param $lang language
 : @param $page xq filepath or document path
 : @return HTML page
 :)
declare
%rest:GET
%output:method("html")
%output:html-version('5')
%rest:path("/{$lang=[a-z]{2}}/{$page=[a-zA-Z0-9_\-]+}.html")
function routes:autoroute($lang as xs:string, $page as xs:string){
    try {
        let $xqueryFile := utils:get-autoroute-file-path($page || '.xq')
        return xquery:eval(xs:anyURI($xqueryFile), map {'lang': $lang})
    }
    catch * {
            try {
                let $content := utils:doc-to-html((), $page, $lang)
                return $content
            }
            catch err:max-database {
                errors:error($err:code, $err:description, $err:line-number, $lang)
            }
            catch err:XPTY0004 {
                errors:error($err:code, $page || ' - Please check autoroute file : ' || $page  || " : " ||$err:description, $err:line-number, $lang)
            }
            catch * {errors:error($err:code, $err:description, $err:line-number, $lang)}
    }
};


(:~
 : Returns a static file.
 : @param  $filepath  file or unknown path