From a1551b074bc5337f9c1cf92b67a56a78dc3e6b0c Mon Sep 17 00:00:00 2001
From: annegf <anne.garciafernandez@univ-grenoble-alpes.fr>
Date: Wed, 5 Feb 2025 10:20:38 +0100
Subject: [PATCH] Improve results presentation

---
 data/xslt/tei2search_appSetup.xsl | 120 +++++++++++++++++++-----------
 1 file changed, 76 insertions(+), 44 deletions(-)

diff --git a/data/xslt/tei2search_appSetup.xsl b/data/xslt/tei2search_appSetup.xsl
index 303465a7..6da8d8cf 100644
--- a/data/xslt/tei2search_appSetup.xsl
+++ b/data/xslt/tei2search_appSetup.xsl
@@ -20,7 +20,7 @@
     xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xs tei" version="2.0">
 
     <xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
-    
+
     <xsl:param name="data_json_file">
         <xsl:text>static/json/auto_index.json</xsl:text>
     </xsl:param>
@@ -31,8 +31,9 @@
     </xsl:variable>
 
     <xsl:template match="/">
-        <xsl:variable name="all_witness_id" select=" distinct-values(//@wit/tokenize(normalize-space(.), ' '))"/>
-        
+        <xsl:variable name="all_witness_id"
+            select="distinct-values(//@wit/tokenize(normalize-space(.), ' '))"/>
+
         <xsl:variable name="witnesses_as_list">
             <xsl:for-each select="$all_witness_id">
                 <xsl:text>'</xsl:text>
@@ -43,19 +44,20 @@
                 </xsl:if>
             </xsl:for-each>
         </xsl:variable>
-        
+
         <xsl:text>// Setup data</xsl:text>
         <xsl:value-of select="$br"/>
         <xsl:text>let $searchable_fields = ['lem',</xsl:text>
         <xsl:value-of select="$witnesses_as_list"/>
         <xsl:text>];</xsl:text>
         <xsl:value-of select="$br"/>
-        
+
         <xsl:text>let $showable_fields = ['book', 'chapter', 'n', 'lem',</xsl:text>
         <xsl:value-of select="$witnesses_as_list"/>
         <xsl:text>];</xsl:text>
         <xsl:value-of select="$br"/>
-        
+        <xsl:value-of select="$br"/>
+
         <xsl:text>// Setup MiniSearch</xsl:text>
         <xsl:value-of select="$br"/>
         <xsl:text>let miniSearch = new MiniSearch({</xsl:text>
@@ -66,57 +68,87 @@
         <xsl:value-of select="$br"/>
         <xsl:text>})</xsl:text>
         <xsl:value-of select="$br"/>
-        
+        <xsl:value-of select="$br"/>
+
         <xsl:text>let $data_json = '</xsl:text>
         <xsl:value-of select="$data_json_file"/>
         <xsl:text>';</xsl:text>
         <xsl:value-of select="$br"/>
         <xsl:value-of select="$br"/>
-        
+
         <xsl:text disable-output-escaping="yes">const renderSearchResults = (results) => {</xsl:text>
         <xsl:value-of select="$br"/>
-        <xsl:text>$resultsList.innerHTML = results.map(({ book, chapter, n, lem,</xsl:text>
+
+        <xsl:text disable-output-escaping="yes">    //JSON.stringify(results);</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    const books = new Set(results.map(a => a.book));</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    let $result;</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    $resultsList.innerHTML = "";</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    for (let b of books){</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">        $resultsList.innerHTML += "&lt;h4>Livre "+b+"&lt;/h4>\n";</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">        results_by_book = results.filter((result) => result.book == b); //filter by book</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">        results_by_book.sort((a,b) => { //sort by verse number</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">          return (a.n - b.n);</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">        });</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:value-of select="$br"/>
+
+        <xsl:text>    $resultsList.innerHTML += results_by_book.map(({ book, chapter, n, lem,</xsl:text>
         <xsl:variable name="simple_quote">
             <xsl:text>'</xsl:text>
         </xsl:variable>
-        <xsl:value-of select="replace($witnesses_as_list,$simple_quote,'')"/>
+        <xsl:value-of select="replace($witnesses_as_list, $simple_quote, '')"/>
         <xsl:text disable-output-escaping="yes"> }) => {</xsl:text>
         <xsl:value-of select="$br"/>
-        let $result=`<h5 class="mt-3">
-                <xsl:text>Vers </xsl:text> 
-                <a>
-                    <xsl:attribute name="href">
-                        <xsl:text>/book${book}/${chapter}#${n}</xsl:text>
-                    </xsl:attribute>
-                    <xsl:text>${n}</xsl:text>
-                </a>
-            </h5>
-            <dl>
-                <dt>Livre : </dt>
-                <dd>${book}</dd>
-                <dt>Chapitre : </dt>
-                <dd>${chapter}</dd>
-                <dt>Lemme :</dt>
-                <dd>${lem}</dd>`
-        <xsl:for-each select="$all_witness_id">
-            <xsl:text>if (! (`${</xsl:text>
-            <xsl:value-of select="replace(.,'#','')"/>
-            <xsl:text>}` === "undefined") ) {</xsl:text>
-            $result += `
-            <dt><xsl:value-of select="replace(.,'#','')"/> : </dt>
-            <dd>
-                <xsl:text>${</xsl:text>
-                <xsl:value-of select="replace(.,'#','')"/>
-                <xsl:text>}</xsl:text>
-            </dd>
-            `}
-        </xsl:for-each>
-                $result += `</dl>`
-        <xsl:text>return $result</xsl:text>
+        <xsl:text disable-output-escaping="yes">    let $chap = `${chapter}`.substring(0, `${chapter}`.indexOf("_"));</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    $result='&lt;h5 xmlns="http://www.w3.org/1999/xhtml" class="mt-3">Vers &lt;a href="';</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    $result+=`/book${book}/${chapter}#${n}">${n}&lt;/a> (chapitre `</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    $result+=$chap</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text disable-output-escaping="yes">    $result+=`)&lt;/h5></xsl:text>
+        <dl>
+            <dt>BnFfr2817 :</dt>
+            <dd>${lem}</dd>
+            <xsl:text>`</xsl:text>
+            <xsl:for-each select="$all_witness_id">
+                <xsl:value-of select="$br"/>
+                <xsl:text> if (! (`${</xsl:text>
+                <xsl:value-of select="replace(., '#', '')"/>
+                <xsl:text>}` === "undefined") ) {</xsl:text>
+                <xsl:value-of select="$br"/>
+                <xsl:text>   $result += `</xsl:text>
+                <dt><xsl:value-of select="replace(., '#', '')"/> : </dt>
+                <dd>
+                    <xsl:text>${</xsl:text>
+                    <xsl:value-of select="replace(., '#', '')"/>
+                    <xsl:text>}</xsl:text>
+                </dd>
+                <xsl:value-of select="$br"/>
+                <xsl:text> `}</xsl:text>
+            </xsl:for-each>
+            <xsl:value-of select="$br"/>
+            <xsl:text> $result += `</xsl:text>
+        </dl>
+        <xsl:text>`</xsl:text>
+        <xsl:value-of select="$br"/>
+        <xsl:text>    return $result</xsl:text>
         <xsl:value-of select="$br"/>
         <xsl:text>}).join('\n')</xsl:text>
         <xsl:value-of select="$br"/>
-        
+        <xsl:text>}</xsl:text>
+        <xsl:value-of select="$br"/>
+
         <xsl:text disable-output-escaping="yes">
         if (results.length > 0) {
             $app.classList.add('hasResults')
@@ -131,6 +163,6 @@
         .slice(0, 5)
       }
         </xsl:text>
-        
+
     </xsl:template>
-</xsl:stylesheet>
\ No newline at end of file
+</xsl:stylesheet>
-- 
GitLab