Commit e9cd2e87 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

restore /id/ in route for fragment with id. #14

parent 57da0d72
Loading
Loading
Loading
Loading
+19 −31
Original line number Diff line number Diff line
@@ -10,27 +10,6 @@ import module namespace i18n = "https://certic.unicaen.fr/max/i18n";
import module namespace database = "https://certic.unicaen.fr/max/database";
import module namespace errors = 'https://certic.unicaen.fr/max/errors' ;

declare function dumb-xml:doc-to-html($document as document-node(), $lang as xs:string) as element(html){
    let $customXQ := file:current-dir()|| 'xq/document.xq'
    let $content := if(file:exists($customXQ))
                    then xquery:eval(xs:anyURI($customXQ), map {'doc': $document, 'lang': $lang})
                    else
                        (:generate random http link to an identified node in $document:)
                        let $identifiers:= $document//*/@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($document), '/' || $database:max-db || '/', '')}</h2>
                                {$document}
                                <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})
};

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

@@ -38,6 +17,8 @@ declare function dumb-xml:xml-to-html($lang as xs:string, $path as xs:string, $i
         let $content := if(file:exists($customXQ) and not($id))
                         then xquery:eval(xs:anyURI($customXQ), map {'path': $path, 'lang': $lang})
                         else
                             let $randomLink :=
                                if(not($id)) then
                                     (:generate random http link to an identified node in $document:)
                                     let $identifiers:= database:get-document($path)//*/@xml:id/string()
                                     let $n := count($identifiers)
@@ -45,12 +26,19 @@ declare function dumb-xml:xml-to-html($lang as xs:string, $path as xs:string, $i
                                     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>
                                     else ()
                             return
                                 <div>
                                 <h2>{i18n:translate('raw', $lang)} | {fn:replace(fn:base-uri(database:get-document($path)), '/' || $database:max-db || '/', '')}</h2>
                                     {
                                     if(not($id))
                                        then database:get-document($path)
                                        else database:get-element-by-id($path, $id)
                                     }
                                     {$randomLink}
                                 </div>
         return templating:render('page.html', map {'content': $content, 'lang': $lang})
};
+9 −6
Original line number Diff line number Diff line
@@ -63,12 +63,15 @@ function routes:consult($lang as xs:string, $path as xs:string){
               then
                   utils:xml-to-html($lang, $fullPath)
               else
                    if(tokenize($path,'/')[last()-1] = 'id')
                    then
                       let $id := fn:tokenize($path, '/')[last()]
                   let $docPath := string-join(tokenize($path, "/")[1 to last()-1], "/") || '.xml'
                       let $docPath := string-join(tokenize($path, "/")[1 to last()-2], "/") || '.xml'
                       return
                        if(database:get-element-by-id($docPath,$id))
                    then utils:xml-to-html($lang, $path, $id)
                        then utils:xml-to-html($lang, $docPath, $id)
                        else errors:not-found('no autoroute, no fragment with path ' || $docPath || ' or id ' || $id, $lang)
                    else errors:not-found('no autoroute, no fragment with path ' || $path, $lang)
    }
};