summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2022-06-29 00:08:28 +0530
committerArun Isaac2022-06-29 00:09:29 +0530
commit3e69dc361669cab60997882a6cf1bf879a7bc05d (patch)
tree25d0abb6ffddf74a416f2887c63328a6f6b97f9c
parent71ae09d9d34a1780c653755bbb4d6c1f5b67906a (diff)
downloadtissue-3e69dc361669cab60997882a6cf1bf879a7bc05d.tar.gz
tissue-3e69dc361669cab60997882a6cf1bf879a7bc05d.tar.lz
tissue-3e69dc361669cab60997882a6cf1bf879a7bc05d.zip
document: Abstract out document snippet selection.
* tissue/document.scm: Import (htmlprag).
(document-snippet): New public function.
(print): Use document-snippet.
* tissue/issue.scm (print): Use document-snippet.
-rw-r--r--tissue/document.scm30
-rw-r--r--tissue/issue.scm7
2 files changed, 25 insertions, 12 deletions
diff --git a/tissue/document.scm b/tissue/document.scm
index 70a3263..63aad96 100644
--- a/tissue/document.scm
+++ b/tissue/document.scm
@@ -24,6 +24,7 @@
   #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-171)
   #:use-module (ice-9 match)
+  #:use-module (htmlprag)
   #:use-module (oop goops)
   #:use-module (term ansi-color)
   #:use-module (xapian xapian)
@@ -37,6 +38,7 @@
             document-id-term
             document-text
             document-term-generator
+            document-snippet
             print
             <file-document>
             file-document-path
@@ -167,6 +169,27 @@ and further text, increase-termpos! must be called before indexing."
     (index-text! term-generator (file-document-path document))
     term-generator))
 
+(define (document-snippet document mset)
+  "Return snippet for DOCUMENT. MSET is the xapian MSet object
+representing a list of search results."
+  ;; mset-snippet returns serialized HTML. So, we reverse it with
+  ;; html->sxml.
+  (match (html->sxml (mset-snippet mset
+                                   (document-text document)
+                                   #:length 200
+                                   #:highlight-start "<b>"
+                                   #:highlight-end "</b>"
+                                   #:stemmer (make-stem "en")))
+    (('*TOP* children ...)
+     (string-join
+      (map (match-lambda
+             ;; Colorize string instead of HTML bold.
+             (('b str) (colorize-string str 'BOLD 'ON-RED))
+             ;; Else, return verbatim.
+             (str str))
+           children)
+      ""))))
+
 (define-method (print (document <file-document>) mset)
   "Print DOCUMENT in command-line search results. MSET is the xapian
 MSet object representing a list of search results."
@@ -175,12 +198,7 @@ MSet object representing a list of search results."
   (display (colorize-string (file-document-path document) 'YELLOW))
   (newline)
   (newline)
-  (let ((snippet (mset-snippet mset
-                               (document-text document)
-                               #:length 200
-                               #:highlight-start (color 'BOLD 'ON-RED)
-                               #:highlight-end (color 'RESET)
-                               #:stemmer (make-stem "en"))))
+  (let ((snippet (document-snippet document mset)))
     (unless (string-null? snippet)
       (display snippet)
       (newline)
diff --git a/tissue/issue.scm b/tissue/issue.scm
index de01ca8..9b2f277 100644
--- a/tissue/issue.scm
+++ b/tissue/issue.scm
@@ -136,12 +136,7 @@
                               (number->string (issue-tasks issue))
                               " tasks done")))
     (newline)
-    (let ((snippet (mset-snippet mset
-                                 (document-text issue)
-                                 #:length 200
-                                 #:highlight-start (color 'BOLD 'ON-RED)
-                                 #:highlight-end (color 'RESET)
-                                 #:stemmer (make-stem "en"))))
+    (let ((snippet (document-snippet issue mset)))
       (unless (string-null? snippet)
         (display snippet)
         (newline)