From 3e69dc361669cab60997882a6cf1bf879a7bc05d Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 29 Jun 2022 00:08:28 +0530 Subject: 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. --- tissue/document.scm | 30 ++++++++++++++++++++++++------ tissue/issue.scm | 7 +------ 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-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 "" + #:highlight-end "" + #: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 ) 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) -- cgit v1.2.3